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>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

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

C++: #include<iostream>
using namespace std;
int main(){
	int monny;
	int coin[]={500,100,50,10,5,1};
	while(cin>>monny&&monny!=0){
		monny=1000-monny;
		int answer=0;
		for(int i=0;i<6;++i){
			answer+=monny/coin[i];
			monny%=coin[i];
		}
		cout<<answer<<endl;
	}
	return 0;
}

C++: #include <stdio.h>
int main(){
int n,ans;
while(1){
scanf("%d",&n);
if(n==0)return 0;
n=1000-n;
ans=0;
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++;
}
printf("%d\n",ans);
}
}

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

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

int main(){
	int cnt = 0,i = 5;
	while(1){
		cin >> X;
		if(X==0)break;
		X = 1000-X;
		while( X > 0 ){
			while( X >= change[i] ){
				X -= change[i];
				cnt++;
			}
			i--;
		}
		cout << cnt << endl;
	}
	return 0;
}

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

int main(){
int buy,oturi,kouka;
while(1){
cin>>buy;
if(buy==0){
break;
}
oturi=1000-buy;
if(oturi>500){
oturi-=500;
kouka+=1;
}
while(oturi>=100){
oturi-=100;
kouka+=1;
}
while(oturi>=50){
oturi-=50;
kouka+=1;
}
while(oturi>=10){
oturi-=10;
kouka+=1;
}
while(oturi>=5){
oturi-=5;
kouka+=1;
}
while(oturi>=1){
oturi-=1;
kouka+=1;
}
cout<<kouka<<'\n';
}
return 0;
}

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

C++: #include<iostream>

using namespace std;

int input,oturi,output; 

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

C++: #include<iostream>
using namespace std;
int main()
{
  int money,oturi,maisu,money2[6] = {500,100,50,10,5,1};
  
  cin >> money;
  oturi = 1000 - money;

  for(int i = 0;i < 6;i++){
    maisu = oturi / money2[i];
    oturi %= money2[i];
  }
  cout << maisu << endl;
    
    return 0;
  }

C++: #include <iostream>

using namespace std;

int main(){
	int pay=0,coin=0;
	cin>>pay;
	pay=1000-pay;
	if(pay/500>=1){
		pay-=500;
		coin++;
	}
	if(pay>=100){
		coin+=pay/100;
		pay-=100*(pay/100);
	}
	if(pay>=50){
		coin+=pay/50;
		pay-=50*(pay/50);
	}
	if(pay>=10){
		coin+=pay/10;
		pay-=10*(pay/10);
	}
	if(pay>=5){
		coin+=pay/5;
		pay-=5*(pay/5);
	}
	if(pay>=1){
		coin+=pay;
	}
	cout<<coin<<endl;
}

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

using namespace std;

int main() {
    while(1<2){
        int money,change,coin=0;

        cin>>money;
        if(money==0)break;
        change=1000-money;
        while(change-500>=0){
            coin+=1;
            change-=500;
            if(change<500)break;
        }
        
            while(change-100>=0){
            coin+=1;
            change-=100;
            if(change<100)break;
        }
        
                while(change-50>=0){
            coin+=1;
            change-=50;
            if(change<50)break;
        }
        
                while(change-10>=0){
            coin+=1;
            change-=10;
            if(change<10)break;
        }
        
                while(change-5>=0){
            coin+=1;
            change-=5;
            if(change<5)break;
        }
        
                while(change-1>=0){
            coin+=1;
            change-=1;
            if(change<1)break;
        }
        
        cout<<coin<<endl;
    }
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
	int n;
	while (cin >> n, n) {
		n = 1000 - n;
		int 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>
#using namespase std;
 
int main(
    int n,m;
    cin >> n;
 
    n = 1000 - n;
    m = 0;
 
    if ( n >= 500) {
        m = m + 1;
    }
    if ( n >= 100) {
        m = m + (n / 100);
    }
    if (n >= 50) {
        m = m + (n / 50);
    }
    if (n >= 10) {
        m = m + (n / 10);
    }
    if (n >= 5) {
        m = m + (n / 5);
    }
    if (n >= 1) {
        m = m + (n / 1);
    }
 
    cout << m << endl;
 
    return 0;
)

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

using ll = long long;
#define pq priority_queue
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

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

   return 0;
}