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>

int main() {
	
	while(1){
		int num,n=0,i=0,a=0,b=0;
		scanf("%d",&num);
		if(num==0) break;
		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 <bits/stdc++.h>
using namespace std;

int main() {
	int N;
	cin >> N;
	vector<int>A;
	A.push_back(500);
	A.push_back(100);
	A.push_back(50);
	A.push_back(10);
	A.push_back(5);
	A.push_back(1);
	while (N != 0) {
		int ans = 0;
		int num = 1000 - N;
		for (int i = 0; i < 6; i++) {
			while (num >= A.at(i)) {
				ans++;
				num -= A.at(i);
			}
		}
		cout << ans << endl;
		cin >> N;
	}
}

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

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

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

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

C++: #include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	char a[100000];
	int coin=0;
	int price=0;
	int flag=0;
	cin>>price;
	while(1){
		if(flag==0){
			break;
		}
		if(price>=500){
			price=price-500;
			coin ++;
		}
		else if(price>=100){
			price=price-100;
			coin++ ;
		}
		else if(price>=50){
			price=price-50;
			coin++ ;
		}
		else if(price>=10){
			price=price-10;
			coin++ ;
		}
		else if(price>=5){
			price=price-5;
			coin++ ;
		}
		else if(price>=1){
			price=price-1;
			coin++ ;
		}
		else if(price==0){
			flag=1;
		}
	}
	
	return 0;

}

C++: #include<iostream>
using namespace std;
int main(){
  int money;
  int change=0;
  cin >> money;
  money=1000-money;
  while(money){
    if(money>=500){
      change++;
      money-=500;
    }
    else if(money>=100){
      change++;
      money-=100;
    }
    else if(money>=50){
      change++;
      money-=50;
    }
    else if(money>=10){
      change++;
      money-=10;
    }
    else if(money>=5){
      change++;
      money-=5;
    }else{
      change++;
      money-=1;
    }
  }
  cout << change << endl;
}

C++: // https://onlinejudge.u-aizu.ac.jp/challenges/sources/JOI/Prelim/0521?year=2008

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;

    while (cin >> n, n) {
        n = 1000 - n;

        int a = n / 500;
        n %= 500;

        int b = n / 100;
        n %= 100;

        int c = n / 50;
        n %= 50;

        int d = n / 10;
        n %= 10;

        int e = n / 5;
        int f = n % 5;

        int ans = a + b + c + d + e + f;
        cout << ans << endl;
    }
    return 0;
}

/*
 * 
 */

C++: #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#include <random>

using namespace std;

//呪文
#define DUMPOUT cerr
#define dump(...) DUMPOUT<<"  ";DUMPOUT<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl;DUMPOUT<<"    ";dump_func(__VA_ARGS__)

typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef pair<string, string> pss;
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const pair<_KTy, _Ty>& m) { ostr << "{" << m.first << ", " << m.second << "}"; return ostr; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const map<_KTy, _Ty>& m) { if (m.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { ostr << ", " << *itr; } ostr << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const vector<_Ty>& v) { if (v.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { ostr << ", " << *itr; }	ostr << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const set<_Ty>& s) { if (s.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { ostr << ", " << *itr; }	ostr << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const stack<_Ty>& s) { if (s.empty()) { ostr << "{ }"; return ostr; } stack<_Ty> t(s); ostr << "{" << t.top(); t.pop(); while (!t.empty()) { ostr << ", " << t.top(); t.pop(); } ostr << "}";	return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const list<_Ty>& l) { if (l.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << l.front(); for (auto itr = ++l.begin(); itr != l.end(); ++itr) { ostr << ", " << *itr; } ostr << "}"; return ostr; }
template <typename _KTy, typename _Ty> istream& operator >> (istream& istr, pair<_KTy, _Ty>& m) { istr >> m.first >> m.second; return istr; }
template <typename _Ty> istream& operator >> (istream& istr, vector<_Ty>& v) { for (size_t i = 0; i < v.size(); i++) istr >> v[i]; return istr; }
namespace aux { // print tuple
	template<typename Ty, unsigned N, unsigned L> struct tp { static void print(ostream& os, const Ty& v) { os << get<N>(v) << ", "; tp<Ty, N + 1, L>::print(os, v); } };
	template<typename Ty, unsigned N> struct tp<Ty, N, N> { static void print(ostream& os, const Ty& value) { os << get<N>(value); } };
}
template<typename... Tys> ostream& operator<<(ostream& os, const tuple<Tys...>& t) { os << "{"; aux::tp<tuple<Tys...>, 0, sizeof...(Tys)-1>::print(os, t); os << "}"; return os; }

template<typename A, size_t N, typename T> inline void Fill(A(&array)[N], const T &val) { std::fill((T*)array, (T*)(array + N), val); }

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); }

#define PI 3.14159265358979323846
#define EPS 1e-11
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define fake false



int main()
{
	//clock_t start, end;
	//start = clock();

	cin.tie(0);
	ios::sync_with_stdio(false);

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

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

		int x = 1000 - N;
		int ans = 0;
		for (int i = 0; i < 6; i++) {
			ans += x / c[i];
			x %= c[i];
		}
		cout << ans << endl;
	}

	//end = clock();
	//printf("%d msec.\n", end - start);

	return 0;
}