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>
#include <vector>

const std::vector<int> ms{500, 100, 50, 10, 5, 1};

bool solve() {
    int n;
    std::cin >> n;
    if (n == 0) return false;

    n = 1000 - n;
    int ans = 0;
    for (auto m : ms) {
        ans += n / m;
        n %= m;
    }

    std::cout << ans << "\n";

    return true;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while (solve()) {}

    return 0;
}

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

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

int main(void){
	int mon, cnt;
	while(true){
		cin >> mon;
		if(mon == 0)	break;
		mon = 1000 - mon;
		cnt = 0;
		for(int i = 0; i < 6; i++){
			if(mon == 0)	break;
			cnt += mon / change[i];
			mon -= change[i] * (mon / change[i]);
		}
		cout << cnt << endl;
	}
  return 0;
}

C++: #include <iostream>

using namespace std;

int coin;

int main()
{
	int money;
	int remoney;
	
	cin >> money;
	
	remoney = 1000 - money;
	
	if ( remoney >= 500){
		coin++;
		remoney -= 500;
	}
	coin += remoney / 100;
	remoney %= 100;
	
	coin += remoney / 50;
	remoney %= 50;
	
	coin += remoney / 10;
	remoney %= 10;
	
	coin += remoney / 5;
	remoney %= 5;
	
	coin += remoney;
	
	cout << coin << endl;
	
	return (0);
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int pay,num = 0;
	cin >> pay;
	pay = 1000 - pay;
	if (pay / 500 == 1){
		pay -= 500;
		num++;
	}
	for (;;){
		if (pay / 100 >= 1){
			pay -= 100;
			num++;
		}
		else break;
	}
	for (;;){
		if (pay / 50 >= 1){
			pay -= 50;
			num++;
		}
		else break;
	}
	for (;;){
		if (pay / 10 >= 1){
			pay -= 10;
			num++;
		}
		else break;
	}
	for (;;){
		if (pay >= 5){
			pay -= 5;
			num++;
		}
		else break;
	}
	for (;;){
		if (pay>= 1){
			pay -= 1;
			num++;
		}
		else break;
	}
	cout << num << endl;	
}

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

int main() {
	int a,b=0;
	while(1){
		cin >> a;
		if(a==0) break;
		a=1000-a;
		if(a>500){
			a-=500;
			b++;
		}
	
		while(1){
			if(a<100) break;
			a-=100;
			b++;
		}

		if(a>50){
			a-=50;
			b++;
		}
	
		while(1){
			if(a<10) break;
			a-=10;
			b++;
		}
		
		if(a>5){
			a-=5;
			b++;
		}
		
		while(1){
			if(a==0) break;
			a-=1;
			b++;
		}
		cout << b << endl;
		b=0;
	}
	return 0;
}

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

int main() {
	int a,b=0;
	while(1){
		cin >> a;
		if(a==0) break;
		a=1000-a;
		if(a>500){
			a-=500;
			b++;
		}
	
		while(1){
			if(a<100) break;
			a-=100;
			b++;
		}

		if(a>50){
			a-=50;
			b++;
		}
	
		while(1){
			if(a<10) break;
			a-=10;
			b++;
		}
		
		if(a>5){
			a-=5;
			b++;
		}
		
		while(1){
			if(a==0) break;
			a-=1;
			b++;
		}
		cout << b << endl;
		b=0;
	}
	return 0;
}

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

using namespace std;

void kesan(int, int*, int*);
int main(){
  int n, i , j, a;
  while(true){
    cin >> n;
    a = 0;
    if(!n)
      break;
    n = 1000 - n;
    kesan(500, &a, &n);
    kesan(100, &a, &n);
    kesan(50, &a, &n);
    kesan(10, &a, &n);
    kesan(5, &a, &n);
    a += n;

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

void kesan(int coin, int *a, int *n){
  int i = *n / coin;
  *a += i;  
  *n -= i * 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++;
		}
		if (b >= 100)
		{
			b = b - 100;
			c++;
		}
		if (b >= 50)
		{
			b = b - 50;
			c++;
		}
		if (b >= 10)
		{
			b = b - 10;
			c++;
		}
		if (b >= 5)
		{
			b = b - 5;
			c++;
		}
		if (b >= 1)
		{
			b = b - 1;
			c++;
		}
		cout << c << endl;
	}
	return 0;
}

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

int main(){
	int a,b,c,d1,e,f,g,h;
	int sum=0;
	while(cin>>a){
		if(a==0)break;
		b=1000-a;
		c=b/500;
		d=(b-500*c)/100;
		e=(b-500*c-100*d)/50;
		f=(b-500*c-100*d-50*e)/10;
		g=(b-500*c-100*d-50*e-10*f)/5;
		h=b-500*c-100*d-50*e-10*f-5*g;
		cout<<c+d+e+f+g+h<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main() {
	while (1) {
		int ki, co = 0;
		cin >> ki;
		if (ki == 0) { break; }
		ki = 1000 - ki;
		if (ki >= 500) {
			ki = ki - 500;
			co++;
		}
		if (ki >= 100) {
			for (int i = 0; ki < 100; i++) {
				ki = ki - 100;
				co++;
			}
		}
		if (ki >= 50) {
			ki = ki - 50;
			co++;
		}
		if (ki >= 10) {
			for (int i = 0; ki < 10; i++) {
				ki = ki - 10;
				co++;
			}
		}
		if (ki >= 5) {
			ki = ki - 5;
			co++;
		}
		if (ki >= 1) {
			for (int i = 0; ki == 0; i++) {
				ki = ki - 1;
				co++;
			}
		}
		cout << co << endl;
	}
	return 0;
}

C++: #include<stdio.h>

int main(void){
  int n,cha,count=0;

  while(1){

    scanf("%d",&n);
    if(n == 0) break;

    cha=1000-n;

    if(cha >= 500){
      cha -= 500;
      count++;
    }

    while(cha >= 100){
      cha -= 100;
      count++;
    }

    if(cha >= 50){
      cha -= 50;
      count++;
    }

    while(cha >= 10){
      cha -= 10;
      count++;
    }

    if(cha >= 5){
      cha -= 5;
      count++;
    }

    while(cha >= 1){
      cha -= 1;
      count++;
    }

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

  }
  return 0;
}

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

int main()
{
	int a,h;
	h=0;
	cin>>a;
	a=1000-a;
	if(a==1000)return 0;
	for(;a>=500;)
	{
		a=a-500;
		h=h+1;
		
	}
	for(;a>=100;)
	{
	a=a-100;
	h=h+1;
	}
	for(;a>=50;)
	{
	a=a-50;
	h=h+1;
	}
	for(;a>=10;)
	{
	a=a-10;
	h=h+1;
	}
	for(;a>=5;)
	{
	a=a-5;
	h=h+1;
	}
	for(;a>=1;)
	{
	a=a-1;
	h=h+1;
	}
	cout<<h<<endl;
		return 0;

}

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

 
int main () 
{
  int m1;
  while(1)
  {
   cin>>m1; 
   if(m1>0||m1<=1000)
   {
       break;
   }
   else
    {
    int m2,m3,m4,m5,m6,m7;

   m2=1000-m1;
   m3=m2/500;
   m4=m2%500/100;
   m5=m2%500%100/50;
   m6=m2%500%100%50/10;
   m7=m2%500%100%50%10/1;
  
   cout<<m3+m4+m5+m6+m7<<'\n';
   }
 }
   
  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;
			}
			if (a == 0) {
				break;
			}
		}
		printf("%d\n", cnt);
	}
}