n,m= gets.split.map(&:to_i)
ans = 0
a = []
b = []
c = []
for num in 0..m-1 do
  s = gets.split.map(&:to_i)
  if s[0] == 1
    a << s[1]
  elsif s[0] == 2
    b << s[1]
  else
    c << s[1]
  end
end

if a != []
  ans += a.sort[0] * 100
end

if b != []
  ans += b.sort[0] * 10
end

if c != []
  ans += c.sort[0]
end
 
if ans == 0 || ans.to_s.length != n
  p -1
else
  p ans
end