S = []
N,M = [int(i) for i in input().split(" ")]

for  _ in range(M):
    S.append([int(i) for i in input().split(" ")])


flag = False
ans = []
for i in range(1,999):
    for s in S:
        try:   
            if [int(str(i)[s[0]-1]) == s[1] for s in S].count(True) == N:
                ans.append(i)
                flag = True
        except:
            pass

if flag:
    print(min(ans))
else:
    print("-1")