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 <stdio.h>

int main(void)
{
    int x, y;

    while(1) {
        scanf("%d", &x);
        if(x == 0) break;
        y = 1000-x;
        int cnt[6] = {0};
    
        while(y > 0) {
            if(y >= 500) {
                y -= 500;
                cnt[0]++;
            } else if(y >= 100) {
                y -= 100;
                cnt[1]++;
            } else if(y >= 50) {
                y -= 50;
                cnt[2]++;
            } else if(y >= 10) {
                y -= 10;
                cnt[3]++;
            } else if(y >= 5) {
                y -= 5;
                cnt[4]++;
            } else if(y >= 1) {
                y -= 1;
                cnt[5]++;
            } else {
                break;
            }
        }
    
        printf("%d\n", cnt[0]+cnt[1]+cnt[2]+cnt[3]+cnt[4]+cnt[5]);
    }

    return (0);
}

C++: #include<stdio.h>
int main()
{
	int cnt = 0, a, b, i, j;
	while(1){
	scanf("%d", &a);
	cnt = 0;
		if (a == 0) {
			break;
		}
		while (1) {
			if (a >= 500) {
				a = a - 500;
				cnt++;
			}
			else if (a >= 100) {
				a = a - 100;
				cnt++;
			}
			else if (a >= 50) {
				a = a - 50;
				cnt++;
			}
			else if (a >= 10) {
				a = a - 10;
				cnt++;
			}
			else if (a >= 1) {
				a = a - 1;
				cnt++;
			}
			else if (a == 0) {
				break;
			}
		}
		printf("%d\n", cnt);
	}
}

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

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

    vector<int> pays;
    while (1) {
        int tmp;
        cin >> tmp;
        if (tmp == 0) {
            break;
        } else {
            pays.push_back(tmp);
        }
    }

    vector<int> coins = {500, 100, 50, 10, 5, 1};
    for (auto pay : pays) {
        int back = 1000 - pay; // お釣り
        int ans = 0;
        for (auto coin : coins) {
            int use = back / coin; // coin円 を使用する枚数
            back -= coin * use;
            ans += use;
        }
        cout << ans << endl;
    }
    return 0;
}

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

C++: #include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int cost;

	while(cin >> cost)
	{
		if (cost == 0) break;

		int change, changenum;
		change = 1000 - cost;
		changenum = 0;

		if (change > 500)
		{
			change -= 500;
			changenum += 1;
		}

		for (int i = 0; change >= 100; i++)
		{
			change -= 100;
			changenum += 1;
		}

		for (int i = 0; change >= 50; i++)
		{
			change -= 50;
			changenum += 1;
		}

		for (int i = 0; change >= 10; i++)
		{
			change -= 10;
			changenum += 1;
		}
		
		for (int i = 0; change >= 5; i++)
		{
			change -= 5;
			changenum += 1;
		}

		changenum += change;

		cout << changenum << endl;
	}

	return 0;
}

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

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a,cnt=0,cmp;
    vector<int> q;
    int w[6]={500,100,50,10,5,1};
    while(1){
        cin >> a;
        if(a==0){break;}
        q.push_back(a);
    }
    int g=q.size();
    for(int j=0;j<g;j++){
        cmp=1000-q[j];
        for(int i=0;i<6;i++){
            cnt+=cmp/w[i];
            cmp-=w[i]*(cmp/w[i]);
        }
        cout << cnt << endl;
        cnt=0;
    }

    return 0;
}

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

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <memory>
#include <memory.h>
#include <fstream>
#include <cmath>
#include <math.h>
#include <numeric>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <map>
#include <iomanip>
#include <list>
#include <cctype>
#include <algorithm>
#include <complex>

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

#define rep(i, n) for(int i = 0; i < n; i++)

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

int main(){
	int n;
	while(cin >> n && n){
		n = 1000 - n;
		int cnt = 0;
		rep(i, 6){
			cnt += n / nums[i];
			n %= nums[i];
		}
		cout << cnt << endl;
	}
	return 0;
}

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

int main(){

 int num,oturi,m = 0;

while(1){
 
cin>>num;

      if(num==0){
         break;
  }else{
    
  oturi=1000-num;

   
 
    m = oturi/500;
 
    m = m+oturi%500/100;
 
    m = m+oturi%500%100/50;
  
    m = m+oturi%500%100%50/10;
 
    m = m+oturi%500%100%50%10/5;

    m = m+oturi%500%100%50%10%5/1;

    num = 0;

     
   }
  cout<<m<<'\n';
  
}

return 0;
}

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

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

int main(){
	int n; scanf("%d",&n);
	int m=1000-n;
	int res=0;
	for(int i=0;i<6;i++){ res+=m/c[i]; m%=c[i]; }
	printf("%d\n",res);
}

C++: #include <stdio.h>

int main(void)
{
	int money;
	int change;
	int coin;
	
	while (scanf("%d", &money) != EOF){
		coin = 0;
		if (money != 0){
			if (change / 500 != 0){
				coin += change / 500;
				change %= 500;
			}	
			if (change / 100 != 0){
				coin += change / 100;
				change %= 100;
			}
			if (change / 50 != 0){
				coin += change / 50;
				change %= 50;
			}
			if (change / 10 != 0){
				coin += change / 10;
				change %= 10;
			}
			if (change / 5 != 0){
				coin += change / 5;
				change %= 5;
			}
			if (change != 0){
				coin += change;
			}
		}
		printf("%d\n", coin);
	}
	
	return (0);
}

C++: #include <iostream>

using namespace std;

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

int main() {
  int m = 0;
  int n;
  while(cin >> m, m) {
    n = 0;
    m = 1000 - m;
    for (int i = 5; i >= 0; i--) {
      int t = m / coins[i];
      n += t;
      m -= t * coins[i];
    }
    cout << n << endl;
  }
}