Go: ///
// File:  0521.go
// Author: ymiyamoto
//
// Created on Wed Mar  7 22:11:28 2018
//
package main

import (
	"fmt"
)

var money int

func main() {
	for {
		fmt.Scan(&money)
		if money == 0 {
			break
		}
		rem := 1000 - money

		count := 0
		for _, coin := range []int{500, 100, 50, 10, 5, 1} {
			count += rem / coin
			rem %= coin
		}
		fmt.Println(count)
	}
}

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

int main(){
	int price,count,x,n;
	int coin[] = {500,100,50,10,5,1};
	while(cin >> n && n){
		price = 1000 - n;
		count = 0;
		for(int i = 0;i<6;i++){
			x = price / coin[i];
			count += x;
			price -= coin[i] * x; 
		}
		cout << count << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> int_pair;
typedef pair<ll,ll> ll_pair;
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
int c[]={500,100,50,10,5,1};
int cn[6];

int main(){
	int a;
	cin>>a;
	while(a){
		a=1000-a;
		FOR(i,0,6){
			cn[i]=a/c[i];
			a-=cn[i]*c[i];
		}
		int ans=0;
		FOR(i,0,6)ans+=cn[i];
		cout<<ans<<endl;
		cin>>a;
	}

	
	return 0;
}

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

int coin[] = {1, 5, 10, 50, 100, 500};
int remain, in;
int main()
{
  while(cin >> in, in){
    remain = 1000 - in;
    int ans = 0;
    for(int i = 0; i < 6; i++){
      ans += remain / coin[5 - i];
      remain %= coin[5 - i];
    }
    cout << ans << endl;
  }
  return 0;
}

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

 
int main () 
{
  int m1;
  while(1)
  {
   cin>>m1; 
   if(m1>0||m1<=1000)
   {
       break;
   }
   else
    {
    int m2,m3,m4,m5,m6,m7;

   m2=1000-m1;
   m3=m2/500;
   m4=m2%500/100;
   m5=m2%500%100/50;
   m6=m2%500%100%50/10;
   m7=m2%500%100%50%10/1;
  
   cout<<m3+m4+m5+m6+m7<<'\n';
   }
 }
   
  return 0;
}

C++: //08-1 ?????????
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main(){
    bool flg = true;
    while(flg){
        int A;
        cin >> A;
        int ans = 0;
        if(A != 0){
            A = 1000 - A;
            while(A>=500){
                A -= 500;
                ans++;
            }
            while(A>=100){
                A -= 100;
                ans++;
            }
            while(A>=50){
                A -= 50;
                ans++;
            }
            while(A>=10){
                A -= 10;
                ans++;
            }
            while(A>=5){
                A -= 5;
                ans++;
            }
            while(A>=1){
                A -= 1;
                ans++;
            }
            cout << ans << endl;
        }
        else{
            flg = false;
        }

    }
    return 0;
}

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;
}