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++: // 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;
}

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

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

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

using namespace std;

#define print(a) printf("%d\n", a)

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)

#define MAX(a,b) ((a)>(b)?(a):(b))


int main(){
	int co, mod, ch = 0;
	
	cin >> co;
	
	mod = 1000 - co;
	
	ch += mod/500;
	mod = mod%500;
	
	ch += mod/100;
	mod = mod%100;
	
	ch += mod/50;
	mod = mod%50;
	
	ch += mod/10;
	mod = mod%10;
	
	ch += mod/5;
	mod = mod%5;
	
	ch += mod;
	
	cout << ch << 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=0,ans=0;
  
  cin>>num;
  num=1000-num;
  while(num!=0){
    ans += num/money[i];
    num = num%money[i];
    i++;
  }
  printf("%d\n",ans);
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
const int coin[6] = {1,5,10,50,100,500};
const int sen = 1000;
int main(){
  int n;
  cin >> n;
  int oturi = sen - n;
  int sum = 0;
  int cnt = 5;
  while(oturi != 0){
    if(oturi/coin[cnt] != 0){
      sum++;
      oturi -= coin[cnt];
    }
    else{
      cnt--;
    }
  }
  cout << sum << endl;
  return 0;
}

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

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

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

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

int main(){
  int n;
  int coin[] = {500, 100, 50, 10, 5, 1};

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