#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <iomanip>
#include <set>
#include <cmath>
#include <stack>

#define LF '\n'

using namespace std;
typedef long long int lli;

int n, m;
vector<stack<int>> z;
vector<stack<int>> d;
vector<pair<int, int>> ans;

void func(int p)
{
    int b = p / m + 1;
    if (p % m == 0) b--;
    int a = d[b].top();
    d[b].pop();
    if (z[a].empty()) {
        ans.push_back({ n * m + 1,p });
        return;
    }
    int x = z[a].top();
    z[a].pop();
    ans.push_back({ x,p });
    func(x);
}

void solve()
{
    cin >> n >> m;
    z.resize(m + 1);
    d.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        vector<int> used;
        used.resize(m + 1, 0);
        for (int j = 1; j <= m; j++) {
            int a;
            cin >> a;
            used[a]++;
            if (used[a] > 1) z[a].push((i-1) * m + j);
        }
        for (int k = 1; k <= m; k++) {
            if (used[k] == 0) {
                d[i].push(k);
            }
        }
    }

    for (int i = 1; i <= m; i++) {
        while (!z[i].empty()) {
            int x = z[i].top();
            z[i].pop();
            ans.push_back({ x,n * m + 1 });
            func(x);
        }
    }
    cout << ans.size() << LF;
    for (auto to : ans)
    {
        cout << to.first << " " << to.second << LF;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    solve();
}

