a = list(map(int, input().split()))
N = a[0]
M = a[1]

Array = [list(map(int, input().split())) for _ in range(M)]

i = 0
hya = 0
jyu = 0
ich = 0
while i < 3:
    if Array[i][0] == 1:
        hya = Array[i][1]
    elif Array[i][0] == 2:
        jyu = Array[i][1]
    elif Array[i][0] == 3:
        ich = Array[i][1]
    i += 1

for i in range(M):
    for j in range(i+1, M):
        if Array[i][0] == Array[j][0]:
            if Array[i][1] != Array[j][1]:
                print(-1)
            else:
                print(hya, jyu, ich, sep="")