n, m = gets.chomp.split.map(&:to_i)
s = []
ans = -1
tmp = [0, 0, 0]
m.times do
  s.push(gets.chomp.split)
end

s.each do |ss|
  x = ss[0]
  ss.each do |sss|
    tmp[x.to_i - 1] = sss.to_i
  end
end

if (n == 3) then
  if (tmp[0] != 0) then
    ans = tmp[0].to_s + tmp[1].to_s + tmp[2].to_s
  end
end
if (n == 2) then
  if (tmp[0] != 0 && tmp[1] != 0) then
    ans = tmp[1].to_s + tmp[2].to_s
  end
end
if (n == 1) then
  if (tmp[0] != 0 && tmp[1] != 0) then
    ans = tmp[2].to_s
  end
end
puts ans
