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<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 || change == 1000) break;
	else if(change < 0 || 1000 < change) 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;
		}
	}
	}
	
	if(pay == 0 ) return 0;
        else if(pay < 0 || 1000 < pay) return 0;
	else cout << i << "\n";

	
	return 0;
}

C++: #include <cstdio>

int main() {
	int num,n,i,a,b;
	a=0,b=0,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>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",i);
	return 0;
}

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

int main(){
    const int m[6] = {500,100,50,10,5,1};
    int price;
    
    while(true){
        cin >> price;
        if(!price) break;
        
        int change = 1000 - price,cnt = 0;
        for(int i = 0 ; i < 7 ;){
            if(change >= m[i]){
                change -= m[i];
                cnt++;
            }
            else i++;
            
            if(!change) break;
        }
        cout << cnt << endl;
    }
    
    return 0;
}

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

int minChange(int money)
{
	int coin[6]={500, 100, 50, 10, 5, 1};
	int n=0;
	while(money > 0)
	{
		for(int i=0; i<6; i++)
		{
			if(money >= coin[i])
			{
				money -= coin[i];
				n++;
				break;
			}
		}
	}
	return n;
}

int main(void)
{
	int money;
	while(1)
	{
		cin >> money;
		if(money == 0)return 0;

		cout << minChange(1000-money) << endl;
	}
	return 0;
}

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

using namespace std;

#define print(a) printf("%d\n", a)

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)

#define MAX(a,b) ((a)>(b)?(a):(b))


int main(){
	int co, mod, ch;
	
	while (cin >> co , co != 0){
		ch = 0;
		
		mod = 1000 - co;
	
		ch += mod/500;
		mod = mod%500;
	
		ch += mod/100;
		mod = mod%100;
	
		ch += mod/50;
		mod = mod%50;
	
		ch += mod/10;
		mod = mod%10;
	
		ch += mod/5;
		mod = mod%5;
	
		ch += mod;
	
		cout << ch << endl;
	}
	
	return 0;
}

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

int main(){
  int m,sum = 0;
  cin >> m;
  m = 1000 - m;
  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;
  cout << sum + m << endl;
}

C++: // 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<bits/stdc++.h>
using namespace std;
typedef long long ll;


int main(){
    while(1)
    {
        int i;
        cin>>i;
        if(i==0)break;
        int remain = 1000-i;
        int count = 0;
        while(remain!=0)
        {
            if(remain>=500)
            {
                count++;
                remain -= 500;
            }
            else if(remain>=100)
            {
                count++;
                remain -= 100;
            }
            else if(remain>=50)
            {
                count++;
                remain -= 50;
            }
            else if(remain>=10)
            {
                count++;
                remain -= 10;
            }
            else if(remain>=5)
            {
                count++;
                remain -= 5;
            }
            else if(remain>=1)
            {
                count++;
                remain -= 1;
            }
        }
        cout<<count<<endl;
    }
    
}

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()
{
  cin >>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>
#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;
	*/

	//おつり
	while(true){
		int N;
		if(N == 0) break;
		
		int nedan;
		cin >> nedan;
		int oturi = 1000-nedan;
		int onum= 0;
		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;
}