#include "bits/stdc++.h"
#define DIM 407
#define mod 100000007
#define eps 0.000000001
#define pi 3.14
#define INF 100000000000001
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<long long, long long> pll;


ll t, n, a[DIM * DIM], c[DIM], d[DIM], b, k, l, r, m, x, y, ans, siz;

vector <pll> need[DIM];
vector <pll> is[DIM];
queue <ll> q;

pll res[DIM * DIM];

bool fl;

//char c;
/*struct aa{
    ll x, y, num;
} a[DIM];*/
//map <ll, ll> c;
//vector <ll> v;

string s, s1[DIM], s2;
//priority_queue <ll, vector<ll>, greater<ll> > q;


int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n * m; i ++){
        cin >> a[i];
    }
    for(int i = 0; i < n; i ++){
        for(int j = 1; j <= m; j ++){
            c[a[i * m + j]] ++;
            if(c[a[i * m + j]] > 1){
                is[a[i * m + j]].push_back({i + 1, i * m + j});
                q.push(i * m + j);
                siz ++;
            }
        }
        for(int j = 1; j <= m; j ++){
            if(c[j] == 0){
                need[i + 1].push_back({j, q.front()});
                q.pop();
            }
            c[j] = 0;
        }
    }
    ll sec;
    for(int i = 1; i <= m; i ++){
        if(is[i].size() > 0){
            k = i;
            sec = is[i][0].fi;
            x = is[i][0].se;
            is[i].erase(is[i].begin());
            break;
        }
    }
    res[++ ans] = {x, n * m + 1};
    //cout << x << ' ' << n * m + 1 << endl;
    siz --;
    while(siz != 0){
        ll r = need[sec][0].fi;
        res[++ ans] = {is[r][0].se, x};
        //cout << is[r][0].se << ' ' << x << endl;
        x = is[r][0].se;
        sec = is[r][0].fi;
        need[sec].erase(need[sec].begin());
        is[r].erase(is[r].begin());
        siz -= 1;
    }
    res[++ ans] = {n * m + 1, x};
    cout << ans << endl;
    for(int i = 1; i <= ans; i ++){
        cout << res[i].fi << ' '<< res[i].se << endl;
    }
}
/*2 4 6 4 1 5 2 4 5 1  3  5  1  6  1  2  3  6  3  6  5  5  1  2  4  3  4  2  3  6
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 
 2 4 6 3 1 5 2 4 5 1  3  6  1  6  5  2  3  4  3  6  5  4  1  2  4  3  5  2  1  6
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
*/
