-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
import Data.Char (isSpace)
import Data.List (unfoldr, sort)
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Ord
import Data.Coerce

sortDown :: forall a. Ord a => [a] -> [a]
sortDown = coerce (sort :: [Down a] -> [Down a])

exchangeOne :: Int -> Int -> Int -> Int -> Int -> Int -> Int -> Int
exchangeOne !gA !sA !bA !gB !sB !bB !n =
  let dg = gB - gA
      ds = sB - sA
      db = bB - sB
      l = sortDown $ filter ((> 0) . fst) [(dg, gA), (ds, sA), (db, bA)]
  in case l of
       [] -> n
       [(dx,xA)] -> let (q,r) = n `quotRem` xA
                    in n + q * dx
       [(dx,xA),(dy,yA)] -> let (qx,rx) = n `quotRem` xA
                                (qy,ry) = rx `quotRem` yA
                            in n + qx * dx + qy * dy
       [(dx,xA),(dy,yA),(dz,zA)] -> let (qx,rx) = n `quotRem` xA
                                        (qy,ry) = rx `quotRem` yA
                                        (qz,rz) = rz `quotRem` zA
                                    in n + qx * dx + qy * dy + qz * dz

main = do
  n <- readLn
  [gA,sA,bA] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  [gB,sB,bB] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  print $ exchangeOne gB sB bB gA sA bA $ exchangeOne gA sA bA gB sB bB n
