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 "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
	while (1) {

		int N; cin >> N;
		if (!N)break;
		N = 1000 - N;
		int ans = 0;
		vector<int>s;
		s = { 500,100,50,10,5,1 };
		for (auto m : s) {
			ans += N / m;
			N %= m;
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>

using namespace std;

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

C++: #include<stdio.h>
int main()
{
	int n,r,i,f;
	for(i=0;i<5;i++){
		r=0;
		scanf("%d",&f);
		if(f==0){
			break;
		}
		n=1000-f;
		if(n>=500){
			r++;
			n-=500;
		}
		while(n>=100){
			r++;
			n-=100;
		}
		if(n>=50){
			r++;
			n-=50;
		}
		while(n>=10){
			r++;
			n-=10;
		}
		if(n>=5){
			r++;
			n-=5;
		}
		while(n>0){
			r++;
			n-=1;
		}
		printf("%d\n",r);
	}

	return 0;
}

C++: #include<iostream>


using namespace std;
int main(void){
    int a;
	int n;
	int c=0;
	cin>>a;

	int count=0;
	for(n=1000-a;n!=0;c++){
        if(n>=500)n-=500;
        else if(n>=100)n-=100;
        else if(n>=50)n-=50;
        else if(n>=10)n-=10;
        else if(n>=5)n-=5;
        else if(n>=1)n-=1;
    }
    cout << c << endl;


	return 0;
}

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

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

int main(){

    int input,money,ans;

    while(1){
      cin >> input;
      if(input == 0){
        break;
      }
      money = 1000 - input;

      ans = 0;

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


    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	int n;
	
	int s[6]={500,100,50,10,5,1};
	while(cin>>n,n!=0){
	int x=0;
	n=1000-n;
	int k=0;
	while(n>0){
		if(n-s[k]>=0){
			n=n-s[k];
			x++;
		}
		else
			k++;
	}
	
	cout<<x<<endl;
	}

}

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

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;
using vii=vector<vector<int>>;

const ll LINF=1LL<<60;
const int INF=1<<29;
const int MOD=1e9+7;

int main(){
    int cost;
    int money[6]={500, 100, 50, 10, 5, 1};
    while(cin >> cost && cost){
        int change=1000-cost;
        int ans=0;
        for(int i=0; i<6; i++){
            if(change == 0) break;
            ans+=change/money[i];
            change=change%money[i];
        }
        cout << ans << endl;
    }
    return 0;
}

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

C++: #include<iostream>
#include<utility>
#include<queue>

using namespace std;

int main(void){

	std::ios_base::sync_with_stdio();

	int kouka[]={500,100,50,10,5,1};
	int n,cnt;

	while(1){
		cin>>n;
		if(n==0) break;
		n=1000-n;
		cnt=0;
		for(int i=0;i<6;i++){
			cnt+=n/kouka[i];
			n-=(n/kouka[i])*kouka[i];
		}
		cout<<cnt<<endl;
	}

	return 0;
}

C++: #include<iostream>

using namespace std;

int main(){

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

    int money = 1000;
    money -= in;

    int sum = 0;

    if(money >= 500){
      sum++;
      money -= 500;
    }

    while(money >= 100){
      sum++;
      money -= 100;
    }

    if(money >= 50){
      sum++;
      money -= 50;
    }

    while(money >= 10){
      sum++;
      money -= 10;
    }

    if(money >= 5){
      sum++;
      money -= 5;
    }

    sum += money;

    cout << sum << endl;

  }

  return 0;
}

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

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

int n, sum = 0;

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