a = input().split()
N = int(a[0])
M = int(a[1])
sc = []

for r in range(M):
  sc += input().split()

insect = [int(i) for i in sc]
ans = [10, 10, 10]

for r in range(M):
  if(ans[insect[2*r]-1] == 10):
    ans[insect[2*r]-1] = insect[2*r+1]
  elif(ans[insect[2*r]-1] != insect[2*r+1]):
    ans[insect[2*r]-1] = 11
    
if(ans[0] == 11 or ans[1]==11 or ans[2]==11):
  print("-1")
elif ans[0] == 0 and N == 3 or ans[0] != 0 and N < 3 or ans[1] == 0 and N ==2 or ans[1] != 0 and N < 2:
  print("-1")
else:
  for i in range (3):
    if(ans[i] == 10):
      if(3-i == N):
        ans[i]=1
      else:
        ans[i]=0
  print(100*ans[0] + 10*ans[1] + ans[2])
