{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.Maybe
import Debug.Trace
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import qualified Data.IntSet as IntSet
import Data.Functor
import Data.Array

main :: IO ()
main = do
  (n:m:_) <- fmap (map read . words) getLine :: IO [Int]
  as <- fmap (map read . words) getLine :: IO [Int]
  print $ solve n as

cost = Map.fromList $ zip [1..] [2,5,5,4,5,6,3,7,6]

calc as = foldl' (\acc (x, y) -> acc + (fromIntegral x) * y) 0 $ zip (sort as) $ map (10^) [0..]

solve :: Int -> [Int] -> Integer
solve n as = calc $ dp!n
  where
    value = Map.fromList . filter (\(c, a) -> elem a as) $ zip [2,5,5,4,5,6,3,7,6] [1..] 
    costs = map (\a -> cost Map.! a) as
    mincost = minimum costs
    dp = listArray (mincost, n) $ map f [mincost..n] :: Array Int [Int]
    f i | i == mincost = [value Map.! mincost]
        | otherwise = select 0 [] $ [ new c | c <- costs]
        where
          new c | i - c >= mincost = dp!(i - c) ++ [value Map.! c]
                | otherwise = []
          select _ maxx [] = maxx
          select maxv maxx (x:xs) = let cx = calc x in 
                                    if maxv <= cx
                                    then select cx x xs
                                    else select maxv maxx xs

