#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pll pair<ll, ll>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> yree;
const ll inf = 0x3fff3fff3fff3fff;
const ll N = 404;
vector<ll> cnt[N][N];
//set<ll, greater<ll>> lovf[N];
set<ll> ovf[N];
set<ll> emp[N];
random_device rd;
mt19937_64 gen(rd());
signed main(){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
    ll n, m;
    cin >> n >> m;
    for(ll i=0;i<n;i++){
        for(ll j = 0;j<m;j++){
            ll s;
            cin >> s;
            --s;
            cnt[i][s].push_back(i*m+j);
        }
        for(ll j = 0;j<m;j++){
            if(cnt[i][j].size()>1) ovf[j].insert(i);//, lovf[i].insert(j);
            if(cnt[i][j].empty()) emp[i].insert(j);
        }
    }
    vector<pll> res;
    for(ll fem = 0;fem<n;fem++){
        if(emp[fem].empty()) continue;
        ll z = 0;
        for(ll j = 1;j<m;j++){
            if(cnt[fem][j].size() > 1) z=j;
        }
        ll cemp = n*m, ev=z;
        ll x = cnt[fem][z].back(); cnt[fem][z].pop_back();
        if(cnt[fem][z].size()==1){
            //lovf[fem].erase(lovf[fem].begin());
            ovf[z].erase(fem);
        }
        res.emplace_back(x, cemp), cemp=x;
        while(true){
            ll cer = cemp/m;
            ll u = -1;
            for(auto j : emp[cer]) if(ovf[j].size()){
                if(u==-1) u = j;
                else if(ovf[j].size()>=ovf[u].size()) u=j;
            }
            if(u==-1){
                auto it = emp[cer].find(ev);
                assert(it!=emp[cer].end());
                emp[cer].erase(it);
                res.emplace_back(n*m, cemp);
                cnt[cer][ev].push_back(cemp);
                break;
            }else{
                auto it = emp[cer].find(u);
                assert(it!=emp[cer].end());
                emp[cer].erase(it);
                ll q = *ovf[u].begin();
                for(auto j : ovf[u]){
                    if(emp[j].size() <= emp[q].size()) q=j;
                }
                ll x = cnt[q][u].back(); cnt[q][u].pop_back();
                if(cnt[q][u].size()==1){
                    //lovf[q].erase(u);
                    ovf[u].erase(ovf[u].find(q));
                }
                res.emplace_back(x, cemp);
                cemp=x;
            }
        }
    }
    cout<<res.size()<<'\n';
    for(auto [i, j] : res) cout<<i+1<<' '<<j+1<<'\n';
}
