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<cstdio>
#include<iostream>
int main(){
    while(1){
        int x;
        int coin,n=0;
        scanf("%d",&x);
        if(x==0)
            break;
        x=1000-x;
        for(int i=0;i<6;i++){
            if(i==0)
                coin=500;
            else if(i==1)
                coin=100;
            else if(i==2)
                coin=50;
            else if(i==3)
                coin=10;
            else if(i==4)
                coin=5;
            else
                coin=1;
            for(;x>=coin;){
                x-=coin;
                n++;
            }
        }
        printf("%d\n",n);
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<functional>
#include<cstring>
using namespace std;
int main() {
	int n;
	while(scanf("%d",&n),n){
		n = 1000 - n;
		int cnt = 0;
		if(500 <= n){
			n -= 500;
			cnt++;
		}
		if(100 <= n){
			cnt += n / 100;
			n %= 100;
		}
		if(50 <= n){
			cnt += n / 50;
			n %= 50;
		}
		if(10 <= n){
			cnt += n / 10;
			n %= 10;
		}
		if(5 <= n){
			cnt += n / 5;
			n %= 5;
		}
		cnt += n;
		printf("%d\n",cnt);
	}
    return 0;
}

C++: #include <iostream>
using namespace std;

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

int main() {

	int change, coin;
	cin >> change;
	change = 1000 - change;

	coin = 0;
	for (int i = 0; i < 6; ++i) {
		coin += change / coins[i];
		change %= coins[i];
		if (!change)
			break;
	}
	cout << coin << endl;

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
int s=0;
int a[]={500,100,50,10,5,1};
int b;
while(scanf("%d",&b),b){
	int c=1000-b;
	for(int d=0;d<6;d++){
		int e=c/a[d];
		s+=e;
		b-=e*a[d];
	}
	printf("%d\n",s);
}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cctype>
#define rep(i,a,b) for(int i=(a);i<b;i++)
#define INF 1000000000
#define MAX_N 105
using namespace std;
bool debug=false;

int main(){
  int n;
  int coin[6]={500,100,50,10,5,1};
  while(cin>>n){
    if(n==0)break;
    n=1000-n;
    int cnt=0;
    for(int i=0;i<6;){
      if(n>=coin[i]){
	n-=coin[i];
	cnt++;
	//cout<<coin[i]<<endl;
      }else{
	i++;
      }
    }
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>

using namespace std;

int main(){
	/*
	//数あて
	int n;
	cin >> n;
	int p[200][3];

	for(int i=0;i<n;i++)
	{
		cin >> p[i][0] >> p[i][1] >> p[i][2];
	}

	int _p;
	for(int k=0;k<3;k++)
	{
		for(int i=0;i<n;i++)
		{
			_p = p[i][k];
			for(int j=i+1;j<n;j++)
			{
				if(_p == p[j][k])
				{
					p[i][k] = 0;
					p[j][k] = 0;
				}
			}
		}
	}

	for(int i=0;i<n;i++)
	{
		cout << p[i][0] + p[i][1] + p[i][2] << endl;
	}
	*/

	/*
	//得点
	int ai,am,as,ae,bi,bm,bs,be;
	cin >> ai >> am >> as >> ae >> bi >> bm >> bs >> be;
	int answer=ai+am+as+ae;
	if(answer < bi+bm+bs+be)
	{
		answer=bi+bm+bs+be;
	}
	cout << answer << endl;
	*/
	
	//おつり
	int onum;
while(true){
		int N;
		cin >> N;
		if(N == 0) break;
		onum=0;
		int oturi = 1000-N;
		if(oturi >= 500)
		{
			onum++;
			oturi -= 500;
		}
		while(oturi>=100)
		{
			onum++;
			oturi -= 100;
		}
		if(oturi >= 50)
		{
			onum++;
			oturi -= 50;
		}
		while(oturi>=10)
		{
			onum++;
			oturi -= 10;
		}
		if(oturi >= 5)
		{
			onum++;
			oturi -= 5;
		}
		while(oturi>=1)
		{
			onum++;
			oturi -= 1;
		}
		 cout << onum << endl;
	}
   
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int,int> P;
typedef long long ll;

int main(){

	while(1){
		int a; cin >> a;
		if(a == 0) return 0;
		a = 1000 - a;

		int ans = 0;

		ans += a/500;
		a %= 500;
		ans += a/100;
		a %= 100;
		ans += a/50;
		a %= 50;
		ans += a/10;
		a %= 10;
		ans += a/5;
		a %= 5;
		ans += a;

		cout << ans << endl;
	}

}

C++: #include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

int main(){

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

	return 0;
}

C++: #include <iostream>
#include <cstdio>

int main()
{
	int x;
	int i;

	

	while (1) {
		scanf("%d", &x);
		if (x == 0) break; 
		x = 1000 - x;
		i = 0;
			 
	while (x >= 500 ) {
		i++;
		x -= 500;
	}
	while (x >= 100 ) {
		i++;
		x -= 100;
	}
	while (x >= 50 ) {
		i++;
		x -= 50;
	}
	while (x >= 10 ) {
		i++;
		x -= 10;
	}
	while (x >= 5 ) {
		i++;
		x -= 5;
	}
	while (x >= 1 ) {
		i++;
		x -= 1;
	}
	printf("%d\n", i);
	}
	
	return (0);
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    while(cin >> n, n)
    {
        int v = 1000 - n;
        int coins[] = {500, 100, 50, 10, 5, 1};
        int pos = 0, ans = 0;
        while(v > 0)
        {
            while(v - coins[pos] < 0) pos++;
            v -= coins[pos];
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
int price,cnt=0,div[6]={500,100,50,10,5,1},i;
while(1){
cin>>price;
if(price==0)break;
price=1000-price;
for(i=0;i<6;i++){
if((price/div[i])!=0){
cnt+=(price/div[i]);
price-=(price/div[i])*div[i];
}
}
cout<<cnt<<endl;
cnt=0;
}
return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void){
  int n,ans;
  int m[6]={500,100,50,10,5,1};
  while(1){
    scanf("%d",&n);
    if(n==0) break;
    ans=0;
    n=1000-n;
    for(int i=0;i<6;i++){
      ans+=n/m[i];
      n%=m[i];
    }
    printf("%d\n",ans);
  }
}