import Data.List (intersect, sortBy)
import qualified Data.IntMap as M
import Data.Function (on)

main :: IO ()
main = do
  n <- read . head . words <$> getLine
  as <- map read . words <$> getLine
  let
      ps = map (id >< head) $ filter (not . null . snd) $ map (id >< (intersect as)) priority
      ps' = M.fromAscList ps
      js = map fst $ reverse $ sortBy (compare `on` snd) ps -- descending
      j' = last js
      n' = n `div` j'
      dp = M.fromAscList $ map ((,) <$> id <*> f) [0..n']
      f 0 = [(0, [])]
      f i = do
        j <- js
        (s, r) <- dp M.! (pred i)
        if j + s <= n then return (j + s, j : r) else []
      v = maximum $ map (foldl (\a j -> 10 * a + (ps' M.! j)) 0) $ head $ dropWhile null $ map (map snd . filter ((== n) . fst) . (dp M.!)) $ reverse [0..n']
  print v

priority = [(2, [1]), (3, [7]), (4, [4]), (5, [5, 3, 2]), (6, [9, 6]), (7, [8])]

(f >< g) (x, y) = (f x, g y)
infixr 5 ><
