// 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);
	bool fselled = false;
	int fsell;
	for (int i = 0; i < n; i++)
	{
		vector<int> els = vector<int>(m);
		for (int j = 0; j < m; j++)
		{
			int inp;
			cin >> inp;
			inp--;
			els[inp]++;
		}
		for (int j = 0; j < m; j++)
		{
			if (els[j] == 0) w_buy[i].push_back(j);
			else
			{
				while (els[j] != 1)
				{
					sell[j].push_back((i) * m + j);
					if (!fselled) 
					{
						fsell = j; fselled = true;
					}
					els[j]--;
				}
			}

		}
	}
	if (!fselled)
	{
		cout << "0";
		return 0;
	}
	vector<pair<int, int>> ans;
	int free_place = n * m;
	bool atf = true;
	while (true)
	{
		int from, to;
		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;
			}
		}
		to = free_place;
		free_place = from;
		ans.push_back(pair<int, int>(from, to));
		atf = false;
	}
	cout << ans.size() << "\n";
	for (int i = 0; i < ans.size(); i++)
	{
		cout << ans[i].first + 1 << " " << ans[i].second + 1 << "\n";
	}
}

// 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
