
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int s;
vector<int> G[2000];
int ptr[2000];
int skipped[2000];
int cnt = 0;
int N;
vector<int> ans;
int a[160007];
deque <int> p[500][500];
void dfs(int v){
    ans.push_back(v);
    if(v == s and ptr[v] == G[v].size()) return;
    if(skipped[v]!=-1 and ptr[v] == G[v].size()){
        ptr[v] = skipped[v];
        skipped[v] = -2;
    }
    int to = G[v][ptr[v]];
    ptr[v]++;
    if(to != s){
        dfs(to);
    }
    else{
        if(ptr[to] < G[to].size())
            dfs(to);
        else if(skipped[v] == -2 or ptr[v] == G[v].size())
            dfs(to);
        else{
            skipped[v] = ptr[v]-1;
            int To = G[v][ptr[v]];
            ptr[v]++;
            dfs(To);
        }
    }
}
inline void solve(){
    int n;
    cin >> n;
    int m;
    cin >> m;
    N = n+m;
    for (int i = 0; i <= 1000+m; ++i) {
        skipped[i] = -1;
    }
    for (int i = 0; i < n*m; ++i) {
        int x;
        cin >> x;
        p[i/m][x].push_back(i);
        if(p[i/m][x].size() > 1) G[i/m].push_back(x+1000);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if(p[i][j].size() == 0) G[1000+j].push_back(i);
        }
    }
    vector<pair<int,int>> answer;
    for (int i = 0; i < n; ++i) {
        if(ptr[i] == G[i].size()) continue;
        s = i;
        dfs(i);
        int empty = n*m;
        int x = ans[ans.size()-2]-1000;
        int f = ans[ans.size()-3];
        int pos = p[f][x].front();
        p[f][x].pop_front();
        answer.push_back({pos,empty});
        empty = pos;
        for (int j = (int)ans.size()-3; j >= 2; j-=2) {
            int X = ans[j-1]-1000;
            int F = ans[j-2];
            int Pos = p[F][X].front();
            p[F][X].pop_front();
            answer.push_back({Pos,empty});
            empty = Pos;
        }
        answer.push_back({n*m,empty});
        ans.clear();
    }
    cout << answer.size() << "\n";
    for (int i = 0; i < answer.size(); ++i) {
        cout << answer[i].first+1 << " " << answer[i].second+1 << "\n";
    }
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) solve();
    return 0;
}


// D - ейлеровый обход