#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define endl '\n'
const int N = 400+7;
vector<int> V[N][N];
set<int> need[N],thr[N];
int cnt[N],A[N*N];
void solve(){
    int n,m;
    cin>>n>>m;
    for(int i = 1;i<=n;i+=1){
        for(int j = 1;j<=m;j+=1){
            int cur; cin>>cur;

            V[i][cur].push_back((i-1)*m+j);
        }
        for(int j = 1;j<=m;j+=1){
            cnt[i] += !V[i][j].empty();
            if (V[i][j].empty()){
                need[j].insert(i);
            }
            if (V[i][j].size()>1){
                thr[i].insert(j);
            }
        }
    }
    vector<pair<int,int> > ans;
    while(1){
        int start = -1;
        for(int i = 1;i<=n;i+=1){
            if (cnt[i]!=m){
                start = i;
            }
        }
        if (start==-1){
            break;
        }
        int cur = start;

        vector<int> seq;
        while(!thr[cur].empty()){
            int col = *thr[cur].begin();
            seq.push_back(V[cur][col].back());
            V[cur][col].pop_back();
            if (V[cur][col].size()==1){
                thr[cur].erase(thr[cur].begin());
            }
            int next = *need[col].begin();
            need[col].erase(need[col].begin());
            V[next][col].push_back(seq.back());
            cnt[next] += 1;
            cur = next;
        }
        int last = n*m+1;
        while(!seq.empty()){
            ans.push_back({seq.back(),last});
            last = seq.back();
            seq.pop_back();
        }
        ans.push_back({n*m+1,last});
    }
    cout<<ans.size()<<endl;
    for(auto [a,b]:ans){
        cout<<a<<' '<<b<<endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
