import qualified Data.IntMap as M
import Data.Maybe
import Data.List

concatIntList :: Integral a => [a] -> a
concatIntList xs = go (length xs - 1) xs 0
  where
    go _ [] sum     = sum
    go n (x:xs) sum = go (n-1) xs (sum + x * 10^n)

solve :: Int -> [(Int,Int)] -> Maybe Int
solve n sc = maybe Nothing f $ go sc M.empty
  where
    go [] m = Just m
    go ((i,v):xs) m =
        case M.lookup i m of
            Nothing -> go xs (M.insert i v m)
            Just vv -> if vv == v
                          then go xs (M.insert i v m) else Nothing
    f m = let l = map (\i -> fromMaybe 0 (M.lookup i m)) [1..n]
              gl = group l
              dl = concat $ drop 0 gl
           in if head (head gl) == 0
                 then Just . concatIntList. concat $ (map (\p -> 1) (head gl) : tail gl)
                 else Just $ concatIntList dl

main :: IO ()
main = do
    [n,m] <- map read . words <$> getLine :: IO [Int]
    sc <- map ((\[a,b] -> (a,b)) . (map read . words))
        . lines <$> getContents :: IO [(Int,Int)]
    print $ fromMaybe (-1) $ solve n sc
