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

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

cmp :: StrL -> StrL -> Ordering
cmp (s1,l1) (s2,l2)
  | l1 > l2 = GT
  | l1 < l2 = LT
  | otherwise = cmp' s1 s2
  where
    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 ("",0)
      nth n =
        case catMaybes [(cons a)<$>result!(n-m)|a<-as, let m=match!a, n-m>=0] of
          [] -> Nothing
          xs -> Just $ maximumBy cmp xs
  putStrLn $ fst $ fromJust $ result!n
