import sys;input=sys.stdin.readline
from collections import Counter
n,m=map(int,input().split())
l=[]
a=[*map(int,input().split())]
for i in range(0,n*m,m):
    l.append(a[i:i+m])


ost=-1
F=0
last=-1
for i in range(n):
    if F==1:break
    d=Counter(l[i])
    for j in d:
        if d[j]>1:
            for k in range(m):
                if j==l[i][k]:ost=[j,k+i*m+1];last=k+i*m+1;l[i][k]=0;break
            F=1;break


mas={}

for i in range(n):
    d=Counter(l[i])
    for j in d:
        if d[j]>1:
            mas.setdefault(j,[])
            for k in range(l[i].index(j)+1,m):
                if l[i][k]==j:
                    mas[l[i][k]].append(k+1+i*m)

                    
skip=-1
ans=[[ost[1],n*m+1]]
cc=0
for i in mas:
    cc+=len(mas[i])
cc+=1


for _ in range(cc):
    d=Counter(l[(last-1)//m])
    for i in range(1,m+1):
        if d[i]==0:
            try:
                g=mas[i].pop()
            except:
                ans.append([n*m+1,last])
                break
            ans.append([g,last])
            l[(last-1)//m].append(i)
            
            last=g             
            break
        
print(len(ans))
for i in ans:
    print(i[0],i[1])
        
        
    
