import Control.Monad
import Data.Char

main = do
  li <- getLine
  let [n,m] = map read $ words li
  scs <- forM [1..m] (\_ -> do
    li <- getLine
    let [s,c] = map read $ words li
    return (s,c)
    )
  let ans = compute n scs
  print ans

compute :: Int -> [(Int,Int)] -> Int
compute n scs = head (cans ++ [-1])
  where
    ds 1 = [0..9]
    ds 2 = [10..99]
    ds 3 = [100..999]
    cans = [ x | x <- ds n, check x scs ]

check x scs = all p scs
  where
    ds = map digitToInt $ show x
    p (s,c) = ds !! pred s == c