n , m = map(int,input().split())
x = [0]
y = [0]
z = [0]

for i in range(m):
    s , c = map(int,input().split())
    if s ==1:
        x.append(c)
    elif s==2:
        y.append(c)
    elif s == 3:
        z.append(c)

if len(set(x))==1:
    print(-1)
elif len(set(x)) >= 3 or len(set(y)) >= 3 or len(set(z)) >=3:
    print(-1)
else: 
    print(max(x)*100 + max(y)*10 + max(z))
    