import Data.List
import Data.Tuple

main = do
  n<-readLn
  a<-map read.words<$>getLine
  b<-map read.words<$>getLine
  print $ max (solve n a b) (solve n b a)

solve n a b= go n ts where
  ts = sortBy (flip compare) $ zipWith f b a where
    f b a = (b-a,a)
  go n [] = n
  go n ((p,c):ts)
    | p <= 0 = n
    | otherwise = (p+c) * t + go (n-c*t) ts
    where t = n `div` c