digit_number, requirements_count = gets.chomp.split(' ').map(&:to_i)

requirements = []
requirements_count.times do
  requirements << gets.chomp.split(' ').map(&:to_i)
end

f_digit_num = 0
s_digit_num = 0
t_digit_num = 0

f_flag = false

requirements.each do |req|
  if req[0] == 1
    if f_digit_num != 0  && f_digit_num != req[1] 
      f_flag = true
      break
    else
      f_digit_num = req[1]
    end
  elsif req[0] == 2
    if s_digit_num != 0  && s_digit_num != req[1] 
      f_flag = true
      break
    else
      s_digit_num = req[1]
    end
  elsif req[0] == 3
    if t_digit_num != 0  && t_digit_num != req[1] 
      f_flag = true
      break
    else
      t_digit_num = req[1]
    end
  end
end

if f_flag || "#{f_digit_num}#{s_digit_num}#{t_digit_num}".to_i.zero?
  puts -1
else
  puts "#{f_digit_num}#{s_digit_num}#{t_digit_num}"
end
