#define _USE_MATH_DEFINES
 
#include <bits/stdc++.h>
 
#ifndef ONLINE_JUDGE
    #define my_dbg
#endif
 
#define speed_up ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define open_files freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#define all(x) x.begin(), x.end()
 
typedef long long ll;
 
using namespace std;

void solve() {
    ll n, m;
    cin >> n >> m;
    ll cnt = 1;
    vector<vector<ll>> spare(m + 1);
    vector<vector<ll>> need(m + 1);
    vector<int> val(n * m + 1);
    for (int i = 0; i < n; ++i) {
        map<int, int> mp;
        vector<int> vec(m);
        vector<int> dupes;
        for (int j = 0; j < m; ++j) {
            int el;
            cin >> el;
            vec[j] = el;
            val[cnt] = el;
            mp[el]++;
            if (mp[el] > 1) {
                spare[el].emplace_back(cnt);
                dupes.emplace_back(cnt);
            }
            ++cnt;
        }
        ll pos = 0;
        for (int h = 1; h <= m; ++h) {
            if (mp[h] == 0) {
                need[h].emplace_back(dupes[pos]);
                pos++;
            }
        }
    }
    vector<pair<ll, ll>> res;
    ll slot, last;
    for (int i = 1; i < m + 1; ++i) {
        if (spare[i].size()) {
            res.push_back({spare[i][0], n * m + 1});
            slot = i, last = n * m + 1;
            break;
        }
    }
    bool flag = true;
    ll init_val = slot, init_pos = spare[init_val][0];
    while (flag) {
        if (need[slot].size() == 0) break;
        if (need[init_val].size() == 0) {
            ll pos = 0;
            while (pos < need[slot].size() && val[need[slot][pos]] == init_val) ++pos;
            swap(need[slot][0], need[slot][pos]);
        }
        res.push_back({last, need[slot][0]});
        last = need[slot][0];
        ll need_delete = slot;
        slot = val[need[slot][0]];
        need[need_delete].erase(need[need_delete].begin());
    }
    cout << res.size() << "\n";
    for (pair<ll, ll> x : res) {
        cout << x.first << " " << x.second << "\n";
    }
}   
 
signed main() {
 
    speed_up
 
#ifdef my_dbg
    open_files
    clock_t processor_time = clock();
#endif
 
    signed t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
 
#ifdef my_dbg
    cout << "\nTIME : " << (clock() - processor_time) / (double)CLOCKS_PER_SEC;
#endif
 
}