n,m=map(int,input().split())
l={1:[],2:[],3:[]}

for _ in range(m):
  i,j=map(int,input().split())
  l[i].append(j)


if n != 1 and len(l[n]) == 0:
  print('-1')
  exit()
  
for i in range(1,4):
  if len(l[i]) == 0:
    l[i].append(0)
    
a1,a2,a3=min(l[1]),min(l[2]),min(l[3])

if n == 1:
  print(a1)
elif n == 2:
  print(''.join(list(map(str, [a1,a2]))))
else:
  print(''.join(list(map(str, [a1,a2,a3]))))
