import Control.Monad (replicateM)
import Data.Char (intToDigit)

f :: Int -> [[Int]] -> Int -> Int
f n sc x 
    | length strX > n = -1
    | and [strX !! (s - 1) == intToDigit c | [s, c] <- sc] = x
    | otherwise = f n sc (x + 1)
  where strX = show x

main = do
    [n, m] <- map read . words <$> getLine :: IO [Int]
    sc <- replicateM m (map read . words <$> getLine) :: IO [[Int]]
    print $ f n sc (if n == 1 then 0 else 10 ^ (n - 1))
