#include <cstdio>
#include <algorithm>
#include <random>
#include <cassert>
#include <vector>

std::mt19937 rng;
const int N = 400;
int n, m, cnt[N][N], a[N * N], p[N * N];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n * m; ++i) {
		scanf("%d", a + i);
		--a[i];
		++cnt[i / m][a[i]];
		p[i] = i;
	}
	std::vector<std::pair<int, int> > ans;
	std::shuffle(p, p + n * m, rng);
	for (int id = 0; id < n * m; ++id) {
		int i = p[id];
		if (cnt[i / m][a[i]] > 1) {
			std::vector<int> p;
			int j = i, cur = -1;
			while (true) {
				p.push_back(j);
				--cnt[j / m][a[j]];
				std::swap(a[j], cur);
				if (a[j] != -1) ++cnt[j / m][a[j]];
				int to;
				for (to = 0; to < n && (to == i / m || cnt[to][cur]); ++to);
				if (to == n) to = i / m;
				if (to == i / m) {
					for (j = to * m; j < (to + 1) * m && (a[j] == -1 || cnt[to][a[j]] == 1); ++j);
					if (j == (to + 1) * m) break;
				} else for (j = to * m; cnt[to][a[j]] == 1; ++j);
			}
			a[i] = cur;
			++cnt[i / m][a[i]];
			ans.push_back(std::make_pair(p.back() + 1, n * m + 1));
			for (int i = p.size() - 2; i >= 0; --i) ans.push_back(std::make_pair(p[i] + 1, p[i + 1] + 1));
			ans.push_back(std::make_pair(n * m + 1, p.front() + 1));
		}
	}
	for (int i = 0; i < n * m; ++i) assert(cnt[i / m][a[i]] == 1);
	printf("%d\n", (int)ans.size());
	for (int i = 0; i < (int)ans.size(); ++i) printf("%d %d\n", ans[i].first, ans[i].second);
	return 0;
}