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

l = ans.to_s.length

if n == 1 && l == 1  && a.count(a[0]) == al && b.count(b[0]) == bl  && c.count(c[0]) == cl
  p ans
elsif n == 2 && l == 2 && a.count(a[0]) == al && b.count(b[0]) == bl  && c.count(c[0]) == cl 
  p ans
elsif n == 2 && l == 1 && a.count(a[0]) == al && b.count(b[0]) == bl  && c.count(c[0]) == cl
  p ans + 10
elsif n == 3 && l == 3 && a.count(a[0]) == al && b.count(b[0]) == bl  && c.count(c[0]) == cl
  p ans
elsif n == 3 && l == 2 && a.count(a[0]) == al && b.count(b[0]) == bl  && c.count(c[0]) == cl
  p ans + 100
elsif n == 3 && l == 1 && a.count(a[0]) == al && b.count(b[0]) == bl  && c.count(c[0]) == cl
  p ans + 110
else
  p -1
end