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 = length $ map digitToInt $ show x

solve n m acc
    | MP.null m && (parse acc /= n) = -1
    | MP.null m && (parse acc == n) = acc
    | null xs = solve n m' (acc*10)
    | otherwise = solve n m' (acc*10 + minimum xs)
    where
        ((i,xs),m') = MP.deleteFindMin m

main = do
   [n,m] <- map read . words <$> getLine :: IO [Int]
   xs <- replicateM m (toTuple . map read . words <$> getLine) :: IO [(Int,Int)]
   let l = foldl' (\m x -> MP.insert x [] m) MP.empty [1,2,3]
   let l' = foldl' (\m (a,b) -> MP.adjust (b:) a m) l xs 
   print $ solve n l' 0