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;

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

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    n = 1000 - n;
    int ans = 0;
    for (int i = 0; i < 6; ++i) {
      ans += n / c[i];
      n %= c[i];
    }

    cout << ans << endl;
  }
  return 0;
}

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

int main()
{
	int x;
	int i = 0;

	scanf("%d", &x);

	x = 1000 - x;

	while (x >= 500 ) {
		i++;
		x -= 500;
	}
	while (x >= 100 ) {
		i++;
		x -= 100;
	}
	while (x >= 50 ) {
		i++;
		x -= 50;
	}
	while (x >= 10 ) {
		i++;
		x -= 10;
	}
	while (x >= 5 ) {
		i++;
		x -= 5;
	}
	while (x >= 1 ) {
		i++;
		x -= 1;
	}

	printf("%d\n", i);
	return (0);
}

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

int main(){

  int sum;
  int cash;

  while(cin >> cash , cash){
    cash = 1000-cash;
    sum = 0;
    sum += cash/500; cash%=500;
    sum += cash/100; cash%=100;
    sum += cash/50; cash%=50;
    sum += cash/10; cash%=10;
    sum += cash/5; cash%=5;
    sum += cash/1;
    
    cout<< sum << endl;
  }

  return 0;
}

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

int main(){
	int okane;
	int oturi;
	int count = 0;
	int a[] = { 1, 5, 10, 50, 100, 500 };

	scanf_s("%d", &okane);
	oturi = 1000 - okane;

	for (int i = 5; i >= 0; i--){
		if (oturi >= a[i]){
			div_t d;

			d = div(oturi, a[i]);
			oturi = oturi - a[i] * d.quot;
			count = count + d.quot;
		}
	}
	printf("%d\n",count);

	return 0;

	div(20, 3);

}

C++: #include<iostream>

using namespace std;

int main()
{
	for(;;){
	int kane, sub, cont = 0;
	
	cin >> kane;
	if(kane == 0){
		break;
	}
	
	kane = 1000 - kane;
	sub = kane;
	for(;;){
		if(sub - 500>= 0){
			cont++;
			sub -= 500;
		}
		else{
			break;
		}
	}
//	cout <<"500:" << cont << endl;
	for(;;){
		if(sub - 100 >= 0){
			cont++;
			sub -= 100;
		}
		else{
			break;
		}
	}
//	cout <<"100:" <<cont<< endl;
	for(;;){
		if(sub - 50 >= 0){
			cont++;
			sub -= 50;
		}
		else{
			break;
		}
	}
	
//	cout << "50:" << cont << endl;
	for(;;){
		if(sub - 10 >= 0){
			cont++;
			sub -= 10;
		}
		else{
			break;
		}
	}

	for(;;){
		if(sub - 10 >= 0){
			cont++;
			sub -= 10;
		}
		else{
			break;
		}
	}

	for(;;){
		if(sub - 5 >= 0){
			cont++;
			sub -= 5;
		}
		else{
			break;
		}
	}
	
	for(;;){
		if(sub - 1 >= 0){
			cont++;
			sub -= 1;
		}
		else{
			break;
		}
	}
	
	cout << cont << endl;
}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstdlib>
#define reep(n1,n2,n3) for(int n1=n3;n1<n2;n1++)
#define rep(n1,n2) reep(n1,n2,0)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define INF 1e9
using namespace std;
typedef long long int lint;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<pair<int,int> > vp;
typedef pair<int,int> pi;
typedef pair<int,pair<int,int> > pii;
typedef vector<string> vs;
const double PI  = acos(-1.0);
int c[]={500,100,50,10,5,1};
int main(){
    int n;
    while(cin>>n,n){
        int ret=0,m=1000;
        while(m>n){
            rep(i,6){
                if(m-c[i]>=n){
                    m-=c[i];
                    ret++;
                    break;
                }
            }
        }
        cout<<ret<<endl;
    }
}

C++: #include<iostream>

using namespace std;

int main()
{
	for(int n;cin>>n,n;){
		n=1000-n;
		int m=0;
		m+=n/500;	n-=n/500*500;
		m+=n/100;	n-=n/100*100;
		m+=n/50;	n-=n/50*50;
		m+=n/10;	n-=n/10*10;
		m+=n/5;		n-=n/5*5;
		m+=n;
		cout<<m<<endl;
	}
	return 0;
}

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

int main()
{
  int n,count;
  while(cin>>n,n){
    count=0;
    n=1000-n;
    while(n>=500){count++;n-=500;}
    while(n>=100){count++;n-=100;}
    while(n>=50){count++;n-=50;}
    while(n>=10){count++;n-=10;}
    while(n>=5){count++;n-=5;}
    while(n>=1){count++;n-=1;}
    cout<<count<<endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
	int a,b,asum=0,bsum=0;
	for(int i=0;i<4;i++)
	{
		cin>>a;
		asum+=a;
	}
	for(int i=0;i<4;i++)
	{
		cin>>b;
		bsum+=b;
	}
	if(asum>=bsum)
		cout<<asum<<endl;
	else
		cout<<bsum<<endl;
	return 0;
}

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

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

	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
using namespace std;
int main()
{
	int a, b, c, d, e, i, j, k, l, m, n, o, p, q, r;
	cin >> a;
	if (a > 0)
	{
		c = 0;
		a = 1000 - a;
		b = a;
		b = a / 500;
		c = c + b;
		a = a % 500;
		b = a / 100;
		c = c + b;
		a = a % 100;
		b = a / 50;
		c = c + b;
		a = a % 50;
		b = a / 10;
		c = c + b;
		a = a % 10;
		b = a / 5;
		c = c + b;
		a = a % 5;
		b = a;
		c = c + b;
		cout << c << endl;
	}
	return 0;
}

C++: #include<stdio.h>

int mian(void){
	int n,k;

	while(0==0){
		scanf("%d",&n);
		if(n==0)break;
		k=0;
		k += n /500;  n %=500;
		k += n/100;   n %=100;
		k += n/50;    n %= 50;
		k += n/10;    n %= 10;
		k += n/5;     k += n%5;
		printf("%d\n",k);
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <algorithm>

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

using namespace std;

int main(void) {	
	int n;
	while (cin >> n && n > 0) {
		int ans = 0;
		n = 1000 - n;
		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 namespace std;
int main(){
	int n,m;
	cin >> n;
	m = 0;
	n = 1000-n;

	if(n>=500){
		m = n/500;
		n = n%500;
	}
	if(n>=100){
		m = m+n/100;
		n = n%100;
	}
	if(n>=50){
		m = m+n/50;
		n = n%50;
	}
	if(n>=10){
		m = m+n/10;
		n = n%10;
	}
	if(n>=5){
		m = m+n/5;
		n = n%5;
	}
	if(n>=1){
		m = m+n/1;
	}

	cout << m << endl;

        return 0;
}

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

using namespace std;

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