#include <bits/stdc++.h>
//author: @van4o_0
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef long double ld;

typedef pair<int, int> p_ii;
typedef pair<int, ll> p_il;
typedef pair <ll, ll> p_ll;

typedef map<int, int> dict_ii;
typedef map <ll, ll> dict_ll;
typedef map<int, string> dict_is;

typedef vector<int> vi;
typedef vector <vi> vvi;
typedef vector <ll> vl;
typedef vector <vl> vvl;
typedef vector<bool> vb;
typedef vector <vb> vvb;
typedef vector<char> vc;
typedef vector <vc> vvc;

//define all(x) (x).begin(), (x).end()
//define sort(x) sort(all(x))

template<typename T>
istream &operator>>(istream &input, vector <T> &arr) {
    for (auto &e: arr) input >> e;
    return input;
}

template<typename T>
//print vector, default sep=" "
ostream &operator<<(ostream &print, vector <T> &arr_out) {
    string sep = " ";
    for (auto x: arr_out) print << x << sep;
    return print;
}

//define r(i, a, b) for (int i = a; i < (b); ++i)
//define range(i, b) for (int i = 0; i < (b); ++i)

void solve() {
    int n, m; cin >> n >> m;

    map<int, int> need;
    map<int, vi> take_i;

    vector<pair<vi, dict_ii>> a(n);
    for(int i=0;i<n;i++){
        vi b(m+1);

        map<int, vi> bb;
        vi vay;

        for(int ii=0;ii<m;ii++) {
            int now;cin >> now;

            if (b[now] == 0) b[now] = 1;
            else {
                b[now]++;
                bb[now].push_back(i*m + ii+1);
                vay.push_back(i*m + ii+1);
            }
        }
        for(int k = 1;k<m+1;k++){
            if(b[k] == 0) {
                need[vay[vay.size() - 1]] = k;
                vay.pop_back();
            }
            if(b[k] > 1) {
                for(int x: bb[k]) take_i[k].push_back(x);
            }
        }
    }

    if (!need.size()) {
        cout << 0 << endl;
        return;
    }

    cout << need.size() + 1 << endl;
    int last_el=0, last_position;

    for(pair p: need){
        last_el = p.second;
        last_position = p.first;

        cout << p.first << " " << n*m+1 << endl;
        break;
    }

    for(int i =0;i<need.size()-1;i++){
        cout << take_i[last_el][take_i[last_el].size()-1] << " " << last_position << endl;

        last_position = take_i[last_el][take_i[last_el].size()-1];
        take_i[last_el].pop_back();

        last_el = need[last_position];
    }
    cout << n*m+1 << " " <<  last_position << endl;
}


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

    int tt = 1;
    // cin>>tt;
    while (tt--) solve();
    return 0;
}