# util functions

# debug
$debugFlag = ( ARGV[0] != nil )
p "==== DEBUG MODE ====" if $debugFlag

def pd(str)
  p str if $debugFlag
end

def printd(str)
  print str if $debugFlag
end

# input
inputs = []
lc = 0
while l = $stdin.gets do
  if false
    inputs << l.chomp.split(" ").map(&:to_s)
  else
    inputs << l.chomp.split(" ").map(&:to_i)
  end
  lc += 1
end

nn = inputs[0][0]
$as = inputs[1]
$bs = inputs[2]

# input check
if $debugFlag
  p "--- INPUT (begin) ---"
  inputs.each do |l_i|
    p l_i
  end
  p "--- INPUT (end) ---"
  print "\n"
end

# body
if $debugFlag
  p nn
  p $as
  p $bs
end

$memo1 = {}
def dp1(x)
  return 0 if x == 0
  return $memo1[x] if $memo1[x] != nil

  cand = [x]
  cand << (dp1(x - $as[0]) + $bs[0]) if x >= $as[0]
  cand << (dp1(x - $as[1]) + $bs[1]) if x >= $as[1]
  cand << (dp1(x - $as[2]) + $bs[2]) if x >= $as[2]

  $memo1[x] = cand.max
  return $memo1[x]
end

$memo2 = {}
def dp2(x)
  return 0 if x == 0
  return $memo2[x] if $memo2[x] != nil

  cand = [x]
  cand << (dp2(x - $bs[0]) + $as[0]) if x >= $bs[0]
  cand << (dp2(x - $bs[1]) + $as[1]) if x >= $bs[1]
  cand << (dp2(x - $bs[2]) + $as[2]) if x >= $bs[2]

  $memo2[x] = cand.max
  return $memo2[x]
end

nn = dp1(nn)
nn = dp2(nn)

puts nn
