ccc = 0
eee = 0
ggg = 0
iii = 0
kkk = 0
error = 0

a,b = map(int, input().split())

if b == 0:
  print(0)
if b == 1:
  c,d = map(int, input().split())
  if a == 1 and d == 0:
    print(0)
  elif d == 0:
    print(-1)
  else:
    print(d)
if b == 2:
  c,d = map(int, input().split())
  e,f = map(int, input().split())
  if c == 1:
    ccc = 100
  elif c == 2:
    ccc = 10
  elif c == 3:
    ccc = 1
  if e == c and f == d:
    eee = 0
  elif e == c:
    error = 1
  elif e == 1:
    eee = 100
  elif e == 2:
    eee = 10
  elif e == 3:
    eee = 1
  if error:
    print(-1)
  elif a == 1 and (d == 0 or f == 0):
    print(0)
  elif (c == 1 and d == 0) or (e == 1 and f == 0):
    print(-1)
  else:
    print(d*ccc+f*eee)
if b == 3:
  c,d = map(int, input().split())
  e,f = map(int, input().split())
  g,h = map(int, input().split())
  if c == 1:
    ccc = 100
  elif c == 2:
    ccc = 10
  elif c == 3:
    ccc = 1
  if e == c and f == d:
    eee = 0
  elif e == c:
    error = 1
  elif e == 1:
    eee = 100
  elif e == 2:
    eee = 10
  elif e == 3:
    eee = 1
  if g == c and h == d:
    ggg = 0
  elif g == c:
    error = 1
  elif g == e and h == f:
    ggg = 0
  elif g == e:
    error = 1
  elif g == 1:
    ggg = 100
  elif g == 2:
    ggg = 10
  elif g == 3:
    ggg = 1
  if error:
    print(-1)
  elif a == 1 and (d == 0 or f == 0 or h == 0):
    print(0)
  elif (c == 1 and d == 0) or (e == 1 and f == 0) or (g == 1 and h == 0):
    print(-1)
  else:
    print(d*ccc+f*eee+h*ggg)
if b == 4:
  c,d = map(int, input().split())
  e,f = map(int, input().split())
  g,h = map(int, input().split())
  i,j = map(int, input().split())
  if c == 1:
    ccc = 100
  elif c == 2:
    ccc = 10
  elif c == 3:
    ccc = 1
  if e == c and f == d:
    eee = 0
  elif e == c:
    error = 1
  elif e == 1:
    eee = 100
  elif e == 2:
    eee = 10
  elif e == 3:
    eee = 1
  if g == c and h == d:
    ggg = 0
  elif g == c:
    error = 1
  elif g == e and h == f:
    ggg = 0
  elif g == e:
    error = 1
  elif g == 1:
    ggg = 100
  elif g == 2:
    ggg = 10
  elif g == 3:
    ggg = 1
  if i == c and j == d:
    iii = 0
  elif i == c:
    error = 1
  elif i == e and j == f:
    iii = 0
  elif i == e:
    error = 1
  elif i == g and j == h:
    iii = 0
  elif i == g:
    error = 1
  elif i == 1:
    iii = 100
  elif i == 2:
    iii = 10
  elif i == 3:
    iii = 1
  if error:
    print(-1)
  elif a == 1 and (d == 0 or f == 0 or h == 0 or j == 0):
    print(0)
  elif (c == 1 and d == 0) or (e == 1 and f == 0) or (g == 1 and h == 0) or (i == 1 and j == 0):
    print(-1)
  else:
    print(d*ccc+f*eee+h*ggg+j*iii)
if b == 5:
  c,d = map(int, input().split())
  e,f = map(int, input().split())
  g,h = map(int, input().split())
  i,j = map(int, input().split())
  k,l = map(int, input().split())
  if c == 1:
    ccc = 100
  elif c == 2:
    ccc = 10
  elif c == 3:
    ccc = 1
  if e == c and f == d:
    eee = 0
  elif e == c:
    error = 1
  elif e == 1:
    eee = 100
  elif e == 2:
    eee = 10
  elif e == 3:
    eee = 1
  if g == c and h == d:
    ggg = 0
  elif g == c:
    error = 1
  elif g == e and h == f:
    ggg = 0
  elif g == e:
    error = 1
  elif g == 1:
    ggg = 100
  elif g == 2:
    ggg = 10
  elif g == 3:
    ggg = 1
  if i == c and j == d:
    iii = 0
  elif i == c:
    error = 1
  elif i == e and j == f:
    iii = 0
  elif i == e:
    error = 1
  elif i == g and j == h:
    iii = 0
  elif i == g:
    error = 1
  elif i == 1:
    iii = 100
  elif i == 2:
    iii = 10
  elif i == 3:
    iii = 1
  if k == c and l == d:
    kkk = 0
  elif k == c:
    error = 1
  elif k == e and l == f:
    kkk = 0
  elif k == e:
    error = 1
  elif k == g and l == h:
    kkk = 0
  elif k == g:
    error = 1
  elif k == i and l == j:
    kkk = 0
  elif k == i:
    error = 1
  elif k == 1:
    kkk = 100
  elif k == 2:
    kkk = 10
  elif k == 3:
    kkk = 1
  if error:
    print(-1)
  elif a == 1 and (d == 0 or f == 0 or h == 0 or j == 0 or l == 0):
    print(0)
  elif (c == 1 and d == 0) or (e == 1 and f == 0) or (g == 1 and h == 0) or (i == 1 and j == 0) or (k == 1 and l == 0):
    print(-1)
  else:
    print(d*ccc+f*eee+h*ggg+j*iii+l*kkk)