#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
#define PII pair<int,int>
#define f first
#define s second
#define PB push_back
#define MIN(a,b) a=min(a,b)
#define MAX(a,b) a=max(a,b)
#define ALL(a) a.begin(),a.end()
#define rALL(a) a.rbegin(),a.rend()
#define V vector
#define PDI pair<double, int> 
#define Pll pair<ll,ll>
const int N=1e5;
const ll mod=1e9+7;
const double eps=1e-9;
const double PI=acos(-1);
#define INF 2000000000

int n,m;
int a[160000];
V<int> b[400];
V<int> c[400];

V<int> ans;

int main(){
	cin>>n>>m;
	for(int i=0;i<n*m;i++){
		cin>>a[i];
		a[i]--;
	}
	for(int i=0;i<n;i++){
		V<bool> b1(m);
		for(int j=i*m;j<(i+1)*m;j++)
			if(b1[a[j]])
				c[a[j]].PB(j);
			else
				b1[a[j]]=1;
		for(int j=0;j<m;j++)
			if(!b1[j])
				b[i].PB(j);
	}
	ans.PB(n*m);
	int k=0;
	while(1){
		int x=-1;
		for(;k<m;k++)
			if(c[k].size()>1){
				x=c[k][c[k].size()-1];
				c[k].pop_back();
				break;
			}
		if(x<0)
			break;
		ans.PB(x);
		while(1){
			int y=x/m;
			int b1=b[y][b[y].size()-1];
			b[y].pop_back();
			if(c[b1].size()==0){
				ans.PB(n*m);
				break;
			}
			x=c[b1][c[b1].size()-1];
			c[b1].pop_back();
			ans.PB(x);
		}
	}
	cout<<ans.size()-1<<"\n";
	for(int i=1;i<ans.size();i++)
		cout<<ans[i]+1<<' '<<ans[i-1]+1<<"\n";
}