import numpy as np

N, M = map(int, input().split())
# print(N)
# print(M)

li = [list(map(int, input().split())) for i in range(M)]
arr = np.array(li)
# print(arr)
# print(arr[0, 1])
ans = [0, 0, 0]
if M == 1 and arr[0, 1] == 0:
    print(-1)
    exit()
else:
    for i in range(M):
        if i == 0:
            if arr[i, 0] == 1:
                ans[0] = arr[i, 1]
                # print(ans)
            elif arr[i, 0] == 2:
                ans[1] = arr[i, 1]
                # print(ans)
            elif arr[i, 0] == 3:
                ans[2] = arr[i, 1]
                # print(ans)
        else:
            if arr[i, 0] == 1:
                if ans[2] != 0:
                    if ans[0] != arr[i, 1]:
                        if i == M-1:
                            print(-1)
                            exit()
                else:
                    ans[0] = arr[i, 1]
                    if i == M - 1:
                        # print(ans)
                        break

            elif arr[i, 0] == 2 :
                if ans[1] != 0:
                    if ans[1] != arr[i, 1]:
                        if i == M - 1:
                            print(-1)
                            exit()
                else:
                    ans[1] = arr[i, 1]
                    if i == M - 1:
                        # print(ans)
                        break

            elif arr[i, 0] == 3 :
                if ans[2] != 0:
                    if ans[2] != arr[i, 1]:
                        if i == M - 1:
                            print(-1)
                            exit()
                else:
                    ans[2] = arr[i, 1]
                    if i == M - 1:
                        # print(ans)
                        break
# print(ans)
print(str(ans[0]) + str(ans[1]) +str(ans[2]))
