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>
#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;
}

C++: #include<stdio.h>
int main(void)
{
	int gohyaku,hyaku,gozyu,zyu,go,ichi;
	int dai,otsuri,mai;
	while(scanf("%d",&dai)!=EOF){
		otsuri=1000-dai;
		gohyaku=0;
		hyaku=0;
		gozyu=0;
		zyu=0;
		go=0;
		ichi=0;
		while(otsuri>=500){
			otsuri=otsuri-500;
				gohyaku++;
		}
		while(otsuri>=100){
			otsuri=otsuri-100;
				hyaku++;
		}
		while(otsuri>=50){
			otsuri=otsuri-50;
				gozyu++;
		}
		while(otsuri>=10){
			otsuri=otsuri-10;
				zyu++;
		}
		while(otsuri>=5){
			otsuri=otsuri-5;
				go++;
		}
		while(otsuri>=1){
			otsuri=otsuri-1;
				ichi++;
		}
		mai=gohyaku+hyaku+gozyu+zyu+go+ichi;
		printf("%d\n",mai);
	}
	return 0;
}

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

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

int coin[] = { 500, 100, 50, 10, 5, 1 };
int main() {
  int n,res;
  while(true) {
    cin>>n;
    if(!n) break;
    n=1000-n,res=0;
    for(int i=0; i<6; i++) {
      res+=n/coin[i];
      n%=coin[i];
    }
    cout<<res<<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;
		}

		price = 1000 - price;

		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<cstdio>

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

int solve(){
  int x,ans = 0;
  scanf("%d",&x);
  if(x==0) return 1;
  x = 1000-x;
  for(int i=0;i<6;i++){
    ans+=x/c[i];
    x%=c[i];
  }
  printf("%d\n",ans);
  return 0;
}

int main(){
  while(true) if(solve()) return 0;
}

C++: //============================================================================
// Name        : 0521.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {
	int x;
	int c[6]={
		500,100,50,10,5,1
	};
	while(1){
		int ans=0;
	cin>>x;
	x=1000-x;
	if(x==1000){
		break;
	}
	for(int i=0;i<6;i++){
		ans=x/c[i]+ans;
		x=x%c[i];

	}
	cout <<ans<<endl;
	}
	return 0;
}

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

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);   

    int n;
    while(cin >> n){
        if(!n) break;

        int m = 1000 - n;
        int ans = 0;

        rep(i,6){
            ans += m/coin[i];
            m = m%coin[i];
        }

        cout << ans << endl;
    }

    return 0;
}

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

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

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

	int price;
	cin >> price;

	int change = 1000 - price;
	int ans = 0;

	for(int i = 0; i < 6; ++i) {
		while(change >= coins[i]) {
			change -= coins[i];
			++ans;
		}
	}

	cout << ans << endl;		

	return EXIT_SUCCESS;
}

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

using ll = long long;
#define pq priority_queue
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

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

   return 0;
}