#
N, M = map(int,input().split())
A = [list(map(int,input().split())) for i in range(M)]
A.sort()

flag = True

for i in range(M-1):
    if (A[i][0]==A[i+1][0]) & (A[i][1]!=A[i+1][1]):
        flag = False

ans = [0]*3
for i in range(M):
    if A[i][0] == 1:
        ans[0] = A[i][1]
    elif A[i][0] == 2:
        ans[1] = A[i][1]
    elif A[i][0] == 3:
        ans[2] = A[i][1]


if (N == 3) & (ans[0] == 0):
    flag = False

if (N == 2) & (ans[1] == 0):
    flag = False

if flag:
    n = 3-N
    for i in range(n):
        ans.pop(0) #iの位置のアイテムをリストから削除し，このアイテムを返す
    for i in range(len(ans)):
       print(ans[i], end='')
else:
    print(-1)