
import Data.Array (Array, (!), listArray)
import qualified Data.Map as M
import Data.Foldable (maximumBy)

import Control.Monad.Trans.State (State, execState, modify, get)
import Control.Monad (forM_)
import Data.Maybe (catMaybes, fromJust)

type StrL = (String,Int)
headL (x:xs, _) = xs
tailL (x:xs, n) = (xs, n-1)
cons x (xs, n) = (x:xs, n+1)

cmp (s1,l1) (s2,l2)
  | l1 < l2 = LT
  | l1 > l2 = GT
  | otherwise = compare s1 s2

main :: IO ()
main = do
  n <- read . head . words <$> getLine
  as <- map head . words <$> getLine
  let result = mm as n
  putStrLn . fst . fromJust . M.lookup n $ result

match = listArray ('1','9') [2,5,5,4,5,6,3,7,6]

mm :: [Char] -> Int -> M.Map Int StrL
mm as n = flip execState (M.fromList []) $ do
    forM_ [0..n] $ \i -> do
      tab <- get
      case nth tab i of
        Nothing -> return ()
        Just s -> modify $ M.insert i s
  where
    nth :: M.Map Int StrL -> Int -> Maybe StrL
    nth _ 0 = Just ("", 0)
    nth tab n =
      let cands = catMaybes [(cons a)<$>M.lookup (n-m) tab |a<-as, let m=match!a] in
      case cands of
        [] -> Nothing
        xs -> Just $ maximumBy cmp xs
