N,M = map(int, input().split())
s = [0] * M
c = [0] * M
for i in range(M):
    s[i], c[i]= map(int, input().split())

kekka=0
s1=0
s2=0
s3=0
def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]
for j in range(1,4):
    r=func1(s,j)
    x=0
    for i in r:
      if x==0:
        hikaku=c[i]
        x=x+1
      else:
        if c[i]!=hikaku:
          kekka=-1


if kekka !=-1:
  a=func1(s,1)
  if len(a)!=0:
    if (c[a[0]]==0)&(N!=1):
      kekka=-1


if kekka !=-1:
  a1=func1(s,1)
  a2=func1(s,2)
  a3=func1(s,3)
  if len(a1)!=0:
    s1=c[a1[0]]
  else:
    s1=1
  if len(a2)!=0:
    s2=c[a2[0]]
  else:
    s2=0
  if len(a3)!=0:
    s3=c[a3[0]]
  else:
    s3=0
  if N==3:
    kekka=s1*100+s2*10+s3
  elif N==2:
    kekka=s1*10+s2
  else:
    kekka=s1
if M==0:
  kekka=-1
    
print(kekka)

  
  
 

