N, M, *sc = map(int, open(0).read().split())
s=[]
c=[]
for i in range(2*M):
  if i%2 == 0:
    s.append(sc[i])
  else:
    c.append(sc[i])

ans = 0
#ここから    
for j in range(M):
  if N == 2 and s[j] == 1 and c[j] == 0:
    ans = -1
    break
  elif N == 3 and s[j] == 1 and c[j] == 0:
    ans = -1
    break

for k in range(M):
  for l in range(M):
    if not k == l and s[k] == s[l] and c[k] != c[l]:
      ans = -1
      break
#ここまで解があるかどうかの判定

kotae = [0,0,0]
if ans == 0:
  for m in range(M):
    kotae[s[m]-1] = c[m]
  ans = kotae[0]*100 + kotae[1]*10 + kotae[2]
  
print(ans)