#include <bits/stdc++.h>
/*
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
*/

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")

#define fast ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);

#define CR const&

#define FORI(i, a, b) for(int i = a, max##i = b; i < max##i; ++i)
#define FOR(i, n) FORI(i, 0, n)
#define RFORI(i, a, b) for(int i = a, min##i = b; i >= min##i; --i)
#define RFOR(i, n) RFORI(i, n, 0)
#define FORA(i, a) for(auto i : a)
#define FORAR(i, a) for(auto& i : a)
#define FORACR(i, a) for(auto CR i : a)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define Count(q) while(q--)

#define OK cerr << "OK" << endl;

#define pb push_back


using namespace std;

template<class T>
using V = vector<T>;
template<class T>
using VV = V<V<T>>;

using ld = long double;
using ll = long long;
using PLL = pair<ll, ll>;
using PII = pair<int, int>;
using VI = V<int>;
using VLL = V<ll>;
using VVI = VV<int>;
using VVLL = VV<ll>;
using Gr = VVI;
using MLL = map<ll, ll>;

template<class T>
constexpr T sqr(T x)
{
    return x * x;
}

template<class T, class U = T>
void cmin(T& a, U const& b)
{
    if(b < a)
        a = b;
}

template<class T, class U = T>
void cmax(T& a, U const& b)
{
    if(a < b)
        a = b;
}

ll get_time()
{
    using namespace chrono;
    return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
mt19937 rng(get_time());

void DFS(int i, int j, VI& res, VV<VI>& pos)
{
    res.pb(pos[i][j].back());
    pos[i][j].pop_back();

    int k;
    for(k = 0; k < pos.size(); ++k)
        if(pos[k][j].size() == 0)
            break;

    if(k == pos.size())
        return;

    for(int d = 0; d < pos[k].size(); ++d)
        if(pos[k][d].size() > 1)
        {
            int p = pos[k][d].back();
            pos[k][j] = {p};
            DFS(k, d, res, pos);
            return;
        }
}

int main()
{
    fast
    int n, m;
    cin >> n >> m;

    VI a(n * m);
    FORAR(i, a)
    {
        cin >> i;
        --i;
    }

    VV<VI> pos(n, VVI(m));
    FOR(i, n)
        FORI(j, i * m, (i + 1) * m)
            pos[i][a[j]].pb(j);

    V<PII> ans;
    FOR(i, n)
        FOR(j, m)
            if(pos[i][j].size() > 1)
            {
                VI res{n * m};
                DFS(i, j, res, pos);
                res.pb(n * m);
                reverse(ALL(res));

                FOR(k, res.size() - 1)
                    ans.pb({res[k], res[k + 1]});
            }

    cout << ans.size() << "\n";
    FORACR(i, ans)
        cout << i.first + 1 << " " << i.second + 1 << "\n";
}
