
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}

import GHC.Exts (IsList(..))

import Data.String (IsString)
import Data.Array (Array, array, (!))
import qualified Data.Map as M
import Data.Map (Map, insert)
import Data.Maybe (catMaybes, fromJust)

import Control.Monad (forM_, when)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State.Strict (State, get, modify, execState)
import Control.Monad.Trans.Reader (ReaderT(..), ask)

newtype StrNum = StrNum String deriving (Eq, IsString)

instance IsList StrNum where
  type Item StrNum = Char
  fromList = StrNum
  toList (StrNum s) = s

instance Show StrNum where
  show (StrNum s) = s

instance Ord StrNum where
  compare (StrNum a) (StrNum b)
    | la < lb = LT
    | la > lb = GT
    | la == lb = case compare (head a) (head b) of
                     EQ -> compare (StrNum (tail a)) (StrNum (tail b))
                     c -> c
    where
      la = length a
      lb = length b

match :: Array Char Int
match = array ('1','9') [('1',2),('2',5),('3',5),('4',4),('5',5),('6',6),('7',3),('8',7),('9',6)]

cons :: Char -> StrNum -> StrNum
cons c (StrNum s) = StrNum (c:s)

main :: IO ()
main = do
  n <- read . head . words <$> getLine :: IO Int
  as <- map head . words <$> getLine
  let dptable = flip execState (M.fromList [(0,"")]) $ flip runReaderT as $ dp n
  print $ fromJust $ M.lookup n dptable

dp :: Int -> ReaderT [Char] (State (Map Int StrNum)) ()
dp 0 = return ()
dp n = do
  dp (n-1)
  table <- lift get
  as <- ask
  let ds = catMaybes [(cons a) <$> M.lookup (n-m) table | a <- as, let m = match ! a]

  when (not $ null ds) (lift $ modify $ insert n $ maximum ds)
