def get_donguri(donguri, from_rate, to_rate)
  rate = from_rate.zip(to_rate)
  rate = rate.reject { |r1, r2| r1 >= r2 }

  dp = [0] * (donguri + 1)
  0.upto(donguri) do |i|
    rate.each do |r|
      next if i + r[0] > donguri
      dp[i + r[0]] = [dp[i + r[0]], dp[i] + r[1]].max
    end
  end
  0.upto(donguri) do |i|
    dp[i] += donguri - i
  end
  dp.max
end


N = gets.chop.to_i
A_RATE = gets.chop.split.map(&:to_i)
B_RATE = gets.chop.split.map(&:to_i)

n = get_donguri(N, A_RATE, B_RATE)
n = get_donguri(n, B_RATE, A_RATE)

puts n

