//
//  main.cpp
//  5
//
//  Created by Igor Romaniuk (del1ke) on 21.05.2022.
//
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimization("funroll-loops")
#pragma GCC target("avx")
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define ull unsigned ll
#define ld long double
#define pb push_back
#define fi first
#define se second
#define stop break
#define skip continue
#define elif else if
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define EXIT return 0
#define SZ(a) (ll)a.size()
#define PB push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define PI acos((ld)-1)
#define em emplace_back
#define all(x) x.begin(), x.end()
#define E exp((ld)1)
const ll MOD = 1e9 + 7, DIM = 1e5 + 7, INF = 1e12 + 7;
signed main () {
    FAST;
    int n, m;
    cin >> n >> m;
    int a[n * m];
    int b[n][m + 1];
    memset(b, 0, sizeof(b));
    vector<pll> ans;
    for (int i = 0; i < n * m; i++) {
        cin >> a[i];
        b[i / m][a[i]]++;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= m; j++) {
            if (b[i][j] == 0) {
                for (int k = i + 1; k < n; k++) {
                    if (b[k][j] > 1) {
                        ll pos = 0;
                        for (int q = i * m; q < (i + 1) * m; q++) 
                            if (a[q] == j) {
                                pos = q;
                                stop;
                            }
                        ans.PB({pos + 1, n * m + 1});
                        pos = 0;
                        for (int q = k * m; q < (k + 1) * m; q++) 
                            if (a[q] == j) {
                                pos = q;
                                stop;
                            }
                        ans.PB({n * m + 1, pos + 1});
                        b[i][j]++;
                        b[k][j]--;
                        stop;
                    }
                }
            }
        }
    }
    cout << SZ(ans) << "\n";
    for (auto to : ans) 
        cout << to.fi << " " << to.se << "\n";
    EXIT;
}
