n,m = gets.chomp.split(" ").map(&:to_i)
cond1 = Array.new
m.times do 
  cond1 << gets.chomp.split(" ").map(&:to_i)
end
condk1 = cond1.select{|elem| elem[0] ==1}.uniq.map{|elem| elem[1]}
condk2 = cond1.select{|elem| elem[0] ==2}.uniq.map{|elem| elem[1]}
condk3 = cond1.select{|elem| elem[0] ==3}.uniq.map{|elem| elem[1]}

ng = false
ng = true if condk1.include?(0)

condk1 = [0] if condk1 ==[]
condk2 = [0] if condk2 ==[]
condk3 = [0] if condk3 ==[]

if ng
  puts -1
else
if n==1
  if condk1.length == 1 and condk1[0] !=0
    puts condk1[0]
  else
    puts -1
  end
end

if n==2
  if (condk1.length ==1 and condk2.length==1 and condk1[0] !=0 )
    if condk1[0] == 0
     puts 10 + condk2[0]
    else
     puts 10*condk1[0] + condk2[0]
    end
  else
    puts -1
  end
end

if n ==3
  if (condk1.length ==1 and condk2.length==1 and condk3.length ==1 and condk1[0] !=0)
    if condk1[0] ==0
      puts 100 + 10*condk2[0] + condk3[0]
    else
      puts 100*condk1[0] + 10*condk2[0] + condk3[0]
    end
  else
    puts -1
  end
end
end
