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)])

x=0
y=0
z=0
t=-1
k=0
p=0
for i in range(1000):
	if t!=-1:
		exit
	else:
		t=0
		x= i // 100
		y= (i//10) % 10
		z=i % 10
		listb=[x,y,z]
		for n in range(b):
			k= tablec[n,1]
			p= listb[n]
			if k != p:
				t=-1
			if t==0:
				if not(x==0 and a==3):
					if not(y==0 and a==2):
						if not(z==0 and a==1):
							t=i
print (t)