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 x;
	cin>>x;
	while(x!=0){
		
	int n=0;
	x=1000-x;
	
	if(x>=500){
		n=x/500;
		x=x%500;
	}
	if(x>=100){
		n+=x/100;
		x=x%100;
	}
	if(x>=50){
		n+=x/50;
		x=x%50;
	}
	if(x>=10){
		n+=x/10;
		x=x%10;
	}
	if(x>=5){
		n+=x/5;
		x=x%5;
	}
	if(x>=1){
		n+=x/1;
	}
	if(x==1){
		n=1;
	}
	cout<<n<<endl;
	cin>>x;
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <sstream>
using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long

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

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

int main()
{
	//?????????
	int num1=0,num2=0,num3=0;
	int p1=0,p2=0,p3=0,p4=0,p5=0,p6=0;
	int u1=0,u2=0,u3=0,u4=0,u5=0,u6=0;
	
	cin>>num1;
	if(num1<1||1000<=num1){
		return 0;
	}
	num2=1000-num1;
	p1=num2/500;
	u1=num2%500;
	p2=u1/100;
	u2=u1%100;
	p3=u2/50;
	u3=u2%50;
	p4=u3/10;
	u4=u3%10;
	p5=u4/5;
	u5=u4%5;
	p6=u5/1;
	u6=u5%1;
	num3=p1+p2+p3+p4+p5+p6;
	cout<<num3;
	cout<<"\n";
	return 0;
}

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

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define REP(i,m,n) for(int i = (int) (m);i < (int) (n);i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

int n;

int main() {

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

	return 0;
}

C++: #include <iostream>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int n[6], count=0, coin[] = { 500,100,50,10,5 };

    for (int i=0; i<6; i++) 
{
cin >> n[i];
if(!n[i])break;
}

    for (int i=0;i<6;i++) {
	if(n[i] == 0) break;

	int s=1000 - n[i];

	for (int j=0;j<5;j++) {
	   int m = s / coin[j];
	   count += m;
	   s -= m * coin[j];
	}

	count += s;
	cout << count << endl;
count = 0;
    }
    return 0;
}

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

C++: #include <iostream>
#include <algorithm>
#include <array>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <vector>

#define INF 1000000000
#define MOD 1000000007
#define ll long long
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define bitget(a,b) (((a) >> (b)) & 1)
#define vint vector<int>
#define vvint vector<vector<int>>
#define vsort(x) sort(x.begin(),x.end())

using int64 = uint64_t;

using namespace std;
void solve(int x) {
	x = 1000 - x;
	int cnt = 0;
	int a[6] = { 500,100,50,10,5,1 };
	rep(i,0,6){
		while (x >= a[i]) {
			x -= a[i];
			cnt++;
		}
	}
	cout << cnt << endl;
}
int main() {

	int i, j, k;
	int n;
	cin >> n;
	while (n != 0) {
		solve(n);
		cin >> n;
	}
	return 0;
}

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

int main()
{
while(true) {
int n;
scanf("%d", &n);
if(n == 0)
break;
int change = 1000 - n;
int ans = 0;
for(int digit = 1; digit < 1000; mul *= 10) {
int cur = change / digit % 10;
ans += (cur / 5) + (cur % 5);
}
printf("%d\n", ans);
}
return 0;
}

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

int money,c[1010],n,x;
int main(){
  x=0;
  scanf("%d",&money);
  n=1000-money;
  for(int i=0;500<=n;i++){x++;n=n-500;}
  for(int i=0;100<=n;i++){x++;n=n-100;}
  for(int i=0;50<=n;i++){x++;n=n-50;}
  for(int i=0;10<=n;i++){x++;n=n-10;}
  for(int i=0;5<=n;i++){x++;n=n-5;}
  for(int i=0;1<=n;i++){x++;n=n-1;}
  printf("%d",x);
}

C++: // quest1.cpp : ??????????????? ??¢????????±????????§????????¨????????? ?????????????????????????????????
//
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	int money, reverse, count = 0;

	scanf("%d", &money);
	reverse = 1000 - money;

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

	printf("%d\n", count);

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <cstdio>
#include <iomanip>

using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;(i)>=0;(i)--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())

typedef long long ll;

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