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())

a=0
for i in range(M-1):
  for j in range(i+1,M):
    if s[i]==s[j] and c[i]!=c[j]:
      a+=1
if a==0:
  if N>=len(list(set(c))) and N>=2:
    
    u_=[0,0,0]
    for i in range(len(s)):
      u_[s[i]-1]=c[i]
     
    if u_[0]==0 and N==3:
      print(-1)
    elif u_[0:2]==[0,0] and N==2:
      print(-1)
    else:
      print(u_[0]*10**2+u_[1]*10+u_[2])
  elif N==1 and len(list(set(c)))==1:
    print(c[0])
  else:
    print(-1)
else:
  print(-1)