n = gets.to_i
prices = gets.split.map(&:to_i)
nums = gets.split.map(&:to_i)
table = Array.new(n + 1, Float::INFINITY)
table[0] = 0
(0..3).each do |i|
  new_table = Array.new(n + 1)
  (0..n).each do |x|
    new_table[x] = (0..[(x.to_f/nums[i]).ceil, 1].max).map {|k| table[[x - k * nums[i], 0].max] + k * prices[i] }.min
  end
  table = new_table
end
p table[n]

