import Data.Maybe
import Data.Char
import Data.List
import Control.Monad

main = do
  [n,m] <- map read.words <$> getLine
  sc <- replicateM m $ pair.map read.words <$> getLine
  print $ solve n sc

solve n sc = fromMaybe (-1) $ find f $ can n where
  f n = all g sc where
    g (s,c) = digitToInt(sn !! (s-1)) == c
    sn = show n
  can 1 = [0..9]
  can 2 = [10..99]
  can 3 = [100..999]

pair (a:b:_) = (a,b)
