#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int a[n*m];
    vector<int> nd[n];
    vector<int> dn[m+1];
    map<int, vector<int> > mp[n];
    for(int i = 0; i < n*m; i++){
        cin >> a[i];
        mp[i/m][a[i]].push_back(i);
    }
    int res = 0;
    int c = -1;
    int sp = -1;
    int mx = -1;
    for(int i = 0; i < n; i++){
        for(int j = 1; j <= m; j++){
            if(mp[i].find(j) == mp[i].end()){
                nd[i].push_back(j);
                res++;
            }else if(mp[i][j].size() > 1){
                for(int z = 0; z < mp[i][j].size()-1; z++){
                    dn[j].push_back(mp[i][j][z]);
                    if(mx > mp[i][j].size()){
                        sp = mp[i][j][z];
                        c = i;
                        mx = mp[i][j].size();
                    }
                }
            }
        }
    }
    if(res == 0){
        cout << 0;
        return 0;
    }
    cout << res+1 << endl;
    sp = dn[a[sp]].back();
    cout << sp+1 << ' ' << n*m+1 << endl;
    dn[a[sp]].pop_back();
    c = sp/m;
    while(--res){
        int lsp = dn[nd[c].back()].back();
       // cout << ' ' << c << ' ' << nd[c].back() << "d ";
        if(dn[nd[c].back()].size()){
            cout << dn[nd[c].back()].back()+1 << ' ' << sp+1  << endl;
            dn[nd[c].back()].pop_back();
            nd[c].pop_back();
        }else{
            int ll = nd[c].back();
            nd[c].pop_back();
            lsp = dn[nd[c].back()].back();
            cout << dn[nd[c].back()].back()+1 << ' ' << sp+1 << endl;
            dn[nd[c].back()].pop_back();
            nd[c].pop_back();
            nd[c].push_back(ll);
        }
        sp = lsp;
        c = lsp/m;
    }
    cout << n*m+1 << ' ' << sp+1 << endl;

}
