loop =0 #while nukeru yatu
kazu=0 # loop naide 1zutu hueru 


A = input().split() 
A[0]=int(A[0]) #ketasuu 
A[1]=int(A[1]) #joukennkaisuu 

if A[1] == 0: 
  print(-1)


if A[1] == 1:
  j = input().split()
  j[0]=int (j[0])# keta me no kazu ga
  j[1]=int (j[1])# no toki 
  if j[0]==1 : dig=100
  if j[0]==2 : dig=10
  if j[0]==3 : dig=1
  
  if (A[0]==3 and j[0]<=2 and j[1]==0) or (A[0]==2 and j[0]<=1 and j[1]==0):
    print(-1)
    
  else:
    while loop == 0:
      if kazu / dig == j[1]:
        loop = 1 
      kazu+=1
    print(kazu-1)
  
if A[1] == 2:
  j = input().split()
  j[0]=int (j[0])
  j[1]=int (j[1])
  if j[0]==1 : dig=100
  if j[0]==2 : dig=10
  if j[0]==3 : dig=1

  k = input().split()
  k[0]=int (k[0])
  k[1]=int (k[1])
  if k[0]==1 : dig2=100
  if k[0]==2 : dig2=10
  if k[0]==3 : dig2=1
    
  while loop == 0:
    if kazu / dig == j[1] and kazu / dig2 == k[1] :
      loop = 1 
    kazu+=1

    print(kazu-1)


