n, m = map(int, input().split())
c = list(map(int, input().split()))
c = [c[m*i:m*(i+1)] for i in range(n)]
d = [[0]*m for i in range(n)]
for i in range(n):
    for j in range(m):
        d[i][c[i][j]-1] += 1
# print(*d, sep = '\n')

vil = n*m
vid = []

for i in range(n):
    if max(d[i]) > 1:
        ind = d[i].index(max(d[i]))
        vid += [[i*m + ind, vil]]
        vil = i*m + ind
        d[i][ind] -= 1
#         d
        for j in range(m):
            if d[i][j] == 0:
                ind = j
                break
        break

while True:
    b = 1
    for i in range(n):
        if d[i][ind] > 1:
            y = i
            b = 0
            break
    if b:
        break
    vid += [[y*m + ind, vil]]
    d[vil//m][ind] += 1
#     d
    vil = y*m + ind
    d[y][ind] -= 1
#     d
    for j in range(m):
        if d[y][j] == 0:
            ind = j
            break
    
vid += [[n*m, vil]]
d[vil//m][vil%m] += 1

# print()
# print(*d, sep = '\n')

print(len(vid))
for i in vid:
    print(i[0]+1, i[1]+1)