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 n ==1:
    if len(set(x)) > 2:
        print('-1')
    else:
        print(x[0])
if n == 2 :
    if (len(set(x)) ==1 and x[-1] == 0) or len(set(x)) > 2 or len(set(y)) >2:
        print('-1') 
    else:
        print(max(x)*10+max(y))
if n ==3 :
    if (len(set(x))==1 and x[-1] ==0 ) or len(set(x)) > 2 or len(set(y)) > 2 or len(set(z)) > 2:
        print('-1')
    else:
        print(max(x)*100+ max(y)*10 + max(z))