N, M = gets.split.map(&:to_i)
guess = [[], [], []]
M.times do
  s, c = gets.split.map(&:to_i)
  guess[s - 1] << c
end

g = guess.map {|c|
  c.uniq!
  if c.length == 0
    '*'
  elsif c.length == 1
    c.first.to_s
  else
    puts -1
    exit
  end
}

if g[3 - N] == '*'
  if 3 - N > 1
    g[3 - N] = 1
  else
    g[3 - N] = 0
  end
end

answer = g.join.tr('*', '0').to_i

if answer.to_s.length == N
  puts answer.to_i
else
  puts -1
end
