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>
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;
	}
	cout << tnok + yj <<endl;
 
	return 0;
}

C++: #include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int coin=0;
	int price=0;
	int flag=0;
	cin>>price;
	price=1000-price;
	while(1){
		if(flag==1){
			break;
		}
		if(price>=500){
			price=price-500;
			coin=coin+1;
		}
		else if(price>=100){
			price=price-100;
			coin=coin+1;
		}
		else if(price>=50){
			price=price-50;
			coin=coin+1 ;
		}
		else if(price>=10){
			price=price-10;
			coin=coin+1 ;
		}
		else if(price>=5){
			price=price-5;
			coin=coin+1 ;
		}
		else if(price>=1){
			price=price-1;
			coin=coin+1 ;
		}
		else if(price==0){
			flag=1;
		}
	}
	cout<<coin<<endl;

	return 0;

}

C++: main(){
int money = 1000;int buy = 0;int maisuu = 0;
cin >> buy;
money = money - buy
if(money >= 500){
 	maisuu = maisuu + 1;
 	money = money - 500 ;
 }
if(money >= 100){
 	maisuu = maisuu + money/100;
 	money =money % 100;
  }
if(money >= 50){
 	maisuu = maisuu + 1;
 	money = money - 50 ;
 }
if(money >= 100){
 	maisuu = maisuu + money/10;
 	money =money % 10;
  }
money = money - buy
if(money >= 5){
 	maisuu = maisuu + 1;
 	money = money - 5 ;
 }
maisuu = maisuu + money;
cout << maisuu << endl;
}

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

int count(int x,int c){
    if(x>=500){
        return count(x-500,c+1);
    }
    if(x>=100){
        return count(x-100,c+1);
    }
    if(x>=50){
        return count(x-50,c+1);
    }
    if(x>=10){
        return count(x-10,c+1);
    }
    if(x>=5){
        return count(x-5,c+1);
    }
    if(x==0){
        return c;
    }
    else{
        return count(x-1,c+1);
    }
}
int main(void){
    int x;
    while(true){
        cin >> x;
        if(x==0){
            break;
        }
        cout << count(1000-x,0)<< endl;
    }
}

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

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cctype>
#define rep(i,a,b) for(int i=(a);i<b;i++)
#define INF 1000000000
#define MAX_N 105
using namespace std;
bool debug=false;

int main(){
  int n;
  int coin[6]={500,100,50,10,5,1};
  while(cin>>n){
    if(n==0)break;
    n=1000-n;
    int cnt=0;
    for(int i=0;i<6;){
      if(n>=coin[i]){
	n-=coin[i];
	cnt++;
	cout<<coin[i]<<endl;
      }else{
	i++;
      }
    }
    cout<<cnt<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e15)
#define PI (acos(-1))
#define MOD 1000000007

#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define arrdump(n,a) for(int i=0;i<n;i++) {cout<<((i>0)? " ":"")<<a[i];}cout<<"\n";


typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0521

int main() {
    int coins[]{500, 100, 50, 10, 5, 1};
    while(true) {
        int p; cin >> p;
        if (p == 0) break;
        int change = 1000 - p;
        int ans = 0;
        for (int c : coins) {
            int v = change / c;
            ans += v;
            change -= v * c;
        }
        cout << ans << "\n";
    }

    return 0;
}

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

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

using namespace std;

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

        int a = n / 500;
        n %= 500;

        int b = n / 100;
        n %= 100;

        int c = n / 50;
        n %= 50;

        int d = n / 10;
        n %= 10;

        int e = n / 5;
        int f = n % 5;

        int ans = a + b + c + d + e + f;
        cout << ans << endl;
    }
    return 0;
}

C++: #include<stdio.h>

int main(){
  
  while(1){
    
    int n,cnt=0;
  
  scanf("%d",&n);
  
  if(n==0){break;}

  n=1000-n;
  
  while(n>=500){
    n-=500;
    cnt++;
  }
  while(n>=100){
    n-=100;
    cnt++;
  }
  while(n>=50){
    n-=50;
    cnt++;
  }
  while(n>=10){
    n-=10;
    cnt++;
  }
  while(n>=5){
    n-=5;
    cnt++;
  }
  while(n!=0){
    n--;
    cnt++;
  }

  printf("%d\n",cnt);
  
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)

using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF = 100000000;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
ll n,ans;
vector<int> abcd(4);

  
int main(){
  vector<int> price={500,100,50,10,5,1};
  while(1){
    int x;
    cin >> x;
    if(x==0)break;
    int y = 1000-x;
    int coins=0;
    rep(i,6){
      int t = y/price[i];
      y -= t*price[i];
      coins += t;
    }
    cout << coins << endl;
  }
  return 0;
}