#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <cstring>
#define endl '\n'

using namespace std;
typedef long long ll;

vector<ll> used[401 * 401 + 7];

ll block[401 * 401 + 7];

vector<ll>d[401 * 401 + 7];

ll vilna;

vector<pair<ll, ll>> ans;

ll obrob[401 * 401 + 7];

void dfs(ll v){
    obrob[v] = 1;
    
    for(auto i: d[v]){
        if(obrob[i] == 1)
            continue;
        
        ans.push_back({i, vilna});
        ans.push_back({v, i});
        
        vilna = v;
        
        dfs(i);
    }
    return;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    ll n, m;
    cin >> n >> m;
    
    map<ll, vector<ll>>zayve, treba;
    
    for(int i = 1; i <= n * m; i++)
        block[i] = (i - 1) / m + 1;
    
    for(int b = 1; b <= n; b++){
        for(int j = 1; j <= m; j++){
            ll p;
            cin >> p;
            
            used[p].push_back((b - 1) * m + j);
        }
        
        ll notneed = -1, pos = 0;
        for(int j = 1; j <= m; j++)
            if(used[j].size() > 1)
                notneed = j, pos = used[j][1];
        
        ll need = -1, pos2 = 0;
        
        for(int j = 1; j <= m; j++)
            if(used[j].size() == 0)
                need = j, pos2 = pos;
        
        //cout << notneed << ' ' << pos << ' ' << need << ' ' << pos2 << endl;
        
        zayve[notneed].push_back(pos);
        treba[need].push_back(pos2);
        
        for(int j = 1; j <= m; j++)
            used[j].clear();
    }
    
    set<ll>st;
    
    for(int i = 1; i <= m; i++){
        if(zayve[i].size() == 0)
            continue;
        
        for(auto j1: zayve[i]){
            for(auto j2: treba[i]){
                st.insert(j1);
                st.insert(j2);
                
                d[j1].push_back(j2);
            }
        }
    }
    
    vilna = n * m + 1;
    
    for(auto i: st){
        if(obrob[i] == 0){
            dfs(i);
        }
    }
    
    cout << ans.size() << endl;
    
    for(auto i:ans){
        cout << i.first << ' ' << i.second << endl;
    }
    
    return 0;
}
/*
 
 4 3
 1 1 3
 1 2 2
 3 1 3
 3 2 2
 
 */
 
 
