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>
#define REP(i, a, n) for(int a = 0; i < n; i++)
using namespace std;


int n, d, cnt;

int main(void) {
  while(cin >> n, n != 0) {
    d = 1000 - n;

    cnt = 0;
    while(d >= 500) d -= 500, cnt++;
    while(d >= 100) d -= 100, cnt++;
    while(d >=  50) d -=  50, cnt++;
    while(d >=  10) d -=  10, cnt++;
    while(d >=   5) d -=   5, cnt++;
    while(d >=   1) d -=   1, cnt++;

    cout << cnt << endl;
  }

  return 0;
}

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

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

C++: #include <iostream>

using namespace std;

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

int main()
{
	int n;
	while (cin >> n && 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 <iostream>
using namespace std;

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

int main() {

	int change, coins;

	cin >> change;
	change = 1000 - change;

	coins = 0;
	for (int i = 0; i < 6; ++i) {
		if (change >= coin[i]) {
			coins += change / coin[i];
			change %= change / coin[i];
			if (!change)
				break;
		}
	}
	cout << coins << endl;

	return 0;
}

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

int solve(int cost){
  int count=0;
  cost=1000-cost;
  while(cost>=500)count++,cost-=500;
  while(cost>=100)count++,cost-=100;
  while(cost>=50)count++,cost-=50;
  while(cost>=10)count++,cost-=10;
  while(cost>=5)count++,cost-=5;
  while(cost>=1)count++,cost-=1;
  return count;
}

int main(){
  int input;
  cin>>input;
  while(input){
    cout<<solve(input)<<endl;
    cin>>input;
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

int main(void){
	
	int coin[6]={500,100,50,10,5,1},price,sum;
	while(cin>>price,price){
		sum=0;
		price=1000-price;
		for(int i=0;i<6;i++){
			while(price>=coin[i]){
				price=price-coin[i];
				sum++;
			}
		}
		cout<<sum<<endl;
	}
}

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

int main() {
  for (int i = 0; i < 5; i++) {
    int n; cin >> n;
    
    if (n == 0) break;
    int ch = 1000 - n;
    
    int ans = 0;
    if (ch > 0 && ch >= 500) {
      ans += ch / 500;
      ch %= 500;
    }
    if (ch > 0 && ch >= 100) {
      ans += ch / 100;
      ch %= 100;
    }
    if (ch > 0 && ch >= 50) {
      ans += ch / 50;
      ch %= 50;
    }
    if (ch > 0 && ch >= 10) {
      ans += ch / 10;
      ch %= 10;
    }
    if (ch > 0 && ch >= 5) {
      ans += ch / 5;
      ch %= 5;
    }
    if (ch > 0 && ch >= 1) {
      ans += ch / 1;
    }
    
    cout << ans << endl;
  }
}

C++: #include <cstdio>

int main() {
	int num,n,i,a,b;
	a=0,b=0,i=0;
	scanf("%d",&num);
	n=1000-num;

	if (n>500) {
		i++;
		n=(n-500);
	}
	if (n>100){
		a=n/100;
		i=a+i;
		n=n%100;
	}
	if (n>50){
		i++;
		n=(n-50);
	}
	if (n>10){
		b=n/10;
		i=b+i;
		n=n%10;
	}
	if (n>5){
		i++;
		n=(n-5);
	}
		i=(i+n);
	printf("%d\n",i);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

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

int main() {
  while (true) {
    int p;
    cin >> p;
    if (!p)
      break;
    int ans{};
    p = 1000 - p;
    for (int i{}; i < 6; ++i) {
      ans += p / coins[i];
      p %= coins[i];
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <cstring>

#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){

	int change, money, pay;
	int c[6] = {500, 100, 50, 10, 5, 1};	

	cin >> pay;
	money = 1000 - pay;
	change = 0;
	
	rep(i, 6){
		change += money / c[i];	
		money = money % c[i];
	}

	cout << change << endl;

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef long long ll;
typedef pair<int,int> pii;
#define ITR(v,c) for(auto v=begin(c);v!=end(c);v++)
#define FOR(v,a,n) for(int v=a;v<(int)(n);v++)
#define FORE(x,arr) for(auto &x:arr)
#define REP(v,n) FOR(v,0,n)
#define RREP(v,n) for(int v=(int)(n);v>=0;v--)
#define ALL(c) begin(c),end(c)
const int DX[4]={0,1,0,-1}, DY[4]={-1,0,1,0};
const int INF = 1e9;
const ll INFLL = 1e18;
template<class T,class U>ostream&operator<<(ostream &os,const pair<T,U> &p){
    os<<"("<<p.first<<","<<p.second<<")";return os;}
template<class T>ostream&operator<<(ostream &os,const vector<T> &v){
    ITR(i,v)os<<*i<<(i==end(v)-1?"":" ");return os;}
//------------------------------------------------------------------------------

signed main() {
    while(1) {
        int n;
        cin>>n;
        if(n==0) break;
        n=1000-n;
        vector<int> a={500,100,50,10,5,1};
        int ans=0;
        FORE(x,a) {
            ans+=n/x;
            n%=x;
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int main() {
  int M;
  cin >> M;
  int m = 1000 - M;

  int ans = 0;
  while (m > 0) {
    if (m >= 500) {
      m -= 500;
    } else if (m >= 100) {
      m -= 100;
    } else if (m >= 50) {
      m -= 50;
    } else if (m >= 10) {
      m -= 10;
    } else if (m >= 5) {
      m -= 5;
    } else {
      m--;
    }

    ans++;
  }

  cout << ans << endl;

  return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
    int n;
    int coins[] = {500, 100, 50, 10, 5};
    while (cin >> n, n) {
        int ans = 0;
        int m = 1000 - n;
        rep(i, 5) {
            ans += m / coins[i];
            m %= coins[i];
        }
        ans += m;
        cout << ans << endl;
    }
    return 0;
}