n = gets.to_i
have = [n,0,0,0]
exc1 = gets.split.map(&:to_i)
exc2 = gets.split.map(&:to_i)
excm = [0,0,0]
for i in 0..2
  if exc1[i] < exc2[i]
    excm[i] = 1
  elsif exc1[i] > exc2[i]
    excm[i] = 2
  end
end


#取引所Aでの変換
if excm.count(1) == 1
  for i in 0..2
    if excm[i] == 1
      have[i+1] += (have[0]/exc1[i])
      have[0] -= have[i+1]*exc1[i]
    end
  end
elsif excm.count(1) >= 2
  dp = Array.new(excm.count(1)+1).map{Array.new(have[0]+1,0)}
  count = 0
  for i in 0..2
    if excm[i] == 1
      count += 1
      for j in 1..have[0]
        x = -1
        if j >= exc1[i]
          x = dp[count][j-exc1[i]]+1
        end
        ex = [dp[count-1][j],dp[count][j-1],x]
        ex.sort!
        dp[count][j] = ex[-1]
      end
    end
  end
  count += 1
  2.downto(0){|i|
    if excm[i] == 1
      count -= 1
      while dp[count][have[0]] > 0 && dp[count][have[0]-exc1[i]]+1 == dp[count][have[0]]
        have[i+1] += 1
        have[0] -= exc1[i]
      end
    end
  }
end


#取引所Bでの変換
for i in 0..2
  have[0] += have[i+1]*exc2[i]
  have[i+1] = 0
end
if excm.count(2) == 1
  for i in 0..2
    if excm[i] == 2
      have[i+1] += (have[0]/exc2[i])
      have[0] -= have[i+1]*exc2[i]
    end
  end
elsif excm.count(2) >= 2
  dp = Array.new(excm.count(2)+1).map{Array.new(have[0]+1,0)}
  count = 0
  for i in 0..2
    if excm[i] == 2
      count += 1
      for j in 1..have[0]
        x = -1
        if j >= exc2[i]
          x = dp[count][j-exc2[i]]+1
        end
        ex = [dp[count-1][j],dp[count][j-1],x]
        ex.sort!
        dp[count][j] = ex[-1]
      end
    end
  end
  count += 1
  2.downto(0){|i|
    if excm[i] == 1
      count -= 1
      while dp[count][have[0]] > 0 && dp[count][have[0]-exc2[i]]+1 == dp[count][have[0]]
        have[i+1] += 1
        have[0] -= exc2[i]
      end
    end
  }
end


for i in 0..2
  have[0] += have[i+1]*exc1[i]
  have[i+1] = 0
end

puts have[0]
