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(void){
    // Your code here!
    int m;
    
    while(true){
        cin >> m;
        if(m == 0) break;
        
        int o; //おつり
        o = 1000 - m;
        int count = 0;
        if(o >= 500){
            count = count + o / 500;
            o = o % 500;
            if(o >= 100){
                count = count + o / 100;
                o = o % 100;
            }
            if(o >= 50){
                count = count + o / 50;
                o = o % 50;
            }
            if(o >= 10){
                count = count + o / 10;
                o = o % 10;
            }
            if(o >= 5){
                count = count + o / 5;
                o = o % 5;
            }
            count = count + o;
            cout << count << endl;
        }
       
        
        else if(o >= 100 && o < 500){
            
            count = count + o / 100;
            o = o % 100;
            
            if(o >= 50){
                count = count + o / 50;
                o = o % 50;
            }
            if(o >= 10){
                count = count + o / 10;
                o = o % 10;
            }
            if(o >= 5){
                count = count + o / 5;
                o = o % 5;
            }
            count = count + o;
            cout << count << endl;
        }
        
        
        else if(o >= 50 && o < 100){
            
            count = count + o / 50;
            o = o % 50;
            
            if(o >= 10){
                count = count + o / 10;
                o = o % 10;
            }
            if(o >= 5){
                count = count + o / 5;
                o = o % 5;
            }
            count = count + o;
            cout << count << endl;
        }
        
        
        else if(o >= 10 && o < 50){
            
            count = count + o / 10;
            o = o % 10;
            if(o >= 5){
                count = count + o / 5;
                o = o % 5;
            }
            count = count + o;
            cout << count << endl;
        }
        
        else if(o >= 5 && o < 10){
            count = count + o / 5;
            o = o % 5;
            count = count + o;
            cout << count << endl;
        }
        
        else{
            count = count + o;
            cout << count << endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

		int a[6] = {500, 100, 50, 10, 5, 1};
		int ans = 0;
		rep(i, 6) {
			if(m >= a[i]) {
				ans += m / a[i];
				m %= a[i];
			}
		}

		cout << ans << endl;
	}


	return 0;
}

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

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

C++: #include <iostream>

using namespace std;

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

C++: #include <iostream>

using namespace std;

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

int main()
{
 int n;
 cin >> n;
 int remaind = 1000 - n;
 int ans = 0;
 for (int i = 0; i < 6; i++)
 {
  ans += remaind / coins[i];
  remaind %= coins[i];
 }
 cout << ans << endl;
 return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T T    cums(T& v){ T r(v.size()); partial_sum(v.begin(), v.end(), r); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    acum(vector<T>& v, T b=0){ return accumulate(v.begin(), v.end(), b); }
TEMP_T vector<T> inpt(int n){ vector<T> v(n); for (int i=0; i<n; i++) cin >> v[i]; return v; }

static inline int in(){ int x; scanf("%d", &x); return x; }

struct Solver {
  Solver(){}
  int solve(){
  }
};

int main()
{
  int n;
  while (n = in()){
    n = 1000 - n;
    int v = 0;
    for (int i = 0; i < 6; i++){
      static const int c[] = {500, 100, 50, 10, 5, 1};
      v += n / c[i];
      n %= c[i];
    }
    printf("%d\n", v);
  }

  return (0);
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
#define rep(i,n) for (ll i=0;i<(n);++i)
#define rep2(i,a,b) for (ll i=(a);i<(b);++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(),(v).end()
const ll MOD=1e9+7;
//const ll MOD=998244353;
const ll INF=1e9;
const ll IINF=1e18;
const double EPS=1e-8;
const double pi=acos(-1);

template<class T> inline bool chmin(T &a,T b){
    if (a>b){a=b; return true;} return false;
}
template<class T> inline bool chmax(T &a,T b){
    if (a<b){a=b; return true;} return false;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    vector<int> coins={500,100,50,10,5,1};
    int a;
    while(cin >> a,a){
        a=1000-a;
        int ans=0;
        rep(i,6) ans+=a/coins[i],a%=coins[i];
        cout << ans << endl;
    }
}

C++: #include<iostream>
using namespace std;
int main(){
int n;cin>>n;
cout<<n/500+(n/100)%5+(n/50)%2+(n/10)%5+(n/5)%2+n%5<<endl;
}

C++: #include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int pay,remain,ans=0;

	while(1){
		cin>>pay;
		if(pay==0){
			break;
		}
		remain=1000-pay;
		ans=0;
		while(remain>=500){
			remain-=500;
			ans++;
		}
		while(remain>=100){
			remain-=100;
			ans++;
		}
		while(remain>=50){
			remain-=50;
			ans++;
		}
		while(remain>=10){
			remain-=10;
			ans++;
		}
		while(remain>=5){
			remain-=5;
			ans++;
		}
		while(remain>=1){
			remain-=1;
			ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}