{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TypeFamilies #-}
import           Data.Maybe
import           Data.Int
import           Data.List
import           Data.Char
  -- import qualified Data.Vector.Unboxing as VU
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.ByteString.Char8 as BS
import           Data.List (scanl')
import           Data.IORef
import           Control.Monad
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Control.Applicative

readInt = BS.readInt . BS.dropWhile isSpace
getVector f = VU.unfoldr f <$> BS.getLine
getIntVector = getVector readInt
getBSVector = getVector BS.uncons

solve n scs = loop scs list
  where
    list = case n of 1 -> [0..9]
                     2 -> [10..99]
                     _ -> [100..999]

    loop [] ns = if null ns then -1 else head ns
    loop ((s,c):xs) ns = loop xs (filter (check s c) ns)

check s c number = ns !! (s-1) == c
  where
    ns = dissNumber number

dissNumber :: Int -> [Int]
dissNumber x = loop x []
  where
    loop 0 list = list
    loop n list = loop (n `div` 10) (n `mod`10:list)

main = do
  (n,m) <- (\vec -> (vec VU.! 0, vec VU.! 1)) <$> getIntVector
  scs <-  VU.replicateM m $ (\vec -> (vec VU.! 0, vec VU.! 1)) <$> getIntVector
  print $ solve n $ VU.toList scs
