Go: package main

import "fmt"

func main() {
	var n, coins, change int
	for {
		fmt.Scan(&n)
		if n == 0 {
			return
		}
		coins = 0
		change = 1000 - n
		for change > 0 {
			coins++
			switch {
			case change >= 500:
				change -= 500
			case change < 500 && change >= 100:
				change -= 100
			case change < 100 && change >= 50:
				change -= 50
			case change < 50 && change >= 10:
				change -= 10
			case change < 10 && change >= 5:
				change -= 5
			default:
				change--
			}
		}
		fmt.Println(coins)
	}
}

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[5];
	int i = 0;
	while(true){
		int N;
		cin >> N;
		if(N == 0) break;
				
		int oturi = 1000-N;
		if(oturi >= 500)
		{
			onum[i]++;
			oturi -= 500;
		}
		while(oturi>=100)
		{
			onum[i]++;
			oturi -= 100;
		}
		if(oturi >= 50)
		{
			onum[i]++;
			oturi -= 50;
		}
		while(oturi>=10)
		{
			onum[i]++;
			oturi -= 10;
		}
		if(oturi >= 5)
		{
			onum[i]++;
			oturi -= 5;
		}
		while(oturi>=1)
		{
			onum[i]++;
			oturi -= 1;
		}
		
		i++;
	}
    cout << onum[0] << endl;
	cout << onum[1] << endl;
	cout << onum[2] << endl;
	cout << onum[3] << endl;
	cout << onum[4] << endl;
	return 0;
}

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

int main()
{

	while(true){

		int t;
		int count = 0;

		cin >> t;
		if(t==0)
			break;

		t = 1000-t;

		count += t/500;
		t = t-500*(t/500);

		count += t/100;
		t = t-100*(t/100);

		count += t/50;
		t = t-50*(t/50);

		count += t/10;
		t = t-10*(t/10);

		count += t/5;
		t = t-5*(t/5);

		count += t;

		cout << count << endl;
	}
	return 0;
}

C++: #include <stdio.h>

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

int main(void) {
	while (true) {
		scanf("%d", &N);
		if (N == 0) { break; }

		int res = 1000 - N;
		while (res != 0) {
			if (res >= 500) {
				ans += res / 500;
				res -= 500 * (res / 500);
			}
			else if (res >= 100) {
				ans += res / 100;
				res -= 100 * (res / 100);
			}
			else if (res >= 50) {
				ans += res / 50;
				res -= 50 * (res / 50);
			}
			else if (res >= 10) {
				ans += res / 10;
				res -= 10 * (res / 10);
			}
			else if (res >= 5) {
				ans += res / 5;
				res -= 5 * (res / 5);
			}
			else { ans += res; res = 0; }
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T T    cums(T& v){ T r(v.size()); partial_sum(v.begin(), v.end(), r); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    acum(vector<T>& v, T b=0){ return accumulate(v.begin(), v.end(), b); }
TEMP_T vector<T> inpt(int n){ vector<T> v(n); for (int i=0; i<n; i++) cin >> v[i]; return v; }

static inline int in(){ int x; scanf("%d", &x); return x; }

struct Solver {
  Solver(){}
  int solve(){
  }
};

int main()
{
  int n;
  while (n = in()){
    n = 1000 - n;
    int c = 0;
    for (int i = 0; i < 6; i++){
      static const int c[] = {500, 100, 50, 10, 5, 1};
      c += n / c[i];
      n %= c[i];
    }
    printf("%d\n", c);
  }

  return (0);
}

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

int main() {
  int payment;
  while (1) {
    cin >> payment;
    if (payment == 0) break;

    int change = 1000 - payment;
    int ans = 0;
    ans += change / 500; change %= 500;
    ans += change / 100; change %= 100;
    ans += change / 50; change %= 50;
    ans += change / 10; change %= 10;
    ans += change / 5; change %= 5;
    ans += change;

    cout << ans << endl;
  }

  return 0;
}

C++: #include<cstdio>
using namespace std;
int main(void){
   while(1){
      int n;
      scanf("%d",&n);
      if(n == 0) break;
      n = 1000 - n;
      int cnt = 0;
      while(1){
         if(n >= 500){
            n -= 500;
            cnt++;
         }
         else if(n >= 100){
            n -= 100;
            cnt++;
         }
         else if(n >= 50){
            n -= 50;
            cnt++;
         }
         else if(n >= 10){
            n -= 10;
            cnt++;
         }
         else if(n >= 5){
            n -= 5;
            cnt++;
         }
         else if(n >= 1){
            n -= 1;
            cnt++;
         }
         else break;
      }
      printf("%d\n",cnt);
   }
   return 0;
}

C++: #include <iostream>

using namespace std;

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

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
template<typename V> constexpr typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int x;
  while (cin >> x, x) {
    x     = 1000 - x;
    int c = 0;
    vi v  = {500, 100, 50, 10, 5, 1};
    rep(i, v.size()) {
      c += x / v[i];
      x %= v[i];
    }
    cout << c << endl;
  }
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;

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