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 n,cnt;
	while(1){
		scanf("%d",&n);
		if(n==0) break;
		n=1000-n; cnt=0;
		if(n>=500){
			cnt+=n/500; n=n%500;
		}
		if(n>=100){
			cnt+=n/100; n=n%100;
		}
		if(n>=50){
			cnt+=n/50; n=n%50;
		}
		if(n>=10){
			cnt+=n/10; n=n%10;
		}
		if(n>=5){
			cnt+=n/5; n=n%5;
		}
		if(n>=1) cnt+=n/1;
		printf("%d\n",cnt);
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
using namespace std;
int main()
{
	int a;
	cin >> a;
	int b = 0;
	a = 1000 - a;
	for (; a != 0;)
	{
		if (a >= 500)
		{
			a = a - 500;
			b++;
		}
		else if (a >= 100)
		{
			a = a - 100;
			b++;
		}
		else if (a >= 50)
		{
			a = a - 50;
			b++;
		}
		else if (a >= 10)
		{
			a = a - 10;
			b++;
		}
		else if (a >= 5)
		{
			a = a - 5;
			b++;
		}
		else if (a >= 1)
		{
			a = a - 1;
			b++;
		}
	}
	cout << b << endl;
	return 0;
}

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

const int coins[] = {500, 100, 50, 10, 5, 1};
int main(){
  int m;
  while(cin >> m){
    if(m==0)break;

    m = 1000 - m;
    int res = 0;
    for(int i=0;i<6;i++){
      while(m >= coins[i]){
	res++;
	m -= coins[i];
      }
    }
    cout << res << endl;
  } 
}

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

int main()
{
	int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,num11,num12,num13;
	int sum;

	cin>>num1;
	num2=1000-num1;
	num3=num2/500;
	num4=num2%500;
	num5=num4/100;
	num6=num4%100;
	num7=num6/50;
	num8=num6%50;
	num9=num8/10;
	num10=num8%10;
	num11=num10/5;
	num12=num10%5;
	num13=num12/1;
	sum=num3+num5+num7+num9+num11+num13;
	cout<<sum;
	cout<<"\n";
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long unsigned int ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e15)
#define PI (acos(-1))
#define MOD 1000000007

#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0521

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

    while(true) {
        int price; cin >> price;
        if (price == 0) break;
        int change = 1000 - price;
        int ans = 0;
        for (int c : coins) {
            int n = change / c;
            ans += n;
            change -= c * n;
        }
        cout << ans << "\n";
    }

    return 0;
}

C++: // AOJ 0521
#include<cstdio>
#define rep(i,a) for( int i = 0; i != (a); ++i )

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

int main()
{
	int p;
	while( scanf( "%d", &p ), p )
	{
		p = 1000-p;

		int ans = 0;
		rep( i, 6 )
			ans += p/C[i], p %= C[i];

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,a,b,x,y,z,m,i,c;
	scanf("%d",&n);
	a=1000-n;
	b=a/500;
	x=a%500;
	y=x/100;
	z=x%100;
	m=z/10;
	i=z%10;
	c=b+y+m;
	printf("%d\n",c);
	return 0;
}

C++: #include <iostream>

using namespace std;

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

C++: #include <iostream>
using namespace std;
int count(int x, int c){
    if(x >= 500){
        return count(x-500, c+1);
    }
    if(x >= 100){
        return count(x-100, c+1);
    }
    if(x >= 50){
        return count(x-50, c+1);
    }
    if(x >= 10){
        return count(x-10, c+1);
    }
    if(x >= 5){
        return count(x-5, c+1);
    }
    if(x == 0){
        return c;
    }
    else {
        return count(x-1,c+1);
    }
}

int main (void){
    int x;
    while (true){
        cin >> x;
        if(x == 0) {
            break;
        }
        cout << count(1000 - x, 0) << endl;
    }
}

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

C++: #include<iostream>
#include<algorithm>
#include<cstring>
#define INF 1<<28
using namespace std;

int memo[10000];

int solve(int n){

  int res = INF;
  if(!n) return 0;
  if(n < 0) return INF;
  if(memo[n]) return memo[n];

  return memo[n] = res = min(res, min(solve(n-500), min(solve(n-100), min(solve(n-50), min(solve(n-10), min(solve(n-5), solve(n-1)))))) +1);
}

int main(){

  int n;
  while(cin >> n, n){
    memset(memo, 0, sizeof(memo));
    cout << solve(1000-n) << endl;
  }

}

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 n;
   while (cin >> n, n) {
      int o = 1000 - n;
      int cnt = 0;
      while (o != 0) {
         if (o >= 500) {
            o -= 500;
         } else if (o >= 100) {
            o -= 100;
         } else if (o >= 50) {
            o -= 50;
         } else if (o >= 10) {
            o -= 10;
         } else if (o >= 5) {
            o -= 5;
         } else if (o >= 1) {
            o -= 1;
         }
         cnt++;
      }
      cout << cnt << endl;
   }

   return 0;
}

C++: #include <iostream>
using namespace std;
int count(int x, int c){
        if(x >= 500){
            return count(x-500,c+1);
        }
        if(x >= 100){
            return count(x-100,c+1);
        }
        if(x >= 50){
            return count(x-50,c+1);
        }
        if(x >= 10){
            return count(x-10,c+1);
        }
        if(x >= 5){
            return count(x-5,c+1);
        }

        if(x == 0){
            return c;
        }
        else{
            return count(x-1,c+1);
        }
    }
int main(void){
    int x;
    while(true){
        cin >> x;
        if(x==0){
            break;
        }
        cout << count(1000-x,0)<<endl;
    }
}