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)
	}
}

C++: #include<stdio.h>

int main(){
	const int CASH = 1000;
	int price;
	int hundred;
	int ten;
	int digit;
	int count;

	while( 1 ) {

		count = 0;

		scanf( "%d",&price );
		if( !price ) break;

		price = CASH - price;

		hundred = price / 100;
		ten = ( price - hundred * 100 ) / 10;
		digit = price - ( hundred * 100 ) - (ten * 10);

		while( hundred > 0 ){
			if( hundred >= 5 ){
				hundred -= 5;
				count++;
			}else{
				hundred--;
				count++;
			}
		}

		while( ten > 0 ){
			if( ten >= 5 ){
				ten -= 5;
				count++;
			}else{
				ten--;
				count++;
			}
		}

		while( digit > 0 ){
			if( digit >= 5 ){
				digit -= 5;
				count++;
			}else{
				digit--;
				count++;
			}
		}

		printf("%d\n",count);

	}
	return 0;
}

C++: // AOJ 0521
#include<cstdio>
#define rep(i,a) for( int i = 0; i != (a); ++i )

const int C[6] = { 500, 100, 50, 10, 5, 1 };

int main()
{
	int p;
	scanf( "%d", &p );
	p = 1000-p;

	int ans = 0;
	rep( i, 6 )
		ans += p/C[i], p %= C[i];

	printf( "%d\n", ans );

	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
	int n,m;
	cin >> n;
	m = 0;

	while( n <= 500 ){
		n = n-500;
		m++;
	}
	while( n <= 100 ){
		n = n-100;
		m++;
	}
	while( n <= 50 ){
		n = n-50;
		m++;
	}
	while( n <= 10 ){
		n = n-10;
		m++;
	}
	while( n <= 5 ){
		n = n-5;
		m++;
	}
	while( n <= 1){
		n = n-1;
		m++;
	}

	cout << m << endl;

	return 0;
}

C++: // ???#include <bits/stdc++.h>
#include <iostream>

// #define MP make_pair
// #define PB push_back
// #define ALL(x)  (x).begin(),(x).end()
// #define RALL(x) (x).rbegin(),(x).rend()
// #define RANGEBOX(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
// #define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
// #define rep(i, N)  for (int i = 0; i < (int)(N); i++)
// #define rrep(i, N) for (int i = N - 1; i >= 0; i--)
// #define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
// #define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
// #define PB push_back

using namespace std;

// typedef long long LL;
// typedef unsigned long long ULL;
// typedef vector<int> VI;
// typedef vector<VI > VVi;
// typedef vector<string> VS;
// typedef vector<LL> VLL;
// typedef pair<int,int> PII;

const int    INF  = 0x3f3f3f3f;
const LL     INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;

int main(){
	ios::sync_with_stdio(false);
    int sum=0;
    int x;
    int cnt=0;
    while(1){
        cin>>x;
        if(!x)
            return 0;
        sum=1000-x;
        while(sum>0){
            if(sum>=500){
                cnt++;
                sum-=500;
            }
            else if(sum>=100){
                cnt++;
                sum-=100;
            }
            else if(sum>=50){
                cnt++;
                sum-=50;
            }
            else if(sum>=10){
                cnt++;
                sum-=10;
            }
            else if(sum>=5){
                cnt++;
                sum-=5;
            }
            else{
                cnt++;
                sum-=1;
            }
        }
        cout<<cnt<<endl;
        cnt=0;
    }


    return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,s,ss,z,z1,z2,z3,z4;
	scanf("%d",&n);
	while(n!=0){
	ss=1000-n;
	s=ss/500;
	z=ss%500;
	s+=z/100;
	z1=z%100;
	s+=z1/50;
	z2=z1%50;
	s+=z2/10;
	z3=z2%10;
	s+=z3/5;
	z4=z3%5;
	s+=z4/1;	
	printf("%d\n",s);
	scanf("%d",&n);
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>

#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){

	int n;
	int money[6] = {500,100,50,10,5,1};
	while(cin >> n, n){
		
		int change = 0;
		n = 1000 - n;
		rep(i,6){
			
			change += n / money[i];
			n = n % money[i];
		}
		cout << change << endl;
	}

	return 0;
}

C++: #include <iostream>
using namespace std;
int main(void){
    // Here your code !
    int n = 1;
    int pay ;

    
    while (n){
        int cent = 0;
        
        cin >>n;
        if (n==0){
            break;
        }
    

    
    pay = 1000 - n;
    
    if (pay >= 500){
        pay -= 500;
        cent++;
    }
    
    while (pay >= 100){
        pay -= 100;
        cent++;
    }
    
    while (pay >= 50){
        pay -= 50;
        cent++;
    }
    
    while (pay >= 10){
        pay -= 10;
        cent++;
    }
    
    while (pay >= 5){
        pay -= 5;
        cent++;
    }
    
    while (pay >= 1){
        pay -= 1;
        cent++;
    }
    
    cout << cent ;
 
    
    }
    return 0;

}

C++: #include<iostream>

using namespace std;

void Calculate(int, int&, int&);

int main(){
  int n, m, count;

  while(1){
    cin >> n;
    if(n == 0) break;

    n = 1000 - n;
    count = 0;

    Calculate(500, n, count);
    Calculate(100, n, count);
    Calculate(50, n, count);
    Calculate(10, n, count);
    Calculate(5, n, count);
    Calculate(1, n, count);

    cout << count << endl;
  }

  return 0;
}

void Calculate(int m, int& n, int& count){
  int x;

  while(1){
    x = n - m;
    if(x < 0) break;
    ++count;
    n = x;
  }
}

C++: #include <cstdio>

using namespace std;

int main() {
	int coin[6]={500,100,50,10,5,1};
	int money,ans;
	while(scanf("%d",&money),money) {
		money=1000-money;
		ans=0;
		for(int i=0;i<6;i++) {
			ans+=money/coin[i];
			if(!(money%=coin[i])) break;
		}
		printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
using namespace std;
int main()
{
	int a = 0;
	int b;
	int c = 0;
	for (; a != 0;)
	{
		cin >> a;
		b = a;
		for (; b != 0;)
		{
			if (b >= 500)
			{
				b = b - 500;
				c++;
			}
			if (b >= 100)
			{
				b = b - 100;
				c++;
			}
			if (b >= 50)
			{
				b = b - 50;
				c++;
			}
			if (b >= 10)
			{
				b = b - 10;
				c++;
			}
			if (b >= 5)
			{
				b = b - 5;
				c++;
			}
			if (b >= 1)
			{
				b = b - 1;
				c++;
			}
			cout<<c<<endl
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include <climits>
#include <map>
#include <set>
const int mod = 1e9 + 7;
const int INF = 1 << 20;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;


int main()
{
    while(true){
        int n;
        int ans = 0;
        cin >> n; 
        if(n == 0){
            break;
        }
        int c = 1000 - n;
        if(c >= 500){
            c -= 500;
            ans++;
        }
        if(c >= 100){
            ans += c/100;
            c %= 100;
        }
        if(c >= 50){
            ans += c/50;
            c %= 50;
        }
        if(c >= 10){
            ans += c/10;
            c %= 10;
        }
        if(c >= 5){
            ans += c/5;
            c %= 5;
        }
        ans += c;
        cout << ans << endl;
    }
}