import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  (n : m : _) <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  scs <- sequence . replicate m $ unsafeListToTuple . map unsafeTextToInt . T.words <$> T.getLine :: IO [(Int, Int)]
  let
    cs :: [[Int]]
    cs | n == 1    = sequence [[0..9]]
       | otherwise = sequence $ [1..9] : replicate (pred n) [0..9]
    p :: [Int] -> Bool
    p xs = all (\(s, c) -> xs !! pred s == c) scs
    rs :: [[Int]]
    rs = filter p cs
  putStrLn $ if null rs then "-1" else map (toEnum . (48 +)) (head rs)

unsafeListToTuple :: [Int] -> (Int, Int)
unsafeListToTuple (x : y : _) = (x, y)

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
