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 <iostream>
using namespace std;

int main() {
	while (1) {
		int all = 0 , rem = 0, count = 0, tmp  = 0;
		cin >> all;
		if (all == 0)
			break;
		rem = 1000 - all;
		if (rem >= 500) {
			count = count + 1;
			rem = rem - 500;
		}
		if (rem >= 100) {
			tmp = rem / 100;
			count = count + tmp;
			rem = rem - (100 * tmp);
		}
		if (rem >= 50) {
			tmp = rem / 50;
			count = count + tmp;
			rem = rem - 50 * tmp;
		}
		if (rem >= 10) {
			tmp = rem / 10;
			count = count + tmp;
			rem = rem - 10 * tmp;
		}
		if (rem >= 5) {
			tmp = rem / 5;
			count = count + tmp;
			rem = rem - 5 * tmp;
		}
		count = count + rem;
		cout << count << endl;
	}
	return 0;
}

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

int a,sum,ans,dx[6]={500,100,50,10,5,1};

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

    for(int i=0;i<6;i++){
    ans=a/dx[i];
    sum=sum+ans;
    a=a%dx[i];
    ans=0;
    }

    cout << sum << endl;
    sum=0;
    }
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main(void){
	int n;
	int i;
	int a[5];
	for(i = 0; i < 5; i++){
		scanf("%d",&n);
		if(n == 0) break;
			a[i] = n;
	}
	
	for(int j = 0; j < i; j++){
		int val = 1000 - a[j];
		int count = 0;
		count += val / 500;
		val -= 500 * (val / 500);
		count += val / 100;
		val -= 100 * (val / 100);
		count += val / 50;
		val -= 50 * (val / 50);
		count += val / 10;
		val -= 10 * (val / 10);
		count += val / 5;
		val -= 5 * (val / 5);
		count += val;
		
		printf("%d\n", count);
	}
	return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>


int main(void) {
	int num,n,i,a,b;
	a=0,b=0;
	scanf("%d",&num);
	n=1000-num;

	if (n>500) {
		i++;
		n=(n-500);
	}
	if (n>100){
		a=n/100;
		i=a+i;
		n=n%100;
	}
	if (n>50){
		i++;
		n=(n-50);
	}
	if (n>10){
		b=n/10;
		i=b+i;
		n=n%10;
	}
	if (n>5){
		i++;
		n=(n-5);
	}
		i=(i+n);
	printf("%d\n",i);
	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;
	int i = 0;
	while(true){
		int N;
		cin >> N;
		if(N == 0) break;
				
		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;
		}
		
		i++;
	}
    cout << onum[0] << endl;
	
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int n;cin>>n,n;){
		n=1000-n;
		int coins[]={500,100,50,10,5,1};
		int res=0;
		rep(i,6){
			res+=n/coins[i];
			n%=coins[i];
		}
		cout<<res<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(void){
    while(1){
        int n;
        cin>>n;
        if(n=='\0')break;
        int d[6]={500,100,50,10,5,1};
        int A=1000-n;
        int ans=0;
        for(int i=0;i<6;i++){
            int x=A/d[i];
            A-=x*d[i];
            ans+=x;
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

int main()
{
	int x;
	int a[6] = { 500, 100, 50, 10, 5, 1 };

	cin >> x;

	while (x > 0) {
		int n = 1000 - x, m = n / 500;

		for(int i = 0; i < 5; i++)
			m += (n % a[i]) / a[i + 1];

		cout << m << endl;

		cin >> x;
	}

	return 0;
}

C++: #include <cstdio>

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

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

#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

int main(void) {
	int temp = 0;
	int cost_input[10];
	int cost_return = 0;
	int num = 0;
	int j = 0;

	for (int i = 0; i < 10; ++i) {
		CI temp;
		if (temp == 0) {
			j = i;
			break;
		}
		cost_input[i] = temp;

		cost_return = 1000 - cost_input[i];
		num = 0;

		if (cost_return < 5) {
			num = cost_return;
			break;
		}
		else {
			if (cost_return < 10) {
				num = 1 + cost_return - 5;
				break;
			}
		}


		if (cost_return >= 500) {
			++num;
			cost_return -= 500;
		}
		while (1) {
			if (cost_return >= 100) {
				++num;
				cost_return -= 100;
			}
			if (cost_return < 100)break;
		}
		if (cost_return >= 50) {
			++num;
			cost_return -= 50;
		}
		while (1) {
			if (cost_return >= 10) {
				++num;
				cost_return -= 10;
				if (cost_return < 10)break;
			}
		}

		if (cost_return >= 5) {
			++num;
			cost_return -= 5;
		}
		while (1) {
			if (cost_return >= 1) {
				++num;
				cost_return -= 1;
			}
			if (cost_return < 1)break;
		}
	
	}
	

	for (int i = 0; i < j;++i) {
		CO num E
	}
}