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++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
#define PI (2*acos(0.0))
#define EPS (1e-9)
#define MOD (int)(1e9+7)
#define INF numeric_limits<int>::infinity()
#pragma endregion


int main()
{
    int n;
    cin >> n;
    int ret = 1000 - n;

    int ans = 0;

    ans += ret / 500;
    ret = ret % 500;

    ans += ret / 100;
    ret = ret % 100;

    ans += ret / 50;
    ret = ret % 50;

    ans += ret / 10;
    ret = ret % 10;

    ans += ret / 5;
    ret = ret % 5;

    ans += ret;

    P(ans);

    return 0;
}

C++: #include <cstdio>

int main (int argc, char const* argv[])
{
	
	int coin[6]={500,100,50,10,5,1};
	
	int a,r=0,m;
	
	scanf("%d",&m);
	m=1000-m;
	
	for(int i=0;i<6;i++){
		a=m/coin[i];
		r+=a;
		m-=(a*coin[i]);
	}
	
	
	printf("%d\n",r);
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b,ans;
    while(cin>>a,a!=0){
        ans=0;
        b=1000-a;
        ans+=b/500;
        b=b%500;
        ans+=b/100;
        b=b%100;
        ans+=b/50;
        b=b%50;
        ans+=b/10;
        b=b%10;
        ans+=b/5;
        b=b%5;
        ans+=b;
        cout<<ans<<endl;
    }
}

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

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

 
int main () 
{
  int m1;
  while(1)
  {
   cin>>m1; 
   if(m1>0||m1<=1000)
   {
       break;
   }
    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 <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000005
#define MAX 5001
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[6] = {500, 100, 50, 10, 5, 1};
int solve(int n){
	int ans = 0;
	while(1){
		for(int i = 0;i < 6;i++){
			if(n >= a[i]){
				n -= a[i];
				ans++;
				break;
			}
		}
		if(n == 0)break;
	}
	return ans;
}

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

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n,ans,sum;
  scanf("%d",&n);
  while(n != 0){
    ans = 0;
    sum = 1000-n;
    
    while(sum >= 500){
      sum -= 500;
      ans++;
    }
    while(sum >= 100){
      sum -= 100;
      ans++;
    }
    while(sum >= 50){
      sum -= 50;
      ans++;
    }
    while(sum >= 10){
      sum -= 10;
      ans++;
    }
    while(sum >= 5){
      sum-=5;
      ans++;
    }
    while(sum != 0){
      sum--;
      ans++;
    }
    cout << ans << endl;
    scanf("%d",&n);
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define trep(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
int v[6] = {500, 100, 50, 10, 5, 1};
int greed(int n)
{
    int count = 0;
    rep(i, 6)
    {
        int cc = n / v[i];
        n -= cc * v[i];
        count += cc;
    }
    return count;
}
int main()
{
    int n;
    while (scanf("%d%*c", &n))
    {
        if (n == 0)
        {
            break;
        }
        int ans = greed(1000-n);
        cout << ans << endl;
    }
    return 0;
}

C++: #include <cstdio>
using namespace std;
int main()
{
 int a;
 scanf("%d",&a);
 a=1000-a;
 printf("%d\n",a/500+(a/100)%5+(a/50)%2+(a/10)%10+(a/5)%2+a%5);
}

C++: #include<iostream>
int c[]={500,100,50,10,5,1};
int main(){int n,r,i;while(1){std::cin>>n;if(!n)break;n=1000-n,r=0;for(i=0;i<6;i++)r+=n/c[i],n%=c[i];std::cout<<r<<"\n";}}

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

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

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

	for(int price; cin >> price, price;) {
		int change = 1000 - price;
		int ans = 0;
		
		for(int i = 0; i < 6; ++i) {
			while(change >= coins[i]) {
				change -= coins[i];
				++ans;
			}
		}
		
		cout << ans << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include<stdio.h>

int main(){
	int kane,a,b;
	while(0<=scanf("%d",&kane)){
		if(kane==0) break;
		a=0;
		kane=1000-kane;
		while(kane>=500){
			kane-=500;
			++a;
		}
		while(kane>=100){
			kane-=100;
			++a;
		}
		while(kane>=50){
			kane-=50;
			++a;
		}
		while(kane>=10){
			kane-=10;
			++a;
		}
		while(kane>=5){
			kane-=5;
			++a;
		}
		while(kane>=1){
			kane-=1;
			++a;
		}
		printf("%d\n",a);
	}
	return 0;
}

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

int main(){
while(true){
  int iwakurasisetudan;
  int b;
  int c;
  cin>>iwakurasisetudan;
if(iwakurasisetudan==0){break;
}else{  c=1000-iwakurasisetudan;
  b=c/500;
  c=c%500;
  b=b+c/100;
  c=c%100;
  b=b+c/50;
  c=c%50;
  b=b+c/10;
  c=c%10;
  b=b+c/5;
  c=c%5;
  b=b+c;
  cout<<b<<endl;
}}}

C++: #include<iostream>
using namespace std;
int main(){
    int n,ans =0;;
    int coin[6] = {500,100,50,10,5,1};
    
    while(cin>>n)
    {
        n = 1000-n;
        for(int i = 0; 1 < 6; i++)
        {
            ans += n/coin[i];
            n %= coin[i];
        }
    }
  return 0;
}