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

int main(void)
{
    int ans;
    int change;
    int money;
    
    while (1){
        scanf("%d", &money);
        if (money == 0) break;
        
        ans = 0;
        change = 1000 - money;
        while (change != 0){
            if (500 <= change){
                change -= 500;
                ans++;
                continue;
            }
            if (100 <= change){
                change -= 100;
                ans++;
                continue;
            }
            if (50 <= change){
                change -= 50;
                ans++;
                continue;
            }
            if (10 <= change){
                change -= 10;
                ans++;
                continue;
            }
            if (5 <= change){
                change -= 5;
                ans++;
                continue;
            }
            if (1 <= change){
                change -= 1;
                ans++;
                continue;
            }
        }
        printf("%d\n", ans);
    }
    
    return (0);
}

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

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

int main(){
	for(;;){
		int N;
		cin >> N;
		if(N == 0) break;
		N = 1000 - N;
		int res = 0;
		for(int i = 0; i < 6; i++){
			int num = N / c[i];
			res += num;
			N -= c[i] * num;
		}
		cout << res << endl;
	}
	return 0;
}

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

C++: 

#include <iostream>
using namespace std;
int main(){
	while(true){

	int n,m;
	cin >> n;
	if(n == 0) return 0;
	m = 0;
	n = 1000-n;

	if(n>=500){
		m = n/500;
		n = n%500;
	}
	if(n>=100){
		m = m+n/100;
		n = n%100;
	}
	if(n>=50){
		m = m+n/50;
		n = n%50;
	}
	if(n>=10){
		m = m+n/10;
		n = n%10;
	}
	if(n>=5){
		m = m+n/5;
		n = n%5;
	}
	if(n>=1){
		m = m+n/1;
	}

	cout << m << endl;

	}

}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const ll mod=998244353,MAX=200001,INF=1<<30;

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

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

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

C++: #include <iostream>

using namespace std;

void solve()
{
	int p;
	while(cin >> p, p)
	{
		p = 1000 - p;
		int ans = 0;
		if(p >= 500)
		{
			p -= 500;
			++ans;
		}
		if(p >= 100)
		{
			int k = p / 100;
			p -= k * 100;
			ans += k;
		}
		if(p >= 50)
		{
			p -= 50;
			++ans;
		}
		if(p >= 10)
		{
			int k = p / 10;
			p -= k * 10;
			ans += k;
		}
		if(p >= 5)
		{
			p -= 5;
			++ans;
		}
		if(p >= 1)
		{
			ans += p;
		}
		cout << ans << endl;
	}
}

int main()
{
	solve();
	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 <iostream>

using namespace std;

int coin;

int main()
{
	int money = 1;
	int remoney;
	
	
	while (1){
	cin >> money;
	if (money == 0) break;
	remoney = 1000 - money;
	
	if ( remoney >= 500){
		coin++;
		remoney -= 500;
	}
	coin += remoney / 100;
	remoney %= 100;
	
	coin += remoney / 50;
	remoney %= 50;
	
	coin += remoney / 10;
	remoney %= 10;
	
	coin += remoney / 5;
	remoney %= 5;
	
	coin += remoney;
	
	cout << coin << endl;
	coin = 0;
	}
	return (0);
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
using namespace std;

//common

typedef int  i32;
typedef long long i64,ll;
typedef long double ld;

#define BR "\n"
#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,l,r) for(int i=(int)l;i<(int)(r);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

//config
//#define NDEBUG
//#define INF 1<<30
//#define EPS 1e-8
//const ll MOD =100000007;

//debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp,act)
#define STOP(e)
#else
#define DUMP(x)  cerr << #x << " = " << (x)
#define DUMPLN(x)  DUMP(x) <<endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x)<<endl
#define LINE()    cerr<< " (L" << __LINE__ << ")"
#define LINELN()    LINE()<<endl
#define CHECK(exp,act)  if(exp!=act){DUMPLN(exp);DEBUGLN(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}

template<class T> inline string toString(const vector<vector<T> >& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

string BITtoString(int bit){
    stringstream ss;
    while(bit!=0){ss<<(bit%2);bit/=2;}
    string res=ss.str();reverse(ALL(res));
    return res;
}

template<typename T> inline T pmod(T v,T MOD){
	return (v%MOD+MOD)%MOD;
}
#define nextInt(n) scanf("%d",&n)
#define defInt(n) int n;nextInt(n)
#define nextLong(n) scanf("%lld",&n)
#define defLong(n) ll n;nextLong(n)
#define nextDouble(n) scanf("%lf",&n)

class Main{
public:

        int N;
        bool input(){
            //   ifstream cin("in");
            //  ofstream cout( "out" );
            nextInt(N);
            return N!=0;
        }

        void run(){
            while(input()){
            	N=1000-N;
            	ll res=0;
            	res+=N/500;N%=500;
            	res+=N/100;N%=100;
            	res+=N/50;N%=50;
            	res+=N/10;N%=10;
            	res+=N/5;N%=5;
            	res+=N/1;N%=1;

            	cout << res<<endl;
            }
        }
};

int main(){
	//cout <<fixed<<setprecision(3);	
	//ios::sync_with_stdio(false);
	Main().run();
	return 0;
}