import qualified Data.Map as MP
import Data.Char
import Data.List
import Control.Monad

toTuple [a,b] = (a,b)

parse :: Int -> [Int]
parse x = map digitToInt $ show x

solve :: Int -> [(Int, Int)] -> Int -> Int
solve n xs i
    | i > 1000 = -1
    | (length p) /= n = solve n xs (i+1)
    | not search = solve n xs (i+1)
    | otherwise = i
    where
        p = parse i 
        search = foldl' (\b sc -> step p b sc) True xs
        step p b (s,c) = b && (p !! (s-1) == c)

main = do
   [n,m] <- map read . words <$> getLine :: IO [Int]
   xs <- replicateM m (toTuple . map read . words <$> getLine) :: IO [(Int,Int)]
   print $ solve n xs 0
    