Go: package main

import "fmt"

func main() {
	var n, coins, change int
	for {
		fmt.Scan(&n)
		if n == 0 {
			return
		}
		coins = 0
		change = 1000 - n
		for change > 0 {
			coins++
			switch {
			case change >= 500:
				change -= 500
			case change < 500 && change >= 100:
				change -= 100
			case change < 100 && change >= 50:
				change -= 50
			case change < 50 && change >= 10:
				change -= 10
			case change < 10 && change >= 5:
				change -= 5
			default:
				change--
			}
		}
		fmt.Println(coins)
	}
}

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

int main() {
	int x;
	cin>>x;	
	while(x!=0){
	
	int n;
	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;
	}
	n+=x;
	cout<<n<<endl;
	cin>>x;
	}
	
	
	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main() {
	int tnok, yj;
	cin >> tnok;
	tnok = 1000 - tnok;
	if(tnok >= 500){
		yj = 1;
		tnok = tnok - 500;
	} else {
		yj = 0;
	}
	if(tnok >= 400){
		yj = yj + 4;
		tnok = tnok - 400;
	}
	else if(tnok >= 300){
		yj = yj + 3;
		tnok = tnok - 300;
	}
	else if(tnok >= 200){
		yj = yj + 2;
		tnok = tnok - 200;
	}
	else if(tnok >= 100){
		yj = yj + 1;
		tnok = tnok - 100;
	}
	if(tnok >= 50){
		yj = yj + 1;
		tnok = tnok - 50;
	}
	if(tnok >= 40){
		yj = yj + 4;
		tnok = tnok - 40;
	}
	else if(tnok >= 30){
		yj = yj + 3;
		tnok = tnok - 30;
	}
	else if(tnok >= 20){
		yj = yj + 2;
		tnok = tnok - 20;
	}
	else if(tnok >= 10){
		yj = yj + 1;
		tnok = tnok - 10;
	}
	if(tnok >= 5){
		yj = yj + 1;
		tnok = tnok - 5;
	}
	if(tnok >= 4){
		yj = yj + 4;
		tnok = tnok - 4;
	}
	else if(tnok >= 3){
		yj = yj + 3;
		tnok = tnok - 3;
	}
	else if(tnok >= 2){
		yj = yj + 2;
		tnok = tnok - 2;
	}
	else if(tnok >= 1){
		yj = yj + 1;
		tnok = tnok - 1;
	}
	cout << yj <<endl;
 
	return 0;
}

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

int main(){
    int n;
    int charge;
    int cnt = 0;

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

    while(cin >> n, n != 0){
        charge = 1000 - n;
        for(int i=0; i<6; i++){
            int j = 0;
            while(charge - coin[i] * j >= coin[i]){
                j++;
            }
            charge -= coin[i] * j;
            cnt += j;
        }

        cout << cnt << endl;
        cnt = 0;
    }
    return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,s,ss,z,z1,z2,z3,z4;
	scanf("%d",&n);
	ss=1000-n;
	s=ss/500;
	z=ss%500;
	s+=z/100;
	z1=z%100;
	s+=z1/50;
	z2=z1%50;
	s+=z2/10;
	z3=z2%10;
	s+=z3/5;
	z4=z3%5;
	s+=z4/1;
	printf("%d\n",s);
	return 0;
}

C++: #include <iostream>

using namespace std;

int main() {
  while(true) {
    //input
    int num;
    cin >> num;
    //if 0 finish
    if(num==0) break;

    //oturi
    num = 1000 - num;

    int ans = 0;

    //500yen
    if(num>=500) {
      ans++;
      num-=500;
    }

    //100yen
    while(num>=100) {
      ans++;
      num-=100;
    }

    //50yen
    if(num>=50) {
      ans++;
      num-=50;
    }

    //10yen
    while(num>=10) {
      ans++;
      num-=10;
    }

    //5yen
    if(num>=5) {
      ans++;
      num-=5;
    }

    //1yen
    while(num>=1) {
      ans++;
      num-=1;
    }

    cout << ans << endl;

  }

  return 0;
}

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

int main(){
  int m;
  while(cin >> m,m){
    int sum = 0;
    m = 1000 - m;
    sum += m / 500;
    m %= 500;
    sum += m / 100;
    m %= 100;
    sum += m / 50;
    m %= 50;
    sum += m / 10;
    m %= 10;
    sum += m / 5;
    m %= 5;
    cout << sum + m << endl;
  }
}

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;
}