// voi22-1_D.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <cmath>
#include <climits>
#include <unordered_map>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
	vector<vector<int>> sell = vector<vector<int>>(m);
	vector<vector<int>> w_buy = vector<vector<int>>(n);
	vector<int> nums;
	bool fselled = false;
	int fsell;
	for (int i = 0; i < n; i++)
	{
		vector<vector<int>> els = vector<vector<int>>(m);
		for (int j = 0; j < m; j++)
		{
			int inp;
			cin >> inp;
			nums.push_back(inp);
			inp--;
			els[inp].push_back(i * m + j);
		}
		for (int j = 0; j < m; j++)
		{
			if (els[j].size() == 0) w_buy[i].push_back(j);
			else
			{
				while (els[j].size() != 1)
				{
					sell[j].push_back(els[j][els[j].size()-1]);
					if (!fselled) 
					{
						fsell = j; fselled = true;
					}
					els[j].pop_back();
				}
			}

		}
	}
	nums.push_back(0);
	if (!fselled)
	{
		cout << "0";
		return 0;
	}
	vector<pair<int, int>> ans;
	int free_place = n * m;
	bool atf = true;
	while (true)
	{
		int from;
		int mp = free_place / m;
		if (mp == n) mp--;
		if (w_buy[mp].size() != 0)
		{
			int lastw = w_buy[mp].size() - 1;
			from = sell[w_buy[mp][lastw]][sell[w_buy[mp][lastw]].size()-1];
			sell[w_buy[mp][lastw]].pop_back();
			w_buy[mp].pop_back();
		}
		else
		{
			if (atf)
			{
				from = sell[fsell][sell[fsell].size() - 1];
				sell[fsell].pop_back();
				sell[fsell].push_back(free_place);
			}
			else
			{
				break;
			}
		}
		ans.push_back(pair<int, int>(from, free_place));
		free_place = from;
		atf = false;
	}
	ans.push_back(pair<int, int>(30, free_place));
	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); i++)
	{
		cout << ans[i].first + 1 << " " << ans[i].second + 1 << "\n";
		swap(nums[ans[i].first], nums[ans[i].second]);
	}
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
