#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio();cin.tie();cout.tie();
#define ops cout<<"ops"<<endl;
#define en cout<<endl;
#define fi first
#define se second

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pllll;
typedef string str;
typedef char cr;

const ll DIM=4e2+7;
const ll DIMM=1e5+7;
const ll DDIM=1e7+7;
const ll INF=1e16+7;
const ll X=1e9;
const ll BS=37;
const ll MODULO=1e9+7;

ll n,m,k;

ll endf;

ll a[DIM][DIM];

queue<ll> rest[DIM],need[DIM];
vector<pllll> res;

void change(ll p1,ll p2){
    res.push_back({p1,p2});
    swap(a[p1],a[p2]);
    return;
}

ll fb(ll p){
   return (p-1)/m+1;
}

ll pos(ll i,ll j){
   return (i-1)*m+j;
}

void solve(){

    cin>>n>>m;

    endf=n*m+1;

    ll val,p=(-1),pval;

    for(int i=1;i<=n;i++){

     for(int j=1;j<=m;j++){

      cin>>val;

      a[i][val]++;

      if(a[i][val]>1){
       if(p==(-1)){
        p=pos(i,j);
        pval=val;
        a[i][val]--;
        continue;
       }
       rest[val].push(pos(i,j));
      }

     }

     for(int j=1;j<=m;j++){
      if(a[i][j]>0)continue;
      need[i].push(j);
     }

    }

    if(p==(-1)){
     cout<<0<<endl;
     return;
    }

    change(p,endf);

    rest[pval].push(endf);

    ll block,p1;

    while(p!=endf){

     block=fb(p);

     val=need[block].front();
     need[block].pop();

     p1=rest[val].front();
     rest[val].pop();

     change(p1,p);

     p=p1;
    }

    cout<<res.size()<<endl;

    for(auto w:res)cout<<w.fi<<' '<<w.se<<endl;

    return;
}

int main()
{

    fast;

    int nt;

    nt=1;

    //cin>>nt;

    while(nt--)
        solve();

    return 0;
}
