Go: ///
// File:  0521.go
// Author: ymiyamoto
//
// Created on Wed Mar  7 22:11:28 2018
//
package main

import (
	"fmt"
)

var money int

func main() {
	for {
		fmt.Scan(&money)
		if money == 0 {
			break
		}
		rem := 1000 - money

		count := 0
		for _, coin := range []int{500, 100, 50, 10, 5, 1} {
			count += rem / coin
			rem %= coin
		}
		fmt.Println(count)
	}
}

C++: #include <stdio.h>

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

int main(void) {
	while (true) {
		scanf("%d", &N);
		if (N == 0) { break; }

		int res = 1000 - N;
		while (res != 0) {
			if (res >= 500) {
				ans += res / 500;
				res -= 500 * (res / 500);
			}
			else if (res >= 100) {
				ans += res / 100;
				res -= 100 * (res / 100);
			}
			else if (res >= 50) {
				ans += res / 50;
				res -= 50 * (res / 50);
			}
			else if (res >= 10) {
				ans += res / 10;
				res -= 10 * (res / 10);
			}
			else if (res >= 5) {
				ans += res / 5;
				res -= 5 * (res / 5);
			}
			else { ans += res; res = 0; }
		}
		printf("%d\n", ans);
	}
	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 c = 0;
    for (int i = 0; i < 6; i++){
      static const int c[] = {500, 100, 50, 10, 5, 1};
      c += n / c[i];
      n %= c[i];
    }
    printf("%d\n", c);
  }

  return (0);
}

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

int main() {
  int payment;
  while (1) {
    cin >> payment;
    if (payment == 0) break;

    int change = 1000 - payment;
    int ans = 0;
    ans += change / 500; change %= 500;
    ans += change / 100; change %= 100;
    ans += change / 50; change %= 50;
    ans += change / 10; change %= 10;
    ans += change / 5; change %= 5;
    ans += change;

    cout << ans << endl;
  }

  return 0;
}

C++: #include<cstdio>
using namespace std;
int main(void){
   while(1){
      int n;
      scanf("%d",&n);
      if(n == 0) break;
      n = 1000 - n;
      int cnt = 0;
      while(1){
         if(n >= 500){
            n -= 500;
            cnt++;
         }
         else if(n >= 100){
            n -= 100;
            cnt++;
         }
         else if(n >= 50){
            n -= 50;
            cnt++;
         }
         else if(n >= 10){
            n -= 10;
            cnt++;
         }
         else if(n >= 5){
            n -= 5;
            cnt++;
         }
         else if(n >= 1){
            n -= 1;
            cnt++;
         }
         else break;
      }
      printf("%d\n",cnt);
   }
   return 0;
}

C++: #include <iostream>

using namespace std;

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

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
template<typename V> constexpr typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int x;
  while (cin >> x, x) {
    x     = 1000 - x;
    int c = 0;
    vi v  = {500, 100, 50, 10, 5, 1};
    rep(i, v.size()) {
      c += x / v[i];
      x %= v[i];
    }
    cout << c << endl;
  }
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;

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

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

using namespace std;

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

int getCoin(int &rest, int coinVal) {
	int cnt;

	cnt = rest / coinVal;
	rest %= coinVal;

	return cnt;
}

int main() {
	int n, rest, sum;

	while (cin >> n, n) {
		sum = 0;
		rest = 1000 - n;
		for (int i = 0; i < 6;++i){
			sum += getCoin(rest, CoinType[i]);
		}
		cout << sum << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int a;
	int i;
	int s=0;
	
	cin>>a;
	a=1000-a;
	if(a>499){
		a=a-500;
		s=s+1;
	}
	for(i=0;i<4;i++){
		if(a>99){
			a=a-100;
			s=s+1;
		}
	}
	if(a>49){
		a=a-50;
		s=s+1;
	}
	for(i=0;i<4;i++){
		if(a>9){
			a=a-10;
			s=s+1;
		}
	}
	if(a>4){
		a=a-5;
		s++;
	}
	s=s+a;
	cout<<s<<endl;
}

C++: #include<iostream>
#include<cmath>

using namespace std;
int main()
{
    int x;
    while(cin >> x){
        if(x==0){
            break;
        }
        int cnt=0;
        int a=1000-x;
        cnt+=a/500;
        a%=500;
        cnt+=a/100;
        a%=100;
        cnt+=a/50;
        a%=50;
        cnt+=a/10;
        a%=10;
        cnt+=a/5;
        a%=5;
        cnt+=a;
        cout << cnt <<endl;
    }
    
    return 0;
}

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