#define _CRT_SECURE_NO_WARNINGS
//#include <iostream>
#include <cstdio>
//#include <cstring>
#include <cstdint>
#include <cstdlib>
#include <algorithm>
#include <cmath>
//#include <numeric>
//#include <string> 
#include <vector>
//#include <array>
//#include <unordered_map>
//#include <map>
//#include <queue>
//#include <set>
//#include <stack>
//#include <string>

using u = unsigned;

constexpr u N = (u)1e5;

struct p {
    p(u m) : d(m), c(m, 0) {}
    std::vector<u> d;
    std::vector<u> c;
    std::vector<u> compl_vals;
    void calc() {
        for(auto v : d) c[v-1]++;
        for(size_t i = 0; i < c.size(); i++) if(!c[i]) compl_vals.push_back(i+1);
    }
};

int main() {
    //freopen("input.txt","r",stdin);
    u n, m;
    scanf("%u%u", &n, &m);
    u vx = 0, vy = n;
    std::vector<p> s(n+1, {m});
    std::vector<std::pair<u, u>> h;
    for(u i = 0; i < n; i++) {
        for(u j = 0; j < m; j++) scanf("%u", &s[i].d[j]);
        s[i].calc();
        if(!s[i].compl_vals.empty() && vy == n) {
            for(u j = 0; j < m; j++) if(s[i].c[s[i].d[j]-1] > 1) {
                s[n].d[0] = s[i].d[j];
                s[n].c[s[i].d[j]-1]--; 
                s[i].d[j] = 0;
                vx = j;
                vy = i;
                h.push_back({vy * m + vx + 1, n*m+1});
                break;
            }
        }
    }

    std::vector<std::pair<u, u>> t(n);
    while(!s[vy].compl_vals.empty()) {
        for(u i = 0; i < n; i++) t[i] = {i, (u)s[i].compl_vals.size()};
        std::sort(t.begin(), t.end(), [](const auto& a, const auto& b){
            return a.second > b.second;
        });

        auto v = std::find_if(t.begin(), t.end(), [s, vy](const auto& v){
            return s[v.first].c[s[vy].compl_vals.back()-1] > 1;
        });
        u vi = v->first;
        s[vi].c[s[vy].compl_vals.back()-1]--;
        auto q = std::find(s[vi].d.begin(), s[vi].d.end(), s[vy].compl_vals.back());
        u qi = u(q - s[vi].d.begin());
        
        s[vy].d[vx] = s[vy].compl_vals.back();
        s[vy].c[s[vy].compl_vals.back()-1]++;

        s[vy].compl_vals.pop_back();

        h.push_back({vi * m + qi + 1, vy * m + vx + 1});

        vy = vi;
        vx = qi;
        s[vy].d[vx] = 0;


    }
    s[vy].d[vx] = s[n].d[0];
    h.push_back({n*m+1, vy * m + vx + 1});
    
    //for(u i = 0; i < n; i++) {
    //    for(u j = 0; j < m; j++) printf("%u ", s[i].d[j]);
    //    putchar('\n');
    //}
    

    printf("%u\n", (u)h.size());
    for(auto [a, b] : h) printf("%u %u\n", a, b);
}