from collections import defaultdict

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

d = defaultdict(set)
for s, c in SC:
    d[s].add(c)

if any(len(v) > 1 for v in d.values()):
    print(-1)
else:
    ans = ""
    for i in range(1, 4):
        if d[i]:
            ans += str(d[i].pop())
        else:
            ans += "0"
        
    print(int(ans) if int(ans) > 0 else -1)
