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


		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>

using namespace std;

int main(void) {
	int now, count;

	while(true) {
		cin >> now;
		if(now == 0) { break; }
		count = 0;
		now = 1000 - now;
		while(now > 0) {
			if(now >= 500) {
				now -= 500;
				count++;
			} else if(now >= 100) {
				now -= 100;
				count++;
			} else if(now >= 50) {
				now -= 50;
				count++;
			} else if(now >= 10) {
				now -= 10;
				count++;
			} else if(now >= 5) {
				now -= 5;
				count++;
			} else {
				now -= 1;
				count++;
			}
		}
		cout << count << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdlib>
#include <math.h>
#include <vector>
#include <set>

using namespace std;

int main()
{
    int ans, n, rest, itr;
    const int coin[] = {500, 100, 50, 10, 5, 1};
    while(true){
        cin >> n;
        if(n == 0){
            break;
        }
        else {
            itr = 0;
            ans = 0;
            rest = 1000 - n;
            while(rest > 0){
                if(itr >= 6){
                    break;
                }
                else if(rest - coin[itr] >= 0){
                    ans++;
                    rest -= coin[itr];
                }
                else {
                    itr++;
                }
            }
            cout << ans << endl;
        }
    }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 3000
using namespace std;

int C[6]={1,5,10,50,100,500};
int T[MAX+1];
void douteki();

int main(){
  int n;
 

  while(cin >> n){
    douteki();
    cout << T[1000-n] << endl;
  }



}

void douteki(){
  for(int j=0;j<=MAX;j++)T[j]=10000;
  T[0]=0;
  for(int i=0;i<6;i++){
    for(int j=C[i];j<=MAX;j++){
      T[j]=min(T[j],T[j-C[i]]+1);
    }
  }

}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};

int N;

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

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

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

	int money = 1000;
	int pay, change, i;

	cin >> pay;
	change = money - pay;

	while(1){
	if(change == 0) break;
	while(1){
		if(change - 500 >= 0){
			i += 1;
			change -= 500;
			break;
		}
		else if(change - 100 >= 0){
			i += 1;
			change -= 100;
			break;
		}
		else if(change - 50 >= 0){
                        i += 1;
                        change -= 50;
                        break;
                }
		else if(change - 10 >= 0){
                        i += 1;
                        change -= 10;
                        break;
                }
		else if(change - 5 >= 0){
                        i += 1;
                        change -= 5;
                        break;
                }
		else{
			i += 1;
			change -= 1;
			break;
		}
	}
	}
	
	cout << i << "\n";		

	return 0;
}

C++: #include<iostream>
using namespace std;
 
int main(){
   
  int n;

  while(cin>>n, n){
    int cnt=0;
    
    n=1000-n;
    cnt+=n/500;
    n%=500;
    cnt+=n/100;
    n%=100;
    cnt+=n/50;
    n%=50;
    cnt+=n/10;
    n%=10;
    cnt+=n/5;
    n%=5;
    
    cout<<cnt+n<<endl;
  }
   
  return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
	while (true)
	{		
		int a;
		int i = 0;
		cin >> a;
		if (a == 0)break;
		int b = 1000 - a;
		while (b >= 500)
		{
			b = b - 500;
			i = i + 1;
		}
		while (b >= 100)
		{
			b = b - 100;
			i = i + 1;
		}
		while (b >= 50)
		{
			b = b - 50;
			i = i + 1;
		}
		while (b >= 10)
		{
			b = b - 10;
			i = i + 1;
		}
		while (b >= 5)
		{
			b = b - 5;
			i = i + 1;
		}
		while (b >= 1)
		{
			b = b - 1;
			i = i + 1;
		}
		cout << i << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int cut=0;
	int a;
	int b;
	while(1){
	cin>>a;
		cut=0;
		if(a==0){
			break;
		}
	b=1000-a;
	cut+=b/500;
	b=b%500;
	cut+=b/100;
	b=b%100;
	cut+=b/50;
	b=b%50;
	cut+=b/10;
	b=b%10;
	cut+=b/5;
	b=b%5;
	cut+=b;
	cout<<cut<<endl;
	}
	return 0;
}

C++: #include <cstdio>

int main() {
	int num,n,i,a,b;
	i=0;
	scanf("%d",&num);
	n=1000-num;

	if (n>=500) {
		i++;
		n=(n-500);
	}
	if (n>=100){
		a=n/100;
		i=a+i;
		n=n%100;
	}
	if (n>=51){
		i++;
		n=(n-50);
	}
	if (n>=11){
		b=n/10;
		i=b+i;
		n=n%10;
	}
	if (n>=6){
		i++;
		n=(n-5);
	}
		i=(i+n);
	printf("%d\n",i);
	return 0;
}

C++: // 2012/12/14 Tazoe

#include <iostream>
using namespace std;

int main()
{
while(true){
	int n;
	cin >> n;

if(n==0)
break;

	int m = 1000-n;

	int sum = 0;

	sum += m/500;
	m %= 500;

	sum += m/100;
	m %= 100;

	sum += m/50;
	m %= 50;

	sum += m/10;
	m %= 10;

	sum += m/5;
	m %= 5;

	sum += m;

	cout << sum << '\n';
}

	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
for(;;){
int n,ans=0;
cin >> n;
if(n==0)return 0;
n=1000-n;
while(n>=500){
n-=500;
ans++;
}
while(n>=100){
n-=100;
ans++;
}
while(n>=50){
n-=50;
ans++;
}
while(n>=10){
n-=10;
ans++;
}
while(n>=5){
n-=5;
ans++;
}
while(n>=1){
n-=1;
ans++;
}
cout<< ans << "\n";
}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<utility>
#include<cstdio>
#include<set>
#include<string>
#include<map>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,x,n) for(int i=x;i<n;i++)
typedef pair<int,int> P;
string s;
int k;
int INF=1000000000;
int dh[4]={0,1,0,-1};
int dw[4]={1,0,-1,0};

int main(){
    int r;
    int v[6]={500,100,50,10,5,1};
    while(true){
        cin>>r;
        if(r==0) break;
        int ans=0;
        int m=1000-r;
        int i=0;
        while(m!=0&&i<6){
            ans+=m/v[i];
            m%=v[i];
            i++;
        }
        cout<<ans<<endl;
    }
}