N,M = map(int,input().split())
m = [[0],[0],[0]]
for i in range(M):
    s,c = map(int,input().split())
    if m[s-1] == [0]:
        m[s-1].pop(0)
        m[s-1].append(c)
    else:
        m[s-1].append(c)
# print(m)
ancer = min(m[0])*10**2+min(m[1])*10+min(m[2])

if ancer < 10**(N-1):
    print(-1)

else:
    print(ancer)