n, m = map(int, input().split())
# input()
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:
        ch = d[i].index(max(d[i]))+1
        ind = c[i].index(ch)
        vid += [[i*m + ind, vil]]
        vil = i*m + ind
        d[i][ch-1] -= 1
        c[i][ind] = 0
#         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 + c[y].index(ind+1), vil]]
    d[vil//m][ind] += 1
#     d
    vil = y*m + c[y].index(ind+1)
    d[y][ind] -= 1
    c[y][c[y].index(ind+1)] = 0
#     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)