#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define pb push_back
#define mp make_pair
#define in insert
#define INF 1e9
#define eps 0.0000000001
#define fi first
#define se second
#define y1 yy
#define tm tmm
#define speedup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

typedef long long ll;

//---------------------------------------|

const int DIM=1e6+7;
const int DIM2=4e3+7;

ll n,m,x,y,c,t,res,pr,beg,k;
ll a[DIM];
ll tmp[DIM2];
queue<ll> tq;
queue<ll> tm[DIM2];
ll tl[DIM];

//---------------------------------------|

void df(ll v)
{
   c++;
   if (c==res-1)
   {
       cout << n*m+1 << " " << v << endl;
       return;
   }

   ll f=tl[v];

   if (tm[f].size()==0)
   {
       k=(v-1)/m;
       for (int i=1+k*m;i<=m+k*m;i++)
       {
           if (i!=v&&tl[i]!=0)
           {
               swap(tl[v],tl[i]);
               break;
           }
       }
   }

   f=tl[v];

   ll s=tm[f].front();
   tm[f].pop();
   tl[v]=0;
   cout << s << " " << v << endl;
   df(s);
}

int main()
{
    //speedup;

    cin >> n >> m;

    for (int i=1;i<=n*m;i++)
    {
        cin >> a[i];
    }

    for (int i=1;i<=n;i++)
    {
        c=m*(i-1);
        for (int j=1;j<=m;j++) tmp[j]=0;
        for (int j=1+c;j<=m+c;j++)
        {
            if (tmp[a[j]])
            {
                tm[a[j]].push(j);
                tq.push(j);
            }
            tmp[a[j]]=1;
        }
        for (int j=1;j<=m;j++)
        {
            if (tmp[j]==0)
            {
                ll k=tq.front();
                tq.pop();
                tl[k]=j;
                res++;
            }
        }
    }

    if (res==0)
    {
        cout << 0 << endl;
        return 0;
    }

    res++;

    cout << res << endl;

    /*for (int i=1;i<=n*m;i++) cout << tl[i] <<" ";cout << endl;
    for (int i=1;i<=m;i++)
    {
        cout << i << " : ";
        while (tm[i].size()>0)
        {
            cout << tm[i].front() << " ";
            tm[i].pop();
        }
        cout << endl;
    }*/

    c=0;
    for (int i=1;i<=m;i++)
    {
        if (tm[i].size()!=0)
        {
            cout << tm[i].front() << " " << n*m+1 << endl;
            ll g=tm[i].front();
            tm[i].pop();
            df(g);
            break;
        }
    }

    return 0;
}
