N, M = map(int, input().split())
index = 0
s_raw = []
c_raw = []
ans_raw = []
ans_100 = []
ans_10 = []
ans_1 = []

for i in range(0, M):
	s, c = map(int, input().split())
	s_raw.append(s)
	c_raw.append(c)

for i in range(0, M):
	if s_raw[i] == 1:
		ans_100.append(c_raw[i])
	elif s_raw[i] == 2:
		ans_10.append(c_raw[i])
	elif s_raw[i] == 3:
		ans_1.append(c_raw[i])

if len(ans_100) == 0:
	ans_100.append(0)
if len(ans_10) == 0:
	ans_10.append(0)	
if len(ans_1) == 0:
	ans_1.append(0)

ans_100.sort() 
ans_10.sort()
ans_1.sort()

ans = [ans_100, ans_10, ans_1]
#print(ans)
#print('=====================')

for i in range(0, 3):
	for j in range(0, len(ans[i])):
		#print(ans[i][j])
		if ans[i][0] != ans[i][j]:
			index = -1
	if N == i + 1:
		if ans[i][0] == 0:
			index = -1


ans_ans = ans[0][0] * 100 + ans[1][0] * 10 + ans[2][0]

if index == -1:
	print('-1')
else:
	print(str(ans_ans))