Go: ///
// File:  0521.go
// Author: ymiyamoto
//
// Created on Wed Mar  7 22:11:28 2018
//
package main

import (
	"fmt"
)

var money int

func main() {
	for {
		fmt.Scan(&money)
		if money == 0 {
			break
		}
		rem := 1000 - money

		count := 0
		for _, coin := range []int{500, 100, 50, 10, 5, 1} {
			count += rem / coin
			rem %= coin
		}
		fmt.Println(count)
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int money=0,money1=0,all=0;
	while(1){
	cin>>money;
	if(money==0){break;}
	money1=1000-money;
	all+=(money1/500);
	all+=(money1%500/100);
	all+=(money1%500%100/50);
	all+=(money1%500%100%50/10);
	all+=(money1%500%100%50%10/5);
	all+=(money1%500%100%50%10%5/1);
	cout<<all<<endl;
	all=0;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n,k;
	cin>>k;
	n=k/500+n%500/100+n%500%100/50+n%500%100%50/10+n%500%100%50%10/5+n%500%100%50%10%5;
	cout<<n<<endl;
	return 0;
}

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

int main(){
	int okane;
	int oturi;
	int count = 0;
	int a[] = { 1, 5, 10, 50, 100, 500 };

	scanf("%d", &okane);
	oturi = 1000 - okane;

	for (int i = 5; i >= 0; i--){
		if (oturi >= a[i]){
			div_t d;

			d = div(oturi, a[i]);
			oturi = oturi - a[i] * d.quot;
			count = count + d.quot;
		}
	}
	printf("%d\n",count);

	return 0;

	div(20, 3);

}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n;

int main(){
  while(scanf("%d",&n) && n){
    n = 1000 - n;
    printf("%d\n", n/500 + (n%500)/100 + (n%100)/50 + (n%50)/10 + (n%10)/5 + (n%5));
  }
  return 0;
}

C++: // 2011/11/21 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){

	int n;
	cin >> n;

	if(n==0)
		break;

	int m = 1000-n;

	int sum = 0;

	sum += m/500;
	m %= 500;

	sum += m/100;
	m %= 100;

	sum += m/50;
	m %= 50;

	sum += m/10;
	m %= 10;

	sum += m/5;
	m %= 5;

	sum += m;

	cout << sum << endl;

	}

	return 0;
}

C++: #include <stdio.h>

int main(){
	int okane;
	int oturi;
	int count = 0;
	int a[] = { 1, 5, 10, 50, 100, 500 };

	scanf_s("%d", &okane);
	oturi = 1000 - okane;

	for (int i = 5; i >= 0; i--){
		if (okane >= a[i]){
			okane = okane - a[i];
			count = count + 1;
		}
	}
	printf("%d\n",count);
	return 0;

}

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

int main() 
{
	while (1) {
		int a;
		cin >> a;
		if (a == 0)break;
		int b = 1000 - a;
		int c = 0;
		c += b / 500;
		b -= b / 500 * 500;
		c += b / 100;
		b -= b / 100 * 100;
		c += b / 50;
		b -= b / 50 * 50;
		c += b / 10;
		b -= b / 10 * 10;
		c += b / 5;
		b -= b / 5 * 5;
		c += b;
		cout << c << endl;
	}
}

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

int main() {
	int a,b=0;
	while(1){
		cin >> a;
		if(a==0) break;
		a=1000-a;
		if(a>=500){
			a-=500;
			b++;
		}

		while(1){
			if(a<100) break;
			a-=100;
			b++;
		}

		if(a>=50){
			a-=50;
			b++;
		}

		while(1){
			if(a<10) break;
			a-=10;
			b++;
		}

		if(a>=5){
			a-=5;
			b++;
		}

		while(1){
			if(a==0) break;
			a-=1;
			b++;
		}
		cout <<b << endl;
		b=0;
	}
	return 0;
}

C++: #include <cstdio>

int n, ans;
int coin[6] = {500,100,50,10,5,1};

int main(){
	while(scanf("%d", &n),n){
		ans = 0;
		n = 1000-n;
		for(int i = 0; i < 6; i++){ 
			ans += n/coin[i]; 
			n %= coin[i]; 
		}
		printf("%d\n", ans);
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;

int main() {
	int a;
	while (scanf("%d", &a), a) {
		int coin[] = { 500,100,50,10,5,1 };
		a = 1000 - a;
		int cnt = 0;
		rep(i, 6) {
			while (a - coin[i] >= 0) {
				a -= coin[i];
				cnt++;
			}
			if (a == 0)break;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

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

int main() {
    while (true) {
        int cost; cin >> cost; if (cost == 0) break;

        int remain = 1000 - cost;
        const int n500 = remain / 500; remain -= 500 * n500;
        const int n100 = remain / 100; remain -= 100 * n100;
        const int n50  = remain / 50;  remain -= 50  * n50;
        const int n10  = remain / 10;  remain -= 10  * n10;
        const int n5   = remain / 5;   remain -= 5   * n5;
        const int n1   = remain;
        
        cout << n500 + n100 + n50 + n10 + n5 + n1 << endl;
    }
}

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

int main(){
int buy,oturi,kouka;
cin>>buy;
if(!buy==0){

oturi=1000-buy;
if(oturi>500){
oturi-=500;
kouka+=1;
}
while(oturi>=100){
oturi-=100;
kouka+=1;
}
while(oturi>=50){
oturi-=50;
kouka+=1;
}
while(oturi>=10){
oturi-=10;
kouka+=1;
}
while(oturi>=5){
oturi-=5;
kouka+=1;
}
while(oturi>=1){
oturi-=1;
kouka+=1;
}
cout<<kouka<<'\n';
}
return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	int n;
	int x=0;
	int s[6]={500,100,50,10,5,1};
	cin>>n;
	
	n=1000-n;
	int k=0;
	while(n>0){
		if(n-s[k]>=0){
			n=n-s[k];
			x++;
		}
		else
			k++;
	}
	
	cout<<x<<endl;
	

}

C++: #include<iostream>
#include<algorithm>
#include<cstdlib>

using namespace std;

int main() {
	while (true) {
		int coin = 0;
		int price = 0;

		cin >> price;
		if (price == 0) {
			break;
		}

		for (auto temp : { 500, 100, 50, 10, 5, 1 }) {
			const auto temp_div = div(price, temp);
			coin += temp_div.quot;
			price = temp_div.rem;
		}

		cout << coin << 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;
}