import sys



coins = [1,5,10,50,100,500]



for i in sys.stdin:

    if int(i) == 0:

        break



    n =1000-int(i)

    c = [ i for i in range(n+1) ]

    for i in range(len(coins)):

        coin = coins[i]

        if coin <= n:

            c[coin] = 1

            coins.append(coin)



    for coin in coins:

        for p in range(coin+1, n+1):

            minc = c[p]

            if minc == 1:

                continue

            c[p] = c[p-coin]+1 if minc > c[p-coin] + 1 else minc



    print((c[n]))
import sys



coins = [500,100,50,10,5,1]



for i in sys.stdin:

    if int(i) == 0:

        break



    n =1000-int(i)

    cnt = 0



    while n != 0:

        for i in range(len(coins)):

            if n >= coins[i]:

                n = n-coins[i]

                break

        cnt += 1



    print(cnt)
import sys

coins = [1, 5, 10, 50, 100, 500]
for i in sys.stdin:
    if int(i) == 0:
        break
    n = 1000 - int(i)
    c = [i for i in range(n + 1)]
    for i in range(len(coins)):
        coin = coins[i]
        if coin <= n:
            c[coin] = 1
            coins.append(coin)
    for coin in coins:
        for p in range(coin + 1, n + 1):
            minc = c[p]
            if minc == 1:
                continue
            c[p] = c[p - coin] + 1 if minc > c[p - coin] + 1 else minc
    print((c[n]))

import sys

coins = [500, 100, 50, 10, 5, 1]
for i in sys.stdin:
    if int(i) == 0:
        break
    n = 1000 - int(i)
    cnt = 0
    while n != 0:
        for i in range(len(coins)):
            if n >= coins[i]:
                n = n - coins[i]
                break
        cnt += 1
    print(cnt)

-coins = [1, 5, 10, 50, 100, 500] +coins = [500, 100, 50, 10, 5, 1] -    c = [i for i in range(n + 1)] -    for i in range(len(coins)): -        coin = coins[i] -        if coin <= n: -            c[coin] = 1 -            coins.append(coin) -    for coin in coins: -        for p in range(coin + 1, n + 1): -            minc = c[p] -            if minc == 1: -                continue -            c[p] = c[p - coin] + 1 if minc > c[p - coin] + 1 else minc -    print((c[n])) +    cnt = 0 +    while n != 0: +        for i in range(len(coins)): +            if n >= coins[i]: +                n = n - coins[i] +                break +        cnt += 1 +    print(cnt)