import numpy as np	
import sys
stdin = open(0)
N,M = (stdin.readline()).split()
N = int(N)
M = int(M)
sc = []
ans = np.array([-1,-1,-1])
for i in range(M):
  sc.append(stdin.readline().split())
sc = np.array(sc, dtype='int')

for i in range(M):
  for j in range(i+i,M):
    if ((sc[i,0] == sc[j,0]) & (sc[i,1] != sc[j,1])):
      print(-1)
      sys.exit()
for i in range(M):
  ans[sc[i,0]-1] = sc[i,1]
if ans[0] == 0:
  print(-1)
  sys.exit()
elif ans[1] == 0:
  print(-1)
  sys.exit()
for i in range(3):
  if ans[i]==-1:
    ans[i] = 0

print(ans[0]*100+ans[1]*10+ans[2])