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

solve :: Int->[Int]->[Int]->Int
solve n a' b'= go ts where
  ts = zipWith f a b where
    f a b = (b-a,a)
  go [(p1,c1),(p2,c2),(p3,c3)] = n + maximum ans where
    ans = [ p1*t1 + p2*t2 + p3*t3
          | t1 <- [0..n`div`c1]
          , t2 <- [0..(n-t1*c1)`div`c2]
          , let t3 = (n-t1*c1-t2*c2)`div`c3]
  a = zipWith min a' b'
  b = zipWith max a' b'