#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, m;
    cin>>n>>m;
    int a[n*m+1];
    vector<int> vd[n+1], vg[n+1][m+1], lf, rd;
    for (int i=1; i<=n*m; i+=m)
    {
        map<int, int> mp, mp2;
        for (int j=0; j<m; j++)
        {
            cin>>a[i+j];
            mp[a[i+j]]--;
            mp2[a[i+j]]=1;
            if (mp[a[i+j]]<=-2)
                vg[i][a[i+j]].push_back(i+j);
        }
        for (int j=1; j<=m; j++)
         {
            if (mp2[j]==0)
                vd[i].push_back(j);
         }
    }
    for (int i=1; i<=n; i++)
    {
        for (int j=0; j<vd[i].size(); j++)
        {
            for (int h=1; h<=n; h++)
            {
                if (i!=h)
                {
                    if (vg[h][vd[i][j]].size()!=0)
                    {
                        for (int l=0; l<vd[h].size(); l++)
                        {
                            if (vg[i][vd[h][l]].size()!=0)
                            {
                                lf.push_back(vg[h][vd[i][j]][0]);
                                rd.push_back(vg[i][vd[h][l]][0]);
                                vg[h][vd[i][j]].insert(vg[h][vd[i][j]].begin(), vg[h][vd[i][j]][0]);
                                vg[i][vd[h][l]].insert(vg[i][vd[h][l]].begin(), vg[i][vd[h][l]][0]);
                                vd[i].insert(vd[i].begin()+j, vd[i][j]);
                                vd[h].insert(vd[h].begin()+l, vd[h][l]);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<lf.size();
    for (int i=0; i<lf.size(); i++)
    {
        cout<<lf[i]<<" "<<rd[i]<<endl;
    }
}
