#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll, ll> pll;
const ll MAXNUM=160007;
ll n, m, a[MAXNUM], place, curr_num, pointer, step_num, border;
pll steps[MAXNUM];
multiset<ll> num_in_block;
int main()
{
    scanf("%lld %lld", &n, &m);
    border=m*n+1;
    for(int i=1; i<=n*m; i++) scanf("%lld", &a[i]);
    for(int i=0; i<n; i++){
        num_in_block.clear();
        for(int j=1; j<=m; j++){
            curr_num=i*m+j;
            num_in_block.insert(a[curr_num]);
        }
        pointer=i*m+1;
        for(ll j=1; j<=m; j++){
            if(num_in_block.count(j)==0){
                while(num_in_block.count(a[pointer])<=1 && a[pointer]!=0) pointer++;
                if(a[pointer]!=0){
                    swap(a[border], a[pointer]);
                    step_num++;
                    steps[step_num]={pointer, border};
                    auto iter=find(a+1+(i+1)*m, a+n*m+1LL, j);
                    place=iter-a;
                    swap(a[place], a[pointer]);
                    step_num++;
                    steps[step_num]={place, pointer};
                    border=place;
                }
                else{
                    auto iter=find(a+1+(i+1)*m, a+n*m+1LL, i);
                    place=iter-a;
                    swap(a[place], a[pointer]);
                    step_num++;
                    steps[step_num]={place, pointer};
                    border=place;
                }
            }
        }
    }
    cout << step_num << "\n";
    for(int i=1; i<=step_num; i++){
        cout << steps[i].first << ' ' << steps[i].second << "\n";
    }
    for(int i=1; i<=n*m; i++) printf("%lld ", a[i]);
    return 0;
}

