### constant

MAX_INT = 1 << 30

COINS = [1, 5, 10, 50, 100, 500]
RCOINS = COINS.reverse

### subroutines

def num_coins(pn, nis)
  num = 0

  5.downto(0) do |i|
    ci = COINS[i]
    k = [pn / ci, nis[i]].min
    next if k == 0

    num += k
    pn -= ci * k
    break if pn == 0
  end

  num
end

def changes(pn)
  count = 0

  for cn in RCOINS
    k = pn / cn
    count += k
    pn -= k * cn
    break if pn == 0
  end

  count
end

### main

loop do
  pn, *nis = gets.strip.split(' ').map(&:to_i)
  break if pn == 0

  min_num = MAX_INT

  for plus in (0..499)
    n0 = num_coins(pn + plus, nis)
    n1 = changes(plus)
    num = n0 + n1

    min_num = num if min_num > num
  end

  puts min_num
end