
import Data.Foldable (maximumBy)
import Data.Maybe (catMaybes, fromJust)
import Data.Array (listArray, (!))

cmp :: String -> String -> Ordering
cmp s1 s2
  | l1 > l2 = GT
  | l1 < l2 = LT
  | otherwise = cmp' s1 s2
  where
    l1 = length s1
    l2 = length s2
    cmp' [] [] = EQ
    cmp' (x:xs) (y:ys)
      | x > y = GT
      | x < y = LT
      | otherwise = cmp xs ys

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

main :: IO ()
main = do
  n <- read . head . words <$> getLine
  as <- map head . words <$> getLine
  let result = listArray (0,n) $ map nth [0..] where
      nth 0 = Just ""
      nth n =
        case catMaybes [(a:)<$>result!(n-m)|a<-as, let m=match!a, n-m>=0] of
          [] -> Nothing
          xs -> Just $ maximumBy cmp xs
  putStrLn $ fromJust $ result!n
