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

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

result = [0, 0, 0]

f_flag = false

requirements.each do |req|
  if req[0] == 1
    if (digit_number >= 2) && req[1] == 0 
      f_flag = true
      break
    elsif  result[0] != 0  && result[0] != req[1] 
      f_flag = true
      break
    else
      result[0] = req[1]
    end
  elsif req[0] == 2
    if (digit_number >= 2) && req[1] == 0 
      f_flag = true
      break
    elsif  result[1] != 0  && result[1] != req[1] 
      f_flag = true
      break
    else
      result[1] = req[1]
    end
  elsif req[0] == 3
    if result[2] != 0  && result[2] != req[1] 
      f_flag = true
      break
    else
      result[2] = req[1]
    end
  end
end

if f_flag
  puts -1
else
  puts "#{result[0]}#{result[1]}#{result[2]}".to_i
end
