
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
        vector <pair<ll, ll> > o = {};
int main()
{
    ll n, m;
    cin >> n >> m;
    vector <vector <ll>> v = {};
    vector <ll> g = {};
    for (ll i = 0; i < m; i++)
    {
        g.push_back(0);
    }
    vector <ll> w = {};
    for (ll i = 0; i < n; i++)
    {
        v.push_back(g);
        for (ll j = 0; j < m; j++)
        {
            ll a;
            cin >> a;
            a--;
            v[i][a]++;
            w.push_back(a);
        }
    }
    w.push_back(-1);
        ll k = -1;
        for (ll i = n - 1; i > -1; i--)
        {
            for (ll j = m - 1; j > -1; j--)
            {
                if (v[i][j] == 0) {k = j; break;}
            }
            if (k != -1) {break;}
        }
        ll p = 0;
        ll last = (m * n);
        for (ll i = 0; i < n; i++)
        {
            if (v[i][k] > 1) {v[i][k] --; p = i;}
        }
        for (ll i = 0; i < m; i++)
        {
            if (w[p * m + i] == k) {p = p * m + i; break;} 
        }
        o.push_back({p + 1, last + 1});
        w[last] = w[p];
        w[p] = -1;
        ll ind = p;
        while (ind != last)
        {
            k = ind / m;
            for (ll i = 0; i < m; i++)
            {
                if (v[k][i] == 0) {v[k][i] ++; k = i; break;}
            }
            ll p = last;
            for (ll i= 0; i < n; i++)
            {
                if (v[i][k] > 1) {v[i][k] --; p = i; break;}
            }
            if (p != last)
            {
                for (ll i = 0; i < n; i++)
                {
                    if (w[p * m + i] == k) {p = p * m + i; break;} 
                }
            }
            o.push_back({ind + 1, p + 1});
            w[ind] = w[p];
            w[p] = -1;
            ind = p;
        }
            cout << o.size() << "\n";
        for (ll i= 0; i < o.size(); i++)
        {
            cout << o[i].first << " " << o[i].second << "\n";
        }
}
