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 <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int main() {
	int harai, oturi, koukakei ;/*?????£??????????????£????????¬??¨????¨?*/
	//int s500, s100, s50, s10, s5, s1 ;/*??¬??¨????????°*/
	div_t a500, a100, a50, a10, a5, a1 ;/*?????????*/
	scanf("%d", &harai);
	oturi = 1000 - harai ;
	a500 = div(oturi, 500);
	a100 = div(a500.rem, 100);
	a50 = div(a100.rem, 50);
	a10 = div(a50.rem, 10);
	a5 = div(a10.rem, 5);
	koukakei = a500.quot + a100.quot + a50.quot +a10.quot + a5.quot + a5.rem ;
	printf("%d", koukakei);
	return 0;
}

C++: #include <iostream>

using namespace std;

int main(void)
{
	int cost;
	int ret;
	int count;
	
	while (1){
		cin >> cost;
		if (cost == 0)break;
		ret = 1000 - cost;
		
		count = 0;
		count += ret / 500;
		ret %= 500;
		count += ret / 100;
		ret %= 100;
		count += ret / 50;
		ret %= 50;
		count += ret / 10;
		ret %= 10;
		count += ret / 5;
		ret %= 5;
		count += ret;
		
		cout << count << endl;
	}
	
	return (0);
}

C++: #include <iostream>
using namespace std;
int main(){
	int n,m;
	cin >> n;
	m = 0;
	n = 1000-n;

	while( n <= 500 ){
		n = n-500;
		m++;
	}
	while( n <= 100 ){
		n = n-100;
		m++;
	}
	while( n <= 50 ){
		n = n-50;
		m++;
	}
	while( n <= 10 ){
		n = n-10;
		m++;
	}
	while( n <= 5 ){
		n = n-5;
		m++;
	}
	while( n <= 1){
		n = n-1;
		m++;
	}

	cout << m << endl;

	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
	int n,m;
	cin >> n;
	m = 0;
	n = 1000-n;

	if(n>=500){
		m = n/500;
		n = n%500;
	}
	if(n>=100){
		m = m+n/100;
		n = n%100;
	}
	if(n>=50){
		m = m+n/50;
		n = n%50;
	}
	if(n>=10){
		m = m+n/10;
		n = n%10;
	}
	if(n>=5){
		m = m+n/5;
		n = n%5;
	}
	if(n>=1){
		m = m+n/1;
	}

	cout << m << endl;

}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb(in,tmp) in.push_back(tmp)
#define all(in) in.begin(),n.end()
#define PI acos(-1)
using namespace std;
int main(){
  int m;
  while(cin>>m,m){
  int co=0;
  int o=1000-m;
  if(o>500){co++;o-=500;}
  while(o>=100){co++;o-=100;}
  while(o>=50){co++;o-=50;}
  while(o>=10){co++;o-=10;}
  while(o>=5){co++;o-=5;}
  while(o>=1){co++;o--;}

  cout<<co<<endl;
  }
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

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

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

  while(1) {
    cin >> money;
    if(!money) break;

    change = 1000-money;

    int cnt = 0;
    REP(i, 6) {
      while(change/coin[i] > 0) {	
	cnt++;
	change -= coin[i];
      }
    }
    cout << cnt << endl;
  }
}

C++: #include <iostream>
using namespace std;
int main(void){
    int x,y,a,b,c,d,e,f;
    while (true){
    cin >> x;
    if(x==0){
        break;
    }
    a=(1000-x)/500;
    b=(1000-x-500*a)/100;
    c=(1000-x-500*a-100*b)/50;
    d=(1000-x-500*a-100*b-50*c)/10;
    e=(1000-x-500*a-100*b-50*c-10*d)/5;
    f=1000-x-500*a-100*b-50*c-10*d-5*e;
    y=a+b+c+d+e+f;
    

    
        cout <<y <<endl;
    }
    
}

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

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

int main(void){
	int mon, cnt;
	while(true){
		cin >> mon;
		if(mon == 0)	break;
		mon = 1000 - mon;
		cnt = 0;
		for(int i = 0; i < 6; i++){
			if(mon == 0)	break;
			cnt += mon / change[i];
			mon -= change[i] * (mon / change[i]);
		}
		cout << cnt << endl;
	}
  return 0;
}

C++: #include <iostream>
using namespace std;
int coin[6] = {500,100,50,10,5,1};

int main() {
  int price,change,res=0;
  while(cin >> price) {
    if(price == 0) break;
    change = 1000 - price;
    for(int i=0;i<6;i++) {
      res += change/coin[i];
      change -= (change/coin[i]) * coin[i];
    }
    cout << res << endl;
    res = 0;
  }
}

C++: #include<iostream>
using namespace std;
int main(){
	int a;
	int i;
	int s=0;
	
	while(cin>>a){
		if(a==0)break;
		s=0;
		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;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a,b) (abs((a)-(b))<EPS)
int m;
int cal(int a)
{
	int k=m/a;
	m-=a*k;
	return k;
}
int main()
{
	while(1)
	{
		int n;
		cin >> n;
		if(n==0)break;
		m=1000-n;
		int ans=0;
		ans+=cal(500);
		ans+=cal(100);
		ans+=cal(50);
		ans+=cal(10);
		ans+=cal(5);
		ans+=cal(1);
		cout << ans << endl;
	}
	return 0;
}