n, m = gets.chomp.split.map(&:to_i)
s = []
ans = -1
tmp = [0, 0, 0]
flag = true
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
    if (n == 2 && x.to_i == 1) then
      flag = false
    end
    if (n == 1 && x.to_i == 1) then
      flag = false
    end
    if (n == 1 && x.to_i == 2) then
      flag = false
    end
  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[1] != 0) then
    ans = tmp[1].to_s + tmp[2].to_s
  end
end

if (n == 1) then
  ans = tmp[2].to_s
end
if (!flag) then
  ans = -1
end
puts ans
