import numpy as np
import sys
list = [int(i) for i in input().split()]
a=list[0]
b=list[1]
 
tablec = np.array([[int(i) for i in input().split()] for b in range(b)])

i=0
x=0
y=0
z=0
for i in range(b):
	if tablec[i,0]==1:
		if x==0:
			x=tablec[i,1]
		elif x!=tablec[i,1]:
			x=-1
	if tablec[i,0]==2:
		if y==0:
			y=tablec[i,1]
		elif y!=tablec[i,1]:
			y=-1
	if tablec[i,0]==3:
		if z==0:
			z=tablec[i,1]
		elif z!=tablec[i,1]:
			z=-1
if x==0 and a==3:
  x=1
if y==0 and a==2:
  y=1
if z==0 and a==1:
  z=1
            
if x==-1 or y==-1 or z==-1:
	print(-1)
elif z==0 and a<1:
	print(-1)
elif y==0 and a<2:
	print(-1)
elif x==0 and a<3:
	print(-1)
else:
	print (x*100+y*10+z)