N,M = map(int,input().split())
SC = [list(map(int, input().split())) for i in range(M)]
l = []
first = []
second = []
third = []
for i in range(M):
  for s,c in SC:
    if s[i] == 1:
      first.append(c[i])
    elif s[i] == 2:
      second.append(c[i])
    elif s[i] == 3:
      third.append(c[i])
if first == []:
  print(-1)
elif second == []:
  second.append(0)
elif third == []:
  third.append(0)
l.append(min(first))
l.append(min(second))
l.append(min(third))
l = ''.join(str(n) for n in l)
print(l)