#include <iostream>
#include <set>
#include <map>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;
typedef long long ll;
const int VERYLONGINT = 1000000001;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<vector<int>> a(n, vector<int>(m));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
            a[i][j]--;
        }
    vector<vector<pair<int, int>>> pr(n);
    vector<vector<pair<int, int>>> sp(n);
    for (int i = 0; i < n; i++) {
        vector<int> bul(m);
        for (int j = 0; j < m; j++)
            if (bul[a[i][j]] > 0)
                pr[i].push_back({ a[i][j], i * m + j });
            else
                bul[a[i][j]]++;
        int k = 0;
        for (int j = 0; j < m; j++)
            if (bul[j] == 0)
                sp[i].push_back({ j, pr[i][k++].second });
    }
    vector<vector<int>> spl(m);
    vector<vector<int>> prl(m);
    vector<pair<int, int>> res;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < pr[i].size(); j++)
            prl[pr[i][j].first].push_back(pr[i][j].second);
        for (int j = 0; j < sp[i].size(); j++)
            spl[sp[i][j].first].push_back(sp[i][j].second);
    }
    vector<bool> g(n * m + 1);
    int pos = -1;
    int pospu = -1;
    for (int i = 0; i < m; i++) {
        if (prl[i].size() == 0)
            continue;
        pos = prl[i][prl[i].size() - 1] / m;
        pospu = prl[i][prl[i].size() - 1];
        res.push_back({ prl[i][prl[i].size() - 1] + 1 , m * n + 1 });
        prl[i][prl[i].size() - 1] = n * m;
        break;
    }
    if (pos == -1) {
        cout << "0";
        return 0;
    }
    while (pospu != n * m) {
        int j = -1;
        for (int i = 0; i < sp[pos].size(); i++)
            if (!g[sp[pos][i].second] && sp[pos][i].second == pospu) {
                j = i;
                break;
            }
        g[sp[pos][j].second] = true;
        int sh = sp[pos][j].first;
        for (int i = 0; i < prl[sh].size(); i++)
            if (!g[prl[sh][i]]) {
                g[pospu] = true;
                res.push_back({ prl[sh][i] + 1, pospu + 1 });
                pospu = prl[sh][i];
                pos = pospu / m;
                break;
            }
    }
    cout << res.size() << "\n";
    for (int i = 0; i < res.size(); i++) {
        cout << res[i].first << " ";
        cout << res[i].second << "\n";
    }
    return 0;
}