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 a,b,c,d,e,f,o,s,m;
	
	scanf("%d",&m);
	while(m!=0){
		a=0;
		b=0;
		c=0;
		d=0;
		e=0;
		f=0;
		s=0;
		
		o=1000-m;
		a=o/500;
		o=o-500*a;
		b=o/100;
		o=o-100*b;
		c=o/50;
		o=o-50*c;
		d=o/10;
		o=o-10*d;
		e=o/5;
		o=o-5*e;
		f=o/1;
		o=o-1*f;
	
	s=a+b+c+d+e+f;
	
	printf("%d\n",s);
	scanf("%d",&m);
	}
	
	return 0;
}

C++: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem0521 : Change **/
int main()
{
	int n;
	int c[] = {500, 100, 50, 10, 5, 1};
	while (cin>>n, n) {
		n = 1000 - n;
		
		int ans = 0;
		rep(i, 6) {
			while (n>=c[i]) {
				n-=c[i];
				ans++;
			}
		}
		cout << ans << endl;
	}
}

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

int memo[1111];
int p[] = {500, 100, 50, 10, 5, 1};

int solve(int n){
  
  if(!n) return 0;
  if(n<0) return 1<<28;
  if(memo[n]) return memo[n];
  
  int ans = 1<<28;
  for(int i=0;i<6;i++) ans = min(ans, solve(n-p[i])+1);
  
  return memo[n] = ans;
}

int main(){
  
  for(int i=0;i<1111;i++) solve(i);

  int n;
  while(cin >> n, n) cout << solve(1000-n) << endl;
  
}

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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

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

int main()
{
    int n;
    while(cin >> n && n)
    {
        int ans = 0;
        n = 1000 - n;

        REP(MAXC)while(n >= coin[i]){ n-= coin[i]; ans++;}
        
        cout << ans << endl;
    }
    return 0;
}

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

#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){

	int n;
	int money[6] = {500,100,50,10,5,1};
	while(cin >> n, n){
		
		int change = 0;
		n = 1000 - n;
		rep(i,6){
			
			change += n / money[i];
			n = n % money[i];
		}
		cout << change << endl;
	}

	return 0;
}

C++: #include <stdio.h>

int main() {
    while (1) {
        int i;
        int value, change;
        int array[6] = {500, 100, 50, 10, 5, 1};
        int times = 0; 
        int tmp;

        scanf("%d", &value);
        if (!value) return 0;

        change = 1000 - value;

        tmp = change;
        for (i=0;i<6;i++) {
            times += (tmp/array[i]);
            tmp %= array[i];
        }

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

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

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

int main(){
  int num;
  int money[]={500,100,50,10,5,1};
  int i,ans;
    
  for(;;){
    cin>>num;
    if(num==0) break;
    
    i=0;ans=0;
    num=1000-num;
    while(num!=0){
      ans += num/money[i];
      num = num%money[i];
      i++;
    }
    printf("%d\n",ans);
  }
  return 0;
}

C++: #include<stdio.h>
int main(){
	int i=0,a,b[6]={500,100,50,10,5,1},c=0;
	scanf("%d",&a);
	a=1000-a;
	while(a){
		if((a-b[i])>=0){
			a-=b[i];
			c++;
		}
		else i++;
	}
	printf("%d\n",c);
}

C++: #include<stdio.h>
int main(void)
{
	int gohyaku,hyaku,gozyu,zyu,go,ichi;
	int dai,otsuri,mai;
	scanf("%d",&dai);
	while(otsuri!=0){
		otsuri=1000-dai;
		gohyaku=0;
		hyaku=0;
		gozyu=0;
		zyu=0;
		go=0;
		ichi=0;
		while(otsuri!=0){
			while(otsuri>=500){
				otsuri=otsuri-500;
					gohyaku++;
			}
			while(otsuri>=100){
				otsuri=otsuri-100;
					hyaku++;
			}
			while(otsuri>=50){
				otsuri=otsuri-50;
					gozyu++;
			}
			while(otsuri>=10){
				otsuri=otsuri-10;
					zyu++;
			}
			while(otsuri>=5){
				otsuri=otsuri-5;
					go++;
			}
			while(otsuri>=1){
				otsuri=otsuri-1;
					ichi++;
			}
			mai=gohyaku+hyaku+gozyu+zyu+go+ichi;
			printf("%d\n",mai);
		}
	}
	return 0;
}

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

int memo[1111];

int solve(int n){
  
  if(!n) return 0;
  if(n<0) return 1<<28;
  if(memo[n]) return memo[n];
  
  int ans = 1<<28;
  ans = min(solve(n-500)+1, min(solve(n-100)+1, min(solve(n-50)+1, min(solve(n-10)+1, min(solve(n-5)+1, solve(n-1)+1)))));
  
  return memo[n] = ans;
}

int main(){
  
  for(int i=0;i<1111;i++) solve(i);

  int n;
  while(cin >> n, n) cout << solve(1000-n) << endl;
  
}

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;
	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 <bits/stdc++.h>
using namespace std;

#define llong long long
#define inf 999999999
#define mp make_pair
#define pb push_back
#define fn fill_n
typedef pair<int, int> P;
typedef pair<P, int> PP;
typedef pair<PP, int> PPP;
typedef struct data{
	int now;
	int cost;
}data;
int nh[4] = {1, 0, -1, 0};
int nw[4] = {0, 1, 0, -1};





int main(){
	int a, b;
	while(cin >> a){
		b = 1000 - a;
		int ret = 0;
		while(b != 0){
			if(b >= 500){
				ret++;
				b -= 500;
			}else if(b >= 100){
				ret += b/100;
				b %= 100;
			}else if(b >= 10){
				ret += b/10;
				b %= 10;
			}else if(b >= 5){
				ret += b/5;
				b %= 5;
			}else{
				ret += b;
				b = 0;
			}
		}
		cout << ret << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main(){
	while(1){
		int a,ans=0,m[6]={500,100,50,10,5,1};
		cin>>a;
		if(a==0) break;
		a=1000-a;
		while(1){
			if(a==0) break;
			rep(i,0,6){
				if(a>=m[i]){
					ans++; a-=m[i]; break;
				}
			}
		}
		o(ans);
	}
}