n, m = (int(x) for x in input().split())
s, c = [], []

for i in range(m):
  _s, _c = (int(x) for x in input().split())
  s.append(_s)
  c.append(_c)

ans = ''
if n == 1:
  c_list = sorted(set([c[i] for i, _s in enumerate(s) if _s == 1]))
  if len(c_list) == 0:
    print(-1)
  else:
    print(c_list[0])
else:
  c_list = sorted(set([c[i] for i, _s in enumerate(s) if _s == 1]))
  if len(c_list) == 0 or (len(c_list) == 1 and c_list[0] == 0):
    print(-1)
    exit()
  elif c_list[0] == 0:
    ans += str(c_list[1])
  else:
    ans += str(c_list[0])
               
  c_list = sorted(set([c[i] for i, _s in enumerate(s) if _s == 2]))
  if len(c_list) == 0:
    ans += '0'
  else:
    ans += str(c_list[0])

  if n == 3:
    c_list = sorted(set([c[i] for i, _s in enumerate(s) if _s == 3]))
    if len(c_list) == 0:
      ans += '0'
    else:
      ans += str(c_list[0])

  if len(ans) == n:
    print(int(ans))
  else:
    print(-1)