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

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

	int n,m;
	cin>>n >> m;
    int arr[n*m + 1];
    for(int i = 0; i < n*m; i += 1){
        cin>>arr[i];
    }
    arr[n*m] = 0;
    vector<pair<int,int> > ans;
    for(int i = 0;i<n*m;i += 1){
        int col[m]={0};
        int pos = i - i % m;
        for(int j = pos;j < pos + m; j += 1){
            col[arr[j] - 1] += 1;
        }
        int el = -1;
        for(int j = 0; j<m;j += 1){
            if(col[j] > 1)el =j;
        }
        if(el>-1){
        int num = pos;
        for(int j = pos; j < pos + m; j+= 1){
            if(arr[j] == el+1 ){
                num = j;
            }
        }
        for(int j = 0; j<m;j += 1){
            if(col[j] < 1)el =j;
        }
        ans.push_back({num + 1, n*m+1});
        swap(arr[n*m], arr[num]);
        int x = 0;
        while(1 > 0){
            bool t = 0;
            for(int j = 0; j < n; j += 1){
                for(int k = 0; k<m;k++){
                    col[k] = 0;
                }
                for(int k= j*m; k< j*m + m; k += 1){
                    if(arr[k]>0)
                    col[arr[k] -1]+=1;
                }
                if(col[el] > 1){
                    t = 1;
                    int el1 = 0, num1 = 0;
                    for(int k = j*m; k<j*m+m; k+=1){
                        if(col[k%m]==0)el1 =k%m;
                        if(arr[k]==el+1)num1=k;
                    }
                    ans.push_back({num1 + 1, num + 1});
                    swap(arr[num],arr[num1]);
                    num = num1;
                    el = el1;
                    break;
                }
            }
            x++;
            if(t==0||x>100){
                ans.push_back({n*m + 1, num + 1});
                swap(arr[n*m],arr[num]);
                break;
            }
        }
        }
    }
    cout<<ans.size()<<"\n";
    for(int i = 0; i < ans.size(); i += 1){
        cout<<ans[i].first<<' '<<ans[i].second<<"\n";
    }

}
