	#include <algorithm>
	#include <iostream>
	#include <cstdlib>
	#include <cstdarg>
	#include <cassert>
	#include <climits>
	#include <cstring>
	#include <complex>
	#include <cstdio>
	#include <vector>
	#include <string>
	#include <queue>
	#include <cmath>
	#include <ctime>
	#include <set>
	#include <map>
	#include <iomanip>
	#include <unordered_map>
	#include <bitset>
	#include<stack>
	#include <type_traits>
	#include <random>
	#include <chrono>
	#define int long long
	using namespace std;
	long long a, b, c, d, e, f, g;
	long long h[4000005];
	long long h2[10000005];
	long long pos[1000005];
	struct lol
	{
		int x, y;
	};
	vector<int> v[500005];
	vector<int> man[500005];
	vector<int> low[500005];
	vector<int> hight[500006];
	
	void solve()
	{
		cin >> a >> b;
		f = 1;
		v[f].push_back(0);
		for (int i = 1; i <= a*b; i++)
		{
			cin >> c;
			h2[i] = c;

			v[f].push_back(c);
			if (i % b == 0) {
				f++; v[f].push_back(0);
			}
		}
		for (int i = 1; i <= a; i++)
		{
			for (int j = 1; j <= b; j++)
			{
				h[j] = 0;
			}
			for (int j = 1; j <= b; j++)
			{
				if (h[v[i][j]] != 0)
				{
				
					man[v[i][j]].push_back((i - 1) * b + j);
					hight[i].push_back(j);
				}
				h[v[i][j]] = 1;
			}
		//cout << "N";
			for (int j = 1; j <= b; j++)
			{

				if (h[j] == 0) low[i].push_back(j);
			}
		}
		e = -1;
		
		vector<lol> ans;
		//cout << "N";
		int k = a % 2 + 1;
		for (int i = k; i <= a; i++)
		{
			if (low[i].size() != 0)
			{
				int pos =(i-1)*b+hight[i][hight[i].size() - 1];
				for (int j = 0; j < man[h2[pos]].size(); j++)
				{
					if (man[h2[pos]][j] == pos) {
						man[h2[pos]][j] = a * b + 1;
						swap(man[h2[pos]][j], man[h2[pos]][0]); break;
					}
				}
				ans.push_back({ pos,a * b + 1 });
				hight[i].pop_back();
				e = low[i][low[i].size()-1];
				low[i].pop_back();
				int e2 = pos;
				while (1 != 0)
				{
					ans.push_back({ man[e][man[e].size() - 1],e2 });
					
					e2 = man[e][man[e].size() - 1];
					man[e].pop_back();
					if (e2 == a * b + 1) break;
					int rr = e2 / b;
					if (e2 % b != 0) rr++;
					e = low[rr][low[rr].size() - 1];
					low[rr].pop_back();
					for (int j = 0; j < hight[rr].size(); j++)
					{
						int to = hight[rr][j];
						to = to + (rr - 1) * b;
						if (to == e2)
						{
							swap(hight[rr][j], hight[rr][hight[rr].size() - 1]);
							hight[rr].pop_back();
							break;
						}
					}
				}
				i--;
			}
		}
		for (int i = 1; i < k; i++)
		{
			if (low[i].size() != 0)
			{
				int pos = (i - 1) * b + hight[i][hight[i].size() - 1];
				for (int j = 0; j < man[h2[pos]].size(); j++)
				{
					if (man[h2[pos]][j] == pos) {
						man[h2[pos]][j] = a * b + 1;
						swap(man[h2[pos]][j], man[h2[pos]][0]); break;
					}
				}
				ans.push_back({ pos,a * b + 1 });
				hight[i].pop_back();
				e = low[i][low[i].size() - 1];
				low[i].pop_back();
				int e2 = pos;
				while (1 != 0)
				{
					ans.push_back({ man[e][man[e].size() - 1],e2 });

					e2 = man[e][man[e].size() - 1];
					man[e].pop_back();
					if (e2 == a * b + 1) break;
					int rr = e2 / b;
					if (e2 % b != 0) rr++;
					e = low[rr][low[rr].size() - 1];
					low[rr].pop_back();
					for (int j = 0; j < hight[rr].size(); j++)
					{
						int to = hight[rr][j];
						to = to + (rr - 1) * b;
						if (to == e2)
						{
							swap(hight[rr][j], hight[rr][hight[rr].size() - 1]);
							hight[rr].pop_back();
							break;
						}
					}
				}
				i--;
			}
		}
		/*
		 4 2 4 5 1 3 1 1 2 2 3 5 2 1 4 5 3 3 4 1 5 4 3 5 2
		
		*/
		cout << ans.size() << '\n';
		for (int i = 0; i < ans.size(); i++)
		{
			swap(h2[ans[i].x], h2[ans[i].y]);
			cout << ans[i].x << " "<<ans[i].y<<'\n';
		}
		f = 1;
		for (int i = 1; i <= b; i++) h[i] = 0;
		for (int i = 1; i <= a * b; i++)
		{
		//	cout << h2[i] << " ";
			h[h2[i]] = 1;
			if (i % b == 0)
			{

				f++;
				for (int j = 1; j <= b; j++)
				{
					if (h[j] == 0) { cout << "ALARM!!!!\n"; while (1 != 0) { cout << "ERORR 404"; } }
					h[j] = 0;
				}
			}
		}
		
	}
	/*
	6
	3 3 3 3 4 5
	*/
	signed main() {
		int q = 1;
		//cin >> q;
		while (q != 0)
		{
			solve();
			q--;
		}
	}

	/*
	4 4
1 1 3 4 2 2 3 4 1 2 3 3 1 2 4 4
	
	*/