#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 < z){
                        sp = mp[i][j][z];
                        c = i;
                        mx = z;
                    }
                }
            }
        }
    }
    if(res == 0){
        cout << 0;
        return 0;
    }
    cout << res+1 << endl;
    sp = dn[a[sp]].back();
    vector<pair<int, int> > ans;
    ans.push_back(make_pair(sp+1, n*m+1));
    dn[a[sp]].pop_back();
    c = sp/m;
    bool ki = 0;
    while(--res){
        int lsp = dn[nd[c].back()].back();
        if(nd[c].size() == 0){
            ki = 1;
            break;
        }
        if(dn[nd[c].back()].size()){
            ans.push_back(make_pair(dn[nd[c].back()].back()+1, sp+1));
            dn[nd[c].back()].pop_back();
            nd[c].pop_back();
        }else{
            int ll = nd[c].back();
            if(nd[c].size() == 0){
                ki = 1;
                break;
            }
            nd[c].pop_back();
            if(nd[c].size() == 0){
                ki = 1;
                break;
            }
            if(dn[nd[c].back()].size() == 0){
                ki = 1;
                break;
            }
            lsp = dn[nd[c].back()].back();
            ans.push_back(make_pair(dn[nd[c].back()].back()+1, sp+1));
            dn[nd[c].back()].pop_back();
            nd[c].pop_back();
            nd[c].push_back(ll);
        }
        sp = lsp;
        c = lsp/m;
    }
    ans.push_back(make_pair(n*m+1, sp+1));
    if(!ki){
        for(int i = 0; i < ans.size(); i++){
            cout << ans[i].first << ' ' << ans[i].second << endl;
        }
        return 0;
    }
    //here
    vector<int> ndd[n];
    vector<int> dnn[m+1];
    map<int, vector<int> > mpp[n];
    for(int i = 0; i < n*m; i++){
        mpp[i/m][a[i]].push_back(i);
    }
     res = 0;
     c = -1;
     sp = -1;
     mx = -1;
    for(int i = 0; i < n; i++){
        for(int j = 1; j <= m; j++){
            if(mpp[i].find(j) == mpp[i].end()){
                ndd[i].push_back(j);
                res++;
            }else if(mpp[i][j].size() > 1){
                for(int z = 0; z < mpp[i][j].size()-1; z++){
                    dnn[j].push_back(mpp[i][j][z]);
                    if(mx < z){
                        sp = mp[i][j][z];
                        c = i;
                        mx = z+4;
                    }
                }
            }
        }
    }
    sp = dnn[a[sp]].back();
    cout << sp+1 << ' ' << n*m+1 << endl;
    dnn[a[sp]].pop_back();
    c = sp/m;
    while(--res){
        int lsp = dnn[ndd[c].back()].back();
        if(dnn[ndd[c].back()].size()){
            cout << dnn[ndd[c].back()].back()+1 << ' ' << sp+1  << endl;
            dnn[ndd[c].back()].pop_back();
            ndd[c].pop_back();
        }else{
            int ll = ndd[c].back();
            ndd[c].pop_back();
            lsp = dnn[ndd[c].back()].back();
            cout << dnn[ndd[c].back()].back()+1 << ' ' << sp+1 << endl;
            dnn[ndd[c].back()].pop_back();
            ndd[c].pop_back();
            ndd[c].push_back(ll);
        }
        sp = lsp;
        c = lsp/m;
    }
    cout << n*m+1 << ' ' << sp+1 << endl;
}
