{-# LANGUAGE OverloadedStrings #-}
import Data.Char
import Data.List
import Data.Maybe
import Control.Monad
import qualified Data.ByteString.Char8 as BS

main :: IO ()
main = do
  [n,m] <- parseLine <$> BS.getLine
  xs <- replicateM m (toTuple.parseLine <$> BS.getLine)
  BS.putStrLn . BS.pack . show $ fromMaybe (-1) (solve n xs) 
  where
     parseLine = unfoldr (BS.readInt . BS.dropWhile isSpace)

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

solve :: Int -> [(Int,Int)] -> Maybe Int
solve n xs = if   check ds
  then generate n . map (\x->(fst x, (head.snd) x)) $ ds
  else Nothing
  where
    ds = map (\x -> (fst.head $ x, map snd x)) . groupBy (\a b-> (fst a) == (fst b)) . sortOn fst $ xs
    check xs' = and . map (\f -> f xs') $ [checkUnique, checkHeadDigit]
    checkUnique = and . map ((==1) . length . nub . snd)
    checkHeadDigit ds = 
      (n==1) || isNothing (lookup 1 ds) || ((0 `notElem`) . fromJust . lookup 1 $ ds)

generate :: Int -> [(Int,Int)] -> Maybe Int
generate _ [] = Nothing
generate n xs = Just . read . concat . map (show . genDigit) $ [1..n]
  where
    genDigit 1 = fromMaybe 1 (lookup 1 xs)
    genDigit d = fromMaybe 0 (lookup d xs)