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 <functional>
 #include <algorithm>
 #include <iostream>
 #include <numeric>
 #include <iomanip>
 #include <utility>
 #include <cstdlib>
 #include <sstream>
 #include <bitset>
 #include <vector>
 #include <cstdio>
 #include <ctime>
 #include <queue>
 #include <deque>
 #include <cmath>
 #include <stack>
 #include <list>
 #include <map>
 #include <set>
 
 using namespace std;
 
 typedef vector<int> vi;
 typedef pair<int,int> pii;
 typedef long long ll;
 
 #define dump(x)  cerr << #x << " = " << (x) << endl
 #define rep(i,n) for(int i=0;i<(n);i++)
 #define all(a)  (a).begin(),(a).end()
 #define pb push_back
 #define sz size()
 
 const double EPS = 1e-10;
 const int INF = 100000000;
 
 int solve(int n){
 int ret=0;
 int m[]={500,100,50,10,5,1};
 rep(i,6){
 ret+=n/m[i];
 n-=m[i]*(n/m[i]);
 }
 return ret;
 }
 
 
 int main(){
 vi a,b;
 rep(j,10){
 int k;
 cin>>k;
 a.pb(k);
 }
 rep(j,10){
 int k;
 cin>>k;
 b.pb(k);
 }
 sort(all(a));
 sort(all(b));
 
 cout<<(a[9]+a[8]+a[7])<<" "<<(b[9]+b[8]+b[7])<<endl;
 
 return 0;
 }

C++: //
//  main.cpp

#include <iostream>
using namespace std;


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

C++: #include <iostream>
using namespace std;
int main(){
  int n=1000 ,m ,coin=0;
  while( true ){
    cin >> m;

    if( m == 0 ){
      break;
    }
    n -= m;
    if( n >= 500 ){
      coin += n / 500;
      n %= 500;
    }
    
    if( n >= 100 ){
      coin += n / 100;
      n %= 100;
    } 
    
    if( n >= 50 ){
      coin += n / 50;
      n %= 50;
    } 
    
    if( n >= 10 ){
      coin += n / 10;
      n %= 10;
    } 
    
    if( n >= 5 ){
    coin += n / 5;
    n %= 5;
    }
    
    if( n >= 1 ){
      coin += n;
    }
  
    cout << coin << endl;
    n = 1000;
    coin = 0;
  }
}

C++: #include<cstdio>
#include<iostream>
int main(){
    while(1){
        int x;
        int coin,n=0;
        scanf("%d",&x);
        if(x==0)
            break;
        x=1000-x;
        for(int i=0;i<6;i++){
            if(i==0)
                coin=500;
            else if(i==1)
                coin=100;
            else if(i==2)
                coin=50;
            else if(i==3)
                coin=10;
            else if(i==4)
                coin=5;
            else
                coin=1;
            for(;x>=coin;){
                x-=coin;
                n++;
            }
        }
        printf("%d\n",n);
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<functional>
#include<cstring>
using namespace std;
int main() {
	int n;
	while(scanf("%d",&n),n){
		n = 1000 - n;
		int cnt = 0;
		if(500 <= n){
			n -= 500;
			cnt++;
		}
		if(100 <= n){
			cnt += n / 100;
			n %= 100;
		}
		if(50 <= n){
			cnt += n / 50;
			n %= 50;
		}
		if(10 <= n){
			cnt += n / 10;
			n %= 10;
		}
		if(5 <= n){
			cnt += n / 5;
			n %= 5;
		}
		cnt += n;
		printf("%d\n",cnt);
	}
    return 0;
}

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

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

int main() {

	int change, coin;
	cin >> change;
	change = 1000 - change;

	coin = 0;
	for (int i = 0; i < 6; ++i) {
		coin += change / coins[i];
		change %= coins[i];
		if (!change)
			break;
	}
	cout << coin << endl;

	return 0;
}

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

int main(){
int s=0;
int a[]={500,100,50,10,5,1};
int b;
while(scanf("%d",&b),b){
	int c=1000-b;
	for(int d=0;d<6;d++){
		int e=c/a[d];
		s+=e;
		b-=e*a[d];
	}
	printf("%d\n",s);
}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cctype>
#define rep(i,a,b) for(int i=(a);i<b;i++)
#define INF 1000000000
#define MAX_N 105
using namespace std;
bool debug=false;

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

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

C++: #include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int,int> P;
typedef long long ll;

int main(){

	while(1){
		int a; cin >> a;
		if(a == 0) return 0;
		a = 1000 - a;

		int ans = 0;

		ans += a/500;
		a %= 500;
		ans += a/100;
		a %= 100;
		ans += a/50;
		a %= 50;
		ans += a/10;
		a %= 10;
		ans += a/5;
		a %= 5;
		ans += a;

		cout << ans << endl;
	}

}

C++: #include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
//#include "h.h"
using namespace std;

#define ll long long
#define pint pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define pm(first, second) pb(mp(first, second))
#define SPACE " "
#define fpf first.first
#define fps first.second
#define spf second.first
#define sps second.second

#define all(X) (X).begin(), (X).end()

int main(){

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

	return 0;
}

C++: #include <iostream>
#include <cstdio>

int main()
{
	int x;
	int i;

	

	while (1) {
		scanf("%d", &x);
		if (x == 0) break; 
		x = 1000 - x;
		i = 0;
			 
	while (x >= 500 ) {
		i++;
		x -= 500;
	}
	while (x >= 100 ) {
		i++;
		x -= 100;
	}
	while (x >= 50 ) {
		i++;
		x -= 50;
	}
	while (x >= 10 ) {
		i++;
		x -= 10;
	}
	while (x >= 5 ) {
		i++;
		x -= 5;
	}
	while (x >= 1 ) {
		i++;
		x -= 1;
	}
	printf("%d\n", i);
	}
	
	return (0);
}