Go: package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
	"bufio"
)

var (
	sc = bufio.NewScanner(os.Stdin)
	buf = make([]byte, 1000)
)

func scanText() string{
	sc.Scan()
	return sc.Text()
}

func scanTextArray(n int) []string {
	out := make([]string, n)
	for i, v := range(strings.Split(scanText(), "")) {
		out[i] = v
	}
	return out
}

func scanInt() int {
	out, e := strconv.Atoi(scanText())
	if e != nil {
        panic(e)
    }
	return out
}

func scanIntArray(n int) []int {
	out := make([]int, n)
	for i, v := range(strings.Split(scanText(), " ")) {
		num, e := strconv.Atoi(v)
		if e != nil {
			panic(e)
		}
		out[i] = num
	}
	return out
}
//  template end

var (
	coins = [6]int{500, 100, 50, 10, 5, 1}
)

func f(data int) {
	ans := 0
	change := 1000 - data
	for _, v := range coins {
		for ;v <= change; {
			change -= v
			ans++
		}
	}
	fmt.Println(ans)
}

func main() {
	sc.Buffer(buf, 10000)
	for{
		data := scanInt()
		if data == 0 {
			break
		}
		f(data)
	}
}

Python: for i in range(1,5+1) :
    n = int(input())
    x = 1000-n
    if n == 0 :
        break
    
    else :
        a = x//500
        b = (x-500*a)//100
        c = (x-500*a-100*b)//50
        d = (x-500*a-100*b-50*c)//10
        e = (x-500*a-100*b-50*c-10*d)//5
        f = x-500*a-100*b-50*c-10*d-5*e
        print(a+b+c+d+e+f)

Python: while True:
    x=int(input())
    if x==0:
        break
    oturi=1000-x
    a=oturi//500
    b=oturi//100-a*5
    c=(oturi%100)//50
    d=(oturi%100)//10-c*5
    e=(oturi%10)//5
    f=(oturi%10)-e*5
    kazu=a+b+c+d+e+f
    print(kazu)

Python: # coding: utf-8
# Your code here!
while True:
   x = int(input())
   if x == 0:
      break
   
   y = 1000 - x;
   a = y % 500
   b = a % 100
   c = b % 50
   d = c % 10
   e = d % 5
   z = y // 500 + a // 100 + b // 50 + c // 10 + d // 5 + e
   
   print(z)

Python: while 1:
    n = int(input())
    if n == 0:
        break

    charge = 1000 - n
    cnt = 0
    if charge >= 500:
        cnt += 1
        charge -= 500
    cnt += charge // 100
    charge %= 100

    if charge >= 50:
        cnt += 1
        charge -= 50
    cnt += charge // 10
    charge %= 10

    if charge >= 5:
        cnt += 1
        charge -= 5
    cnt += charge

    print(cnt)

Python: x=1000
while True:
    y=int(input())
    if y>0:
        a=(x-y)//500
        b=(x-y)%500//100
        c=(x-y)%100//50
        d=(x-y)%50//10
        e=(x-y)%10//5
        f=(x-y)%5
        print(a+b+c+d+e+f)
    else:
        break

Python: while True:
    a=int(input())
    if a==0:
        break
    b=(1000-a)//500
    c=(1000-a)%500//100
    d=(1000-a)%500%100//50
    e=(1000-a)%500%100%50//10
    f=(1000-a)%500%100%50%10//5
    g=(1000-a)%500%100%50%10%5
    print(b+c+d+e+f+g)

Python: a = [500, 100, 50, 10, 5, 1]
while True:
	b = 1000-int(input())
	if b == 1000:
	        break
	c = 0
	for i in a:
		c += b // i
		b %= i 
	print(c)

Python: while True:
    n = int(input())
    if n == 0:
        break
    p = 1000-n
    
    c = 0
    for k in [500, 100, 50, 10, 5, 1]:
        if p == 0:
            break
        c += p // k
        p %= k
        
    print(c)

Python: COINS = [500, 100, 50, 10, 5, 1]

change = 1000 - int(input())
while change != 1000:
    total = 0
    for coin in COINS:
        total += change // coin
        change %= coin

    print(total)

    change = 1000 - int(input())

Python: i=0
while i<6:
    i+=1
    x=int(input())
    y=1000-x
    g=y//500
    h=(y-500*g)//100
    d=(y-500*g-100*h)//50
    z=(y-500*g-100*h-50*d)//10
    f=(y-500*g-100*h-50*d-10*z)//5
    i=y-500*g-100*h-50*d-10*z-5*f
    if x==0:
        break
    print(g+h+d+z+f+i)

Python: while True:
    n=int(input())
    if n==0:
        break
    m=1000-n
    a=0
    for i in [500,100,50,10,5,1]:
        a+=m//i
        m=m%i
    print(a)

Python: while True:
    a = int(input())
    if  a == 0:
        break
    else:
        b = 1000-a
        c = b%500
        d = c%100
        e = d%50
        f = e%10
        g = f%5
        print(b//500+c//100+d//50+e//10+f//5+g)

Python: while True:
    try:
        a=list(map(int,input().split()))
        x=0
        y=0
        z=0
        u=0
        w=0
        
        g=1000-a[0]
        if g==1000:
            pass
        else:
            if g>=500:
                x=1
                g=g-500
            y=g//100
            g=g-100*y
            if g>=50:
                z=1
                g=g-50
            u=g//10
            g=g-10*u
            if g>=5:
                w=1
                g=g-5
            print(x+y+z+u+w+g)
    except:
        break;

Python: try:
    while True:
        n=int(input())
        m=1000-n
        i=0
        if m==1000:
            break
        else:
            i+=m//500
            m=m%500
            i+=m//100
            m=m%100
            i+=m//50
            m=m%50
            i+=m//10
            m=m%10
            i+=m//5
            m=m%5
            i+=m
        print(i)
except EOFError:
    pass

Python: c = [500, 100, 50, 10, 5, 1]
while True:
    n = int(input())
    x = 1000 - n
    if x == 1000:
        break
    a = 0
    for i in c:
        a += x // i 
        x %= i 
    print(a)

Python: while True:
  coin = int(input())
  if coin == 0: break
  ans = 0
  oturi = 1000 - coin
  for i in [500,100,50,10,5,1]:
    ans += oturi // i
    oturi = oturi % i
  print(ans)

Python: while True:
	n = int(input())
	if n == 0:
		break
	m = 0
	c = 1000 - n
	while c != 0:
		if c >= 500:
			c = c - 500
			m = m + 1
		elif c >= 100:
			c = c - 100
			m = m + 1
		elif c >= 50:
			c = c - 50
			m = m + 1
		elif c >= 10:
			c = c - 10
			m = m + 1
		elif c >= 5:
			c = c - 5
			m = m + 1
		elif c >= 1:
			c = c - 1
			m = m + 1
	print(m)

Python: while True:
    t = int(input())
    if t==0:
        break
    n = (1000-t)//500+((1000-t)%500)//100+((1000-t)%100)//50+((1000-t)%50)//10+((1000-t)%10)//5+((1000-t)%5)
    print(n)

Python: while True:
    n, ans = 1000 - int(input()), 0
    if n == 1000:
        break
    for m in (500, 100, 50, 10, 5, 1):
        ans, n = ans+n//m, n%m
    print(ans)

Python: #!/usr/bin/env python3
# -*- coding: utf-8 -*-


def solve(price):
    change = 1000 - price
    total_coins = 0
    for coin in [500, 100, 50, 10, 5, 1]:
        coins, change = divmod(change, coin)
        total_coins += coins
    return total_coins


if __name__ == '__main__':
    while True:
        n = int(input())
        if n == 0:
            break
        else:
            print(solve(n))

Python: while True:
    a=int(input())
    if a==0:
        break
    b=((1000-a)//500)
    c=(((1000-a)-500*b)//100)
    d=(((1000-a)-500*b-100*c)//50)
    e=(((1000-a)-500*b-100*c-50*d)//10)
    f=(((1000-a)-500*b-100*c-50*d-10*e)//5)
    g=((1000-a)-500*b-100*c-50*d-10*e-5*f)
    print(b+c+d+e+f+g)

Python: a = [500,100,50,10,5,1]
while True:
    x = int(input())
    if x == 0:
        break
    x = 1000-x
    b = 0
    for c in a:
        b += x//c
        x %= c
    print(b)

Python: while True:
    a = int(input())
    if a == 0:
        break
    a = 1000-a
    c = 0
    coins = [500,100,50,10,5,1]
    for coin in coins:
        c += a//coin
        a = a%coin
    print(c)

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0521

"""
import sys
from sys import stdin
input = stdin.readline


def count_coint(change):
    ans = 0
    coins = (500, 100, 50, 10, 5, 1)
    for c in coins:
        t = change // c
        ans += t
        change -= t * c
    return ans


def main(args):
    while True:
        payment = int(input())
        if payment == 0:
            break

        change = 1000 - payment
        result = count_coint(change)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Python: coin = [500,100,50,10,5,1]
while True:
    n = int(input())
    if n == 0:
        break
    n = 1000-n
    ans = 0
    for c in coin:
        ans += n//c
        n %= c
    print(ans)