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 main():
    while True:
        s = int(input())
        if s == 0:
            break
        cnt = 0
        s = 1000 - s
        if s//500 != 0:
            cnt += s//500
            s %= 500
        if s//100 != 0:
            cnt += s//100
            s %= 100
        if s//50 != 0:
            cnt += s//50
            s %= 50
        if s//10 != 0:
            cnt += s//10
            s %= 10
        if s//5 != 0:
            cnt += s//5
            s %= 5
        if s//1 != 0:
            cnt += s//1
            s %= 1
        print(cnt)

if __name__ == "__main__":
    main()

Python: # -*- coding: utf-8 -*-

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

while True:
    n = int(input())
    if n == 0:
        break

    ch = 1000 - n
    ans = 0
    for i in range(6):
        num = int(ch / c[i])
        ans += num
        ch -= num * c[i]
    print(ans)

Python: coins=[500,100,50,10,5,1]
while 1:
    v=int(input())
    if v==0:
        break
    else:
        change=1000-v
        cnt=0
        for coin in coins:
            c=change//coin
            if c!=0:
                cnt+=c
                change-=coin*c
        print(cnt)

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

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

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

Python: def change(v):
    global c, o
    if o>= v:
        t= o//v
        o, c= o-(v*t), c+t

while True:
    o= int(input())
    if o== 0: break
    c, o= 0, 1000-o
    for l in [500, 100, 50, 10, 5, 1]: change(l)
    print(c+o)

Python: # AOJ 0521: Change
# Python3 2018.6.30 bal4u

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

Python: x = int(input())
while x != 0:
    y = 1000 - x
    a = y//500
    b = (y-500*a)//100
    c = (y-(500*a+100*b))//50
    d = (y-(500*a+100*b+50*c))//10
    e = (y-(500*a+100*b+50*c+10*d))//5
    f = (y-(500*a+100*b+50*c+10*d+5*e))//1
    g = a+b+c+d+e+f
    print(g)
    x = int(input())

Python: while True:
    n=int(input())
    if n==0:
        break
    a=(1000-n)//500
    b=(1000-n-(500*a))//100
    c=(1000-n-(500*a)-(100*b))//50
    d=(1000-n-(500*a)-(100*b)-(50*c))//10
    e=(1000-n-(500*a)-(100*b)-(50*c)-(10*d))//5
    f=(1000-n-(500*a)-(100*b)-(50*c)-(10*d)-(5*e))//1
    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: c = [500, 100, 50, 10, 5, 1]
while True:
	m = 1000-int(input())
	if m == 1000: break
	ans = 0
	for i in c:
		ans += m // i
		m %= i 
	print(ans)

Python: coins = [500, 100, 50, 10, 5, 1]
if __name__ == '__main__':
    while True:
        p = int(input())
        if p == 0:
            break
        ch = 1000 - p

        count = 0
        for coin in coins:
            n = ch // coin
            ch -= coin * n
            count += n
        
        print(count)

Python: while 1:
    n = int(input())
    if n == 0:
        break
    else:
        change = 1000-n
        sum_coin = 0
        coin = [500,100,50,10,5,1]

        for i in coin:
            r = change // i
            change %= i
            sum_coin += r
    
        print(sum_coin)

Python: 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 True:
  n = int(input())
  if not n: break
  ans = 0
  m = 1000 - n
  for i in [500,100,50,10,5,1]:
    ans += m // i
    m = m % i
  print(ans)

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

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

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

while True:
    x = 1000 - int(input())
    if x == 1000:
        break

    ret = 0
    for coin in coins:
        n = x//coin
        x = x - n*coin
        ret += n

    print(ret)

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

Python: for e in iter(input,'0'):
	a,b=0,1000-int(e)
	for i in[500,100,50,10,5]:
		a+=b//i
		b%=i
	print(a+b)

Python: #Volume5-0521
while True:
  nedan = input()
  if nedan == str(0):
    break
  o = str(1000 - int(nedan))
  m = int(0)
  if int(nedan) > 990:
    n = int(1)
  elif int(nedan) > 900:
    n = int(2)
  else:
    n = int(3)
  for a in range(n):
    if int(o[a]) < 5:
      m = m+int(o[a])
    else:
      m = m+int(o[a])-4
  print(m)

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

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