N, M = map(int, input().split())
s = []
c = []
a = [10, 10, 10]

for i in range(M):
    s1,c1=[int(i) for i in input().split()]
    s.append(s1)
    c.append(c1)
    
if N not in s:
  print(-1)
else:
  for i in range(M):
    if c[i] < a[s[i] - 1]:
      a[s[i] - 1] = c[i]
      
  for i in range(3):
    if a[i] == 10:
      a[i] = 0
  
  if a[3 - N] == 0:
    print(-1)
  else:
    ans = a[0] * 100 + a[1] * 10 + a[2]
    print(ans)