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<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;
	int i = 0;
	while(true){
		int N;
		cin >> N;
		if(N == 0) break;
				
		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;
		}
		
		i++;
	}
    cout << onum[0] << endl;
	
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

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

C++: #include <iostream>
using namespace std;
int main(void){
    while(1){
        int n;
        cin>>n;
        if(n=='\0')break;
        int d[6]={500,100,50,10,5,1};
        int A=1000-n;
        int ans=0;
        for(int i=0;i<6;i++){
            int x=A/d[i];
            A-=x*d[i];
            ans+=x;
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

int main()
{
	int x;
	int a[6] = { 500, 100, 50, 10, 5, 1 };

	cin >> x;

	while (x > 0) {
		int n = 1000 - x, m = n / 500;

		for(int i = 0; i < 5; i++)
			m += (n % a[i]) / a[i + 1];

		cout << m << endl;

		cin >> x;
	}

	return 0;
}

C++: #include <cstdio>

int main(){
	int n;
	int a[6] = {500,100,50,10,5,1};
	while(scanf("%d",&n), n!= 0){
		int t = 0;
		n = 1000 - n;
		for(int i = 0; i < 6; i++){
			t += n / a[i];
			n %= a[i];
		}
		printf("%d\n",t);
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>

#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

int main(void) {
	int temp = 0;
	int cost_input[10];
	int cost_return = 0;
	int num = 0;
	int j = 0;

	for (int i = 0; i < 10; ++i) {
		CI temp;
		if (temp == 0) {
			j = i;
			break;
		}
		cost_input[i] = temp;

		cost_return = 1000 - cost_input[i];
		num = 0;

		if (cost_return < 5) {
			num = cost_return;
			break;
		}
		else {
			if (cost_return < 10) {
				num = 1 + cost_return - 5;
				break;
			}
		}


		if (cost_return >= 500) {
			++num;
			cost_return -= 500;
		}
		while (1) {
			if (cost_return >= 100) {
				++num;
				cost_return -= 100;
			}
			if (cost_return < 100)break;
		}
		if (cost_return >= 50) {
			++num;
			cost_return -= 50;
		}
		while (1) {
			if (cost_return >= 10) {
				++num;
				cost_return -= 10;
				if (cost_return < 10)break;
			}
		}

		if (cost_return >= 5) {
			++num;
			cost_return -= 5;
		}
		while (1) {
			if (cost_return >= 1) {
				++num;
				cost_return -= 1;
			}
			if (cost_return < 1)break;
		}
	
	}
	

	for (int i = 0; i < j;++i) {
		CO num E
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <string>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef vector<int> vi;
typedef vector< vi > vvi;
int a[6];
int main() {
	int L,N=0,M=0;
	while (cin >> N) {
		L = 1000 - N;
		if (N == 0) {
			break;
		}
		if (L >= 500) {
			a[0]++;
			L = L - 500;
		}
		while (L >= 100) {
			a[1]++;
			L = L - 100;
		}
		while (L >= 50) {
			a[2]++;
			L = L - 50;
		}
		while (L >= 10) {
			a[3]++;
			L = L - 10;
		}
		while (L >= 5) {
			a[4]++;
			L = L - 5;
		}
		a[5] = L;
		M = a[0] + a[1] + a[2] + a[3] + a[4] + a[5];
		cout << M << endl;
		M = 0;
		for (int i = 0; i < 6; i++) {
			a[i] = 0;
		}
	}
}

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

int main(){
	int a,b,c,d,e,f,g,h;
	int sum=0;
	while(cin>>a){
		if(a==0)break;
		b=1000-a;
		c=b/500;
		d=(b-500*c)/100;
		e=(b-500*c-100*d)/50;
		f=(b-500*c-100*d-50*e)/10;
		g=(b-500*c-100*d-50*e-10*f)/5;
		h=b-500*c-100*d-50*e-10*f-5*g;
		cout<<c+d+e+f+g+h<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <string>
#include <utility>
#include <map>
int main() {
	int a[6], sum[5] = {0};
    int i = 0;
    while(a[i]) {
        std::cin >> a[i];
		i++;
        if (a[i] == 0) {
            break;
        }
  
        
    }
	i = 0;
    while (a[i] != 0) {
        sum[i] += (1000 - a[i]) / 500;
        a[i] = (1000 - a[i]) % 500;
        sum[i] += a[i] / 100;
        a[i] = a[i] % 100;
        sum[i] += a[i] / 50;
        a[i] = a[i] % 50;
        sum[i] += a[i] / 10;
        a[i] = a[i] % 10;
        sum[i] += a[i] / 5;
        a[i] = a[i] % 5;
        sum[i] += a[i];
        i++;
    }
    for (int j = 0; j<i; j++) {
        std::cout << sum[j] << std::endl;
    }
    return 0;
}

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

 
int main () 
{
  int m1;
  while(1)
  {
   cin>>m1; 
   if(m1>0||m1<=1000)
   {
       break;
   }
   else
    {
    int m2,m3,m4,m5,m6,m7;
    }
   m2=1000-m1;
   m3=m2/500;
   m4=m2%500/100;
   m5=m2%500%100/50;
   m6=m2%500%100%50/10;
   m7=m2%500%100%50%10/1;
  
   cout<<m3+m4+m5+m6+m7<<'\n';
 }
   
  return 0;
}

C++: #include<iostream>

using namespace std;

int main(){

	int yen;
	int ans=0;

	while(cin>>yen){
	if(yen==0) break;
	yen=1000-yen;
	ans=yen%5;
	yen/=5;
	ans+=yen%2;
	yen/=2;
	ans+=yen%5;
	yen/=5;
	ans+=yen%2;
	yen/=2;
	ans+=yen%5;
	ans+=yen/5;
	cout<<ans<<endl;
	}
}

C++: #include<iostream>
int main(){
	int a;
	while (std::cin >> a,a){
		int change = 1000 - a;
		int coin[] = { 500, 100, 50, 10, 5, 1 };
		int ans = 0;
		for (int i = 0; i < 6; ++i){
			while (change >= coin[i]){
				ans++;
				change -= coin[i];
			}
		}
		std::cout << ans << std::endl;
	}
	
}