n,m= gets.split.map(&:to_i)
ans = 0
a = []
b = []
c = []
for num in 0..m-1 do
  s =gets.split.map(&:to_i)
  if s[0] == 1
    a << s[1]
  elsif s[0] == 2
    b << s[1]
  else
    c << s[1]
  end
end
al = a.length
bl = b.length
cl = c.length
if a != [] && a.count(a[0]) == al
  ans += a[0] * 100
end

if b != [] && b.count(b[0]) == bl
  ans += b[0] * 10
end

if c != [] && c.count(c[0]) == cl
  ans += c[0] 
end

if ans == 0
  p -1
else
  p ans
end