#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;

using ll = long long;
using ld = long double;

ll n, m, k, q, x, y, z, a, b, c, d;
string s;
unordered_map< ll, unordered_set<ll> > m1;
unordered_map< ll, unordered_set<ll> > m0;
vector<ll> v;
vector< unordered_map< ll, unordered_set<ll> > > v1;
vector< pair<ll, ll> > ans;

void solve() {
    cin >> n >> m;
    v.resize(n * m);
    v1.resize(n);
    for (int i = 0; i < n * m; i++) {
        cin >> v[i];
        v[i]--;
        v1[i / m][v[i]].insert(i);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (v1[i][j].size() == 0) {
                m0[i].insert(j);
            }
            else if (v1[i][j].size() > 1) {
                while (v1[i][j].size() > 1) {
                    m1[j].insert(*v1[i][j].begin());
                    v1[i][j].erase(v1[i][j].begin());
                }
            }
        }
    }

    while (!m1.empty()) {
        auto mp1 = m1;
        auto mp0 = m0;
        for (auto &i : mp1) {
//            cout << "D1 " << i.first << endl;
            if (mp1[i.first].size() == 0) {
                m1.erase(i.first);
            }
        }
        for (auto &i : mp0) {
//            cout << "D0 " << i.first << endl;
            if (mp0[i.first].size() == 0) {
                m0.erase(i.first);
            }
        }
        if (m1.empty()) {
            break;
        }
//         for (auto &i : m1) {
//             cout << i.first + 1 << '\n';
//             for (ll j : i.second) {
//                 cout << '\t' << j + 1 << '\n';
//             }
//         }
//         cout << '\n';
//         for (auto &i : m0) {
//             cout << i.first + 1 << '\n';
//             for (ll j : i.second) {
//                 cout << '\t' << j + 1 << '\n';
//             }
//         }
//         cout << endl;
//         cout << endl;
//        cout << m1.size() << endl;
        for (int i = 0; i < n; i++) {
            if (m1[i].size() > 0) {
//                cout << i << ' ' << *m1[i].begin() << endl;
                ans.push_back({*m1[i].begin(), n * m});
                ll nm = *m1[i].begin();
                m1[i].erase(*m1[i].begin());
                if (m1[i].empty()) {
                    m1.erase(i);
                }
                ll x = nm / m;
                ll e = nm;
                while (m0[x].size() != 0) {
                    ll y = *m0[x].begin();
//                    cout << "DEBUG" << endl;
                    if (m1.count(y)) {
//                        cout << "DEBUG1" << endl;
                        ll z = *m1[y].begin();
                        ans.push_back({z, e});
//                        cout << "DEBUG1" << endl;
                        m0[x].erase(m0[x].begin());
                        if (m0[x].empty()) {
                            m0.erase(x);
                        }
//                        cout << "DEBUG1" << endl;
                        m1[y].erase(m1[y].begin());
                        if (m1[y].empty()) {
                            m1.erase(y);
                        }
//                        cout << "DEBUG1" << endl;
                        x = z / m;
                        e = z;
//                        cout << '\t' << x << ' ' << e << endl;
                    }
                    else {
//                        cout << "DEBUG2" << endl;
                        ans.push_back({n * m, e});
//                        cout << "DEBUG2" << endl;
                        m0[x].erase(m0[x].begin());
                        if (m0[x].empty()) {
                            m0.erase(x);
                        }
//                        cout << "DEBUG2" << endl;
                        break;
                    }
                }
//                cout << "DEBUG3" << endl;
                break;
            }
        }
    }
    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first + 1 << ' ' << ans[i].second + 1 << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout << setprecision(10);
    solve();
}
