local n, m = io.read("*n", "*n")
local min, max = 0, 9
if n == 2 then
  min, max = 10, 99
elseif n == 3 then
  min, max = 100, 999
end
local s, c = {}, {}
for i = 1, m do
  local x, y = io.read("*n", "*n")
  s[i], c[i] = x, tostring(y)
end
local f = false
for ic = min, max do
  local str = tostring(ic)
  f = true
  for i = 1, m do
    if n < s[i] or str:sub(s[i], s[i]) ~= c[i] then
      f = false
      break
    end
  end
  if f then print(ic) break end
end
if not f then print(-1) end
