X = input().split()
N = int(X[0])
M = int(X[1])
sc = []
 
 #リストscの[偶数]には桁、[奇数]には数字
for r in range(M):
  sc += input().split()

#scをint化
insect = [int(i) for i in sc]

ans = [10, 10, 10]


for r in range(M):
  if(ans[insect[2*r]-1] == 10):
    ans[insect[2*r]-1] = insect[2*r+1]
	#初期値10のままなら書き換える
	
  elif(ans[insect[2*r]-1] != insect[2*r+1]):
    ans[insect[2*r]-1] = 11
	#初期値10じゃない状態でかつすでに違う数字があったらそこは11に書き換える

if(N<3 and ans[2]!=10):
	ans[2] = 11
if(N<2 and ans[1] !=10):
	ans[1] = 11
if(N>=2 and ans[0]==0):
	ans[0] = 11

for r in range(3):
	if(ans[r] == 10):
		ans[r] = 0


if(ans[0] == 11 or ans[1] ==11 or ans[2] == 11):
	print("-1")

else:
	j = 1000
	if(N==1):
		k = ans[0]
		for i in range (10):
			if(k == 9-i):
				j = 9-i
	
	
	elif(N==2 and ans[0] != 0):
		k = 10*ans[0]+ans[1]
		for i in range (100):
			if(k == 99-i):
				j = 99-i
					
	else:
		k = 100*ans[0]+10*ans[1]+ans[2]
		for i in range(1000):
			if(k == 999-i):
				j = 999-i
	print(j)

