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