N, M = map(int,input().split()) 
P = [list(map(int,input().split())) for i in range(M)]
num = [0, 0, 0]
ans = 0
flag1 = 0
flag2 = 0
flag3 = 0
for i in range(M):
  if P[i][0] == 1:
    if num[0] != 0 and num[0] != P[i][1]:
      flag3 += 1
    num[0] = P[i][1]
  elif P[i][0] == 2:
    if num[1] != 0 and num[1] != P[i][1]:
      flag2 += 1
    num[1] = P[i][1]
  else :
    if num[2] != 0 and num[2] != P[i][1]:
      flag1 += 1
    num[2] = P[i][1]
ans += num[0] * 100 
ans += num[1] * 10 
ans += num[2]  
if ans == 0 or flag1 > 0 or flag2 > 0 or flag3 > 0:
  print(-1)
else:
  print(ans)

