#pragma GCC Optimize("O3")
#include "bits/stdc++.h"

using namespace std;

typedef int ll;
typedef pair<ll,ll>pairll;
typedef long double ld;

#define pb push_back
#define fr first
#define sc second
#define INF 1000000000000000007
#define MOD 1000000007
#define endl '\n'

ll n,m,d[160007],f[407];

queue<ll>a[407],q[407];

queue<pairll>res;

int main() {

    cin>>n>>m;
    for(int i=1;i<=n*m;i++){
        cin>>d[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            f[j]=0;
        }
        for(int j=(i-1)*m+1;j<=(i-1)*m+m;j++){
            if(f[d[j]]==0){
                f[d[j]]=1;
            }else{
                q[d[j]].push(j);
            }
        }
        for(int j=1;j<=m;j++){
            if(f[j]==0){
                a[i].push(j);
            }
        }
    }
    ll x=0;
    ll y=0;
    while(1){
        x=0;
        y=0;
        ll mx=0;
        for(int i=1;i<=m;i++){
            mx=max(mx,ll(q[i].size()));
        }
        for(int i=1;i<=m;i++){
            if(q[i].size()==mx && mx>0){
                y=i;
                x=q[i].front();
                q[i].pop();
                break;
            }
        }
        if(x==0)break;
        res.push({x,n*m+1});
        while(1){
            ll z=(x+m-1)/m;
            ll h=a[z].front();
            a[z].pop();
            if(h==y && q[h].size()==0){
                res.push({n*m+1,x});
                break;
            }
            ll g=q[h].front();
            q[h].pop();
            res.push({g,x});
            x=g;
        }
    }
    cout<<res.size()<<endl;
    while(res.size()>0){
        cout<<res.front().fr<<" "<<res.front().sc<<endl;
        res.pop();
    }
    
    return 0;

}
