#include <bits/stdc++.h>
#define mp make_pair
#define f first
#define s second
#define pb push_back

using namespace std;


vector<int> arr;
vector <pair <int, int>> moves;
int b, bpos, emppos, searchingFor;
int n, m;
struct shop{
    vector<pair<int, int>> h;
    vector<int> n;
};
vector <shop> shops;



void make_move(int p){
    moves.push_back(make_pair(p+1, emppos+1));
    emppos = p;
}

int find1(){
    for(int i = 0; i < n; i++){
        if(shops[i].h.empty() == false){
            b = shops[i].h[shops[i].h.size() - 1].f;
            int p = shops[i].h[shops[i].h.size() - 1].s;
            shops[i].h.pop_back();
            return p;
        }
    }
    return bpos;
}

int find2(){
    int sh = emppos / m;
    searchingFor = shops[sh].n[shops[sh].n.size() - 1];
    shops[sh].n.pop_back();
    for(int i = 0; i < n; i++){
        for(int j = 0; j < shops[i].h.size(); j++){
            if(shops[i].h[j].f == searchingFor){
                int p = shops[i].h[j].s;
                shops[i].h.erase(shops[i].h.begin()+j);
                return p;
            }
        }
    }
    int p = bpos;
    b = -1;
    return p;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	cin>>n>>m;

    b = -1;
    bpos = n*m;
    emppos = bpos;
    shops.resize(n);

    for(int i = 0; i < n*m; i++){
        int a;
        cin >> a;
        a--;
        arr.pb(a);
    }



    for(int i = 0; i < n; i++){
        vector<bool> used(m);
        for(int j = 0; j < m; j++){
            int a = arr[i*m+j];
            if(!used[a]){
                used[a] = true;
            }
            else{
                shops[i].h.pb(mp(a, i*m+j));
            }
        }
        for(int j = 0; j < m; j++){
            if(!used[j])
                shops[i].n.pb(j);
        }
    }

    while(true){
        if(emppos == bpos){
            int p = find1();

            if(p == bpos)
                break;
            else{
                make_move(p);
            }
        } else {
            int p = find2();
            make_move(p);
        }
    }

    cout << moves.size() <<'\n';
    for(auto i:moves){
        cout << i.f << ' ' << i.s << '\n';
    }

}
