N,M=map(int,input().split())
x=-1
tf=0
if N==1:
  for i in range(0,10):
    for j in range(M):
      tf+=not str(i)==input()[2]
    if not tf:
      x=i
      break
else:
  for i in range(10**(n-1),10**n):
    for j in range(M):
      n=input()
      tf+=not str(i)[int(n[0])]==n[2]
    if not tf:
      x=i
      break
print(x)
