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 <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using vs=vector<string>;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF    	((ll)1ll<<60)
#define INF     	((int)1<<30)
#define EPS     	(1e-9)
#define MOD     	(1000000007ll)
#define fcout(a)	cout<<setprecision(a)<<fixed
#define fs 			first
#define sc			second
#define PI			3.1415926535897932384

int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s; a=0;} return os;}
void OUT(bool b){cout<<(b?"YES":"NO")<<endl;}
void Out(bool b){cout<<(b?"Yes":"No")<<endl;}
void out(bool b){cout<<(b?"yes":"no")<<endl;}
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int clz(int x) { return 31 - __builtin_clz(x); }
int clz(ll x) { return 63 - __builtin_clzll(x); }
int ctz(int x) { return __builtin_ctz(x); }
int ctz(ll x) { return __builtin_ctzll(x); }

class compare {
public:
	bool operator()(tuple<ll, ll> a, tuple<ll, ll> b) {
		return (get<1>(a) > get<1>(b));
	}
};

void solve(int n){
	n=1000-n;
	int ans=0;
	int c[5]={500,100,50,10,5};
	rep(i,5){
		while(n-c[i]>=0){
			ans++;
			n-=c[i];
		}
	}
	ans+=n;
	cout<<ans<<endl;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	while(cin>>n,n){
		solve(n);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
  for(int N; cin>>N&&N;){
    int n=1000, a=0;
    while(1) {
      if(n-500>=N) n-=500,a++;
      else if(n-100>=N) n-=100,a++;
      else if(n-50>=N) n-=50,a++;
      else if(n-10>=N) n-=10,a++;
      else if(n-5>=N)n-=5,a++;
      else if(n-1>=N)n--,a++;
      else break;
    }cout <<a<<endl;
  }
  return 0;
}

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

using namespace std;

int calcCoins(int price) {
	const int coinNums[] = {500, 100, 50, 10, 5, 1};
	int result = 0;
	int remain = price;

	for (int i = 0; i < 6; i++) {
		int coinMaisu = remain / coinNums[i];
		result += coinMaisu;
		remain -= coinMaisu * coinNums[i];
	}

	return result;
}

int main(void){
    vector<int> prices;
	while (true) {
		int price;
		cin >> price;

		if (price == 0) break;
		prices.push_back(price);
	}

	for (int i = 0; i < prices.size(); i++) {
		cout << calcCoins(1000 - prices[i]) << endl;
	}
}

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

using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define np string::npos
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define repc(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto n :(array))

double eps = 1e-10 ;

  template <typename T>
void out(vector < T > v)
{
  for(size_t i = 0; i < v.size(); i++)
  {
    debug(v[i]);
  }
}

template<typename T>
string ntos( T i ) {
  ostringstream s ;
  s << i ;
  return s.str() ;
}

template<typename T>
T ston(string str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,ans = 0;
  while(1){
    cin >> n;
    if(n == 0){
      break ;
    }
    n = 1000-n;
    while(n >= 500){
      n -= 500;
      ++ans;
    }
    while(n >= 100){
      ++ans;
      n -= 100;
    }
    while(n >= 50){
      n -= 50;
      ++ans;
    }
    while(n >= 10){
      ++ans;
      n -= 10;
    }
    while(n >= 5){
      n -= 5;
      ++ans;
    }
    while(n >= 1){
      n -= 1;
      ++ans;
    }
    cout << ans<< endl;
    ans = 0;
  }
  return 0;
}

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

int nedan;
int oturi;
int maisuu = 0;
int i;
	

 int main(){
	cin >> oturi;
	i = 1000 - oturi;
	for(i=0 ; i<1000; )
	{
		if(nedan<500)maisuu+=1;
	}
	for(i=0 ; i<1000; )
	{
		if(nedan<100)maisuu+=1;
	}
	for(i=0 ; i<1000; )
	{
		if(nedan<50)maisuu+=1;
	}
	for(i=0 ; i<1000; )
	{
		if(nedan<10)maisuu+=1;
	}
	for(i=0 ; i<1000; )
	{
		if(nedan<5)maisuu+=1;
	}
	for(i=0 ; i<1000; ){
		if(nedan<1)maisuu+=1;
	}
	 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;
  while(cin >> n, n != 0){

    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>
using namespace std;

int main()
{
  int n;

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

    cout << cnt << endl;
  }

  return 0;
}

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

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

int main(){
int buy,oturi,kouka;
cin>>buy;
if(!buy==0){

oturi=1000-buy;
if(oturi>500){
oturi-=500;
kouka+=1;
}
while(oturi>=100){
oturi-=100;
kouka+=1;
}
while(oturi>=50){
oturi-=50;
kouka+=1;
}
while(oturi>=10){
oturi-=10;
kouka+=1;
}
while(oturi>=5){
oturi-=5;
kouka+=1;
}
while(oturi>=1){
oturi-=1;
kouka+=1;
}
cout<<kouka<<'\n';
}
return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
using namespace std;
int main()
{

	cout << 1 << endl;
	cout << 6 << endl;
	cout << 11 << endl;
	cout << 13 << endl;
	cout << 9 << endl;
	return 0;
}