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: def calc(n):
	a = 1000 - n
	y = [500, 100, 50, 10, 5, 1]
	ans = 0
	for i in y:
		ans += a // i
		a %= i
		if a == 0:
			return ans
def resolve():
	while True:
		v = int(input())
		if v == 0:
			return
		print(calc(v))
resolve()

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

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

print(total)

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

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

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

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

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

Python: # coding: utf-8
# Your code here!

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

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

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

Python: i = 0

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

Python: while True:
    p=int(input())      #while文のなかで定義
    if p==0:break       #p=pay
    a=0                 #aを定義しておく
    c=1000-p        #c=change
    for i in [500,100,50,10,5,1]:
        a+=c//i     #aにc//iを足す
        c=c%i
    print(a)

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

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

Python: x = int(input())
while x != 0:
    y = 1000 - x
    a = y // 500
    A = y % 500
    b = A // 100
    B = A % 100
    c = B // 50
    C = B % 50
    d = C // 10
    D = C % 10
    e = D // 5
    E = D % 5
    print(a + b + c + d + e + E)
    x = int(input())

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:
    price = int(input())
    change = 1000 - price
    n = 0
    if price == 0:
        break
    if change >= 500:
        change -= 500
        n += 1
    while change >= 100:
        change -= 100
        n += 1
    if change >= 50:
        change -= 50
        n += 1
    while change >= 10:
        change -= 10
        n += 1
    if change >= 5:
        change -= 5
        n += 1
    while change >= 1:
        change -= 1
        n += 1
        
    print(n)

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

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

Python: coins = (500, 100, 50, 10, 5, 1)

def count(x, y):
    ret = 0
    while x >= y:
        x -= y
        ret += 1
    return x, ret

while True:
    n = input()
    if n == 0:
        break
    ans = 0
    n = 1000 - n
    for val in coins:
        n,  t = count(n, val)
        ans += t

    print(ans)

Python: while True:
    N = int(input())
    if N == 0:
        break
    ans = 0
    change = 1000 - N
    while change >= 500:
        change -= 500
        ans += 1
    while change >= 100:
        change -= 100
        ans += 1
    while change >= 50:
        change -= 50
        ans += 1
    while change >= 10:
        change -= 10
        ans += 1
    while change >= 5:
        change -= 5
        ans += 1
    ans += change
    print(ans)

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

Python: while 1:
    try:
        n=int(input())
        if n==0:break
        change=1000-n
        ans=0
        kouka=[500,100,50,10,5,1]
        for i in kouka:
            ans+=change//i
            change=change%i
        print(ans)
    except:break

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

Python: while True:
    n = int(input())
    if n == 0:
        break
    chg = 1000 - n
    m = [500, 100, 50, 10, 5, 1]
    cnt = [0,0,0,0,0,0]
    for i in range(len(m)):
        cnt[i] = chg//m[i]
        chg -=cnt[i]*m[i]
    ans = 0
    for i in range(len(cnt)):
        ans += cnt[i]
    print(ans)