n,m = map(int,input().split())
s = []
c = []
while True:
  try:
    tmp = list(map(int,input().split()))
    s.append(tmp[0])
    c.append(tmp[1])
  except EOFError:
    break
if m == 0:
  print(0)
  exit()
  
if n ==1:
  for i in range(10):
    cnt = 0
    for _ in range(len(c)):
      if c[_]==i:
        cnt +=1
    if cnt == len(c):
      print(i)
if n==2:
  for i in range(10,100):
    cnt = 0
    for _ in range(len(c)):
      if s[_]==1:
      	if c[_]==i//10:
          cnt +=1
      else:
        if c[_]==i%10:
          cnt+=1
    if cnt == len(c):
      print(i)
      exit()
if n==3:
  for i in range(100,1000):
    cnt = 0
    for _ in range(len(c)):
      if s[_]==1:
      	if c[_]==i//100:
          cnt +=1
      elif s[_]==2:
      	if c[_]==(i%100)//10:
          cnt +=1
      elif s[_]==3:
        if c[_]==(i%10):
          cnt+=1
    if cnt == len(c):
      print(i)
      exit()
print("-1")
  
      
