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;

int main()
{
int m;
while(cin>>m){
int ans=0;
m=1000-m;
while(m>=500){
ans++;
m-=500;
}
while(m>=100){
ans++;
m-=100;
}

while(m>=50){
ans++;
m-=50;
}
while(m>=10){
ans++;
m-=10;
}

while(m>=5){
ans++;
m-=5;
}

ans+=m;
cout<<ans;

}

}

C++: #include<iostream>
using namespace std;
int main(){
	int a;
	int i;
	int s=0;
	
	while(cin>>a){
		if(a==0)break;
		a=1000-a;
		if(a>499){
			a=a-500;
			s=s+1;
		}
		for(i=0;i<4;i++){
			if(a>99){
				a=a-100;
				s=s+1;
			}
		}
		if(a>49){
			a=a-50;
			s=s+1;
		}
		for(i=0;i<4;i++){
			if(a>9){
				a=a-10;
				s=s+1;
			}
		}
		if(a>4){
			a=a-5;
			s++;
		}
		s=s+a;
		cout<<s<<endl;
	}
	return 0;
}

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

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

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

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

	cout << cnt << endl;
}

C++: #include <iostream>

using namespace std;

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

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

int main(){
  int i0[] = {500,100,50,10,5,1};
  int i1 = 1,i2 = 0;
  int i4[5] = {0};

  for(int j = 0; j < 6; j++){
    i2 = 0;
    scanf("%d",&i1);
    
    if(i1 == 0){   
      for(int n : i4) if(n != 0) printf("%d\n",n);
      return 0;
    }
    
    i1 = 1000 - i1;

    for(int n = 0; n < 6; n++){
      while(i1 >= i0[n]){
        i1 -= i0[n];
        i2++;
      }
    }
    i4[j] = i2;    
  }
}

C++: #include <iostream>

int main() {
	int a,coin = 0;
	std::cin >> a;
	a = 1000 - a;

	coin += a / 500;
	a = a - ((a / 500) * 500);
	coin += a / 100;
	a = a - ((a / 100) * 100);
	coin += a / 50;
	a = a - ((a / 50) * 50);
	coin += a / 10;
	a = a - ((a / 10) * 10);
	coin += a / 5;
	a = a - ((a / 5) * 5);
	coin += a / 1;
	a = a - ((a / 1) * 1);

	std::cout << coin;




}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
using namespace std;
int main()
{
	int a=1;
	int b;
	int c;
	int d;
	int e;
	int f;
	int g;
	int h;
	int i;
	int j;
	while (1)
	{
		cin >> a;
		if (a = 0)
		{
			break;
		}
		b = a;
		if (b >= 500)
		{
			b = b - 500;
			c++;
		}
		else if (b >= 100)
		{
			b = b - 100;
			c++;
		}
		else if (b >= 50)
		{
			b = b - 50;
			c++;
		}
		else if (b >= 10)
		{
			b = b - 10;
			c++;
		}
		else if (b >= 5)
		{
			b = b - 5;
			c++;
		}
		else if (b >= 1)
		{
			b = b - 1;
			c++;
		}
		cout << c << 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() {
	double a[1000],b;
	int c=0,i=0;
	while(cin>>a[i]){
		
		b=1000-a[i];
	    
	while(1){
		
		if(b<0){
			b=b+500.0;
			c--;
			break;
			
		}
		c++;
		b=b-500.0;
	}	
	while(1){
		
		if(b<0){
			b=b+100.0;
			c--;
			break;
		}
		c++;
		b=b-100.0;
	}	
	while(1){
		
		if(b<0){
			b=b+50;
			c--;
			break;
		}
		c++;
		b=b-50;
	}	
	while(1){
		
		if(b<0){
			b=b+10.0;
			c--;
			break;
		}
		c++;
		b=b-10;
    	}
    	while(1){
     		
    		if(b<0){
	    		b=b+5.0;
    			c--;
    			break;
    		}
		c++;
		b=b-5.0;
     	}
	    while(1){
		
		    if(b<0){
		    	b=b+1.0;
			    c--;
		    	break;
		    }
		    c++;
		    b=b-1.0;
	    }
	   if(a[i]==0){
	   	
	   }
       else{
       	cout<<c<<endl;
        }
        i++;
        c=0;
	}
	// your code goes here
	return 0;
}

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

using namespace std;

int main(){
	/*
	//数あて
	int n;
	cin >> n;
	int p[200][3];

	for(int i=0;i<n;i++)
	{
		cin >> p[i][0] >> p[i][1] >> p[i][2];
	}

	int _p;
	for(int k=0;k<3;k++)
	{
		for(int i=0;i<n;i++)
		{
			_p = p[i][k];
			for(int j=i+1;j<n;j++)
			{
				if(_p == p[j][k])
				{
					p[i][k] = 0;
					p[j][k] = 0;
				}
			}
		}
	}

	for(int i=0;i<n;i++)
	{
		cout << p[i][0] + p[i][1] + p[i][2] << endl;
	}
	*/

	/*
	//得点
	int ai,am,as,ae,bi,bm,bs,be;
	cin >> ai >> am >> as >> ae >> bi >> bm >> bs >> be;
	int answer=ai+am+as+ae;
	if(answer < bi+bm+bs+be)
	{
		answer=bi+bm+bs+be;
	}
	cout << answer << endl;
	*/
	
	//おつり
	int onum;
	while(true){
		int N;
		cin >> N;
		if(N == 0) break;
				
		int oturi = 1000-N;
		if(oturi >= 500)
		{
			onum++;
			oturi -= 500;
		}
		while(oturi>=100)
		{
			onum++;
			oturi -= 100;
		}
		if(oturi >= 50)
		{
			onum++;
			oturi -= 50;
		}
		while(oturi>=10)
		{
			onum++;
			oturi -= 10;
		}
		if(oturi >= 5)
		{
			onum++;
			oturi -= 5;
		}
		while(oturi>=1)
		{
			onum++;
			oturi -= 1;
		}
		 cout << onum << endl;
	}
   
	
	return 0;
}

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i<n; i++)
#define ALL(a) begin(a), end(a)

int main (void) {
    int n;
    while (cin >> n , n) {
        int m = 1000 - n;
        int ans = 0;
        if (m >= 500) {
            ans ++;
            m -= 500;
        }
        //cout << m << " " << ans << endl;
        if (m >= 100) {
            ans += m/100;
            m = m % 100;
        }
        //cout << m << " " << ans << endl;
        if (m >= 50) {
            ans += m/50;
            m = m % 50;
        }
        //cout << m << " " << ans << endl;
        if (m >= 10) {
            ans += m / 10;
            m = m % 10;
        }
        //cout << m << " " << ans << endl;
        if (m >= 5) {
            ans += m / 5;
            m = m % 5;
        }
        if (m >= 1) {
            ans += m/1;
            m = m % 1;
        }
        //cout << m << " " << ans << endl;
        cout << ans << endl;
    }
    return 0;
}