import Control.Monad
import Data.Char
import Data.List

main = do
    [n, m] <- fmap read <$> words <$> getLine
    a <- fmap read <$> words <$> getLine
    putStrLn $ solve n m a

solve :: Int -> Int -> [Int] -> String
solve n m a = doit n ""
  where
    doit i s | i >= (ms !! head pr) * 3 = doit (i - (ms !! head pr)) (s ++ show (head pr))
             | otherwise = max (maxnum ++ s) (s ++ maxnum)
      where
        maxnum = show $ head $ filter ok [9999, 9998 .. 1]
        ok num = all (\d -> elem d pr) ds && sum (map (ms !!) ds) == i
          where
            ds = map digitToInt $ show num
    ms = [100, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    pr = filter (\i -> elem i a) [1, 7, 4, 5, 3, 2, 9, 6, 8]