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 "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
	while (1) {

		int N; cin >> N;
		if (!N)break;
		N = 1000 - N;
		int ans = 0;
		set<int>s;
		s = { 500,100,50,10,5,1 };
		for (auto m : s) {
			ans += N / m;
			N /= m;
		}
		cout << ans << endl;
	}
	return 0;
}

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

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

C++: #include <iostream>

using namespace std;

int main(int argc, char **argv)
{
	int n, m, c, j;
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		m = 1000-n;
		c = 0;
		
		for(int i = 100; m != 0; i /= 10){
			j = 5 * i;
			if(m >= j){
				m -= j;
				c++;
			}
		
			while(m >= i){
				m -= i;
				c++;
			}
		}
		
		cout << c << endl;
	}
	
	return 0;
}

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

int main() {

  long long int n;
  cin >> n;
  n = 1000 - n;

  long long int ans = 0;
  if ( n >= 500 ) {
    ans++;
    n -= 500;
  }
  ans += n / 100;
  n %= 100;

  if ( n >= 50 ) {
    ans++;
    n -= 50;
  }
  ans += n / 10;
  n %= 10;

  if ( n >= 5 ) {
    ans++;
    n -= 5;
  }
  ans += n / 1;

  cout << ans << endl;

  return 0;

}

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

   cout<<i<<endl;
return 0;
}

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

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int pay, change, i;
	
	cin >> pay;
	
	change = 1000 - pay;

	while(1){
	if(change == 0 || change == 1000) break;
	else if(change < 0 || 1000 < change) break;
	while(1){
		if(change - 500 >= 0){
			i += 1;
			change -= 500;
			break;
		}
		else if(change - 100 >= 0){
			i += 1;
			change -= 100;
			break;
		}
		else if(change - 50 >= 0){
                        i += 1;
                        change -= 50;
                        break;
                }
		else if(change - 10 >= 0){
                        i += 1;
                        change -= 10;
                        break;
                }
		else if(change - 5 >= 0){
                        i += 1;
                        change -= 5;
                        break;
                }
		else{
			i += 1;
			change -= 1;
			break;
		}
	}
	}
	
	if(pay == 0 ) return 0;
        else if(pay < 0 || 1000 < pay) return 0;
	else cout << i << "\n";

	return 0;
}

C++: #include<bits/stdc++.h>
#define enld '\n'
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[6] = {1, 0, -1, 0,1,1};
const int dy[6] = {0, 1, 0, -1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<int> vec = {500,100,50,10,5,1};
    int N;
    cin >> N;
    while(N != 0){
        N = 1000 - N;
        int ans = 0;
        for(int i=0; i<6; i++){
            while(vec[i] <= N){
                N -= vec[i];
                ans++;
            }
        }
        cout << ans << enld;
        cin >> N;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int main()
{
    int x;
    int ans;
    cin >> x;
    while(x!=0) {
        x=1000-x;
        ans=0;
        ans+=x/500;
        x=x%500;
        ans+=x/100;
        x=x%100;
        ans+=x/50;
        x=x%50;
        ans+=x/10;
        x=x%10;
        ans+=x/5;
        x=x%5;
        ans+=x;
        cout << ans << endl;
        cin >> x;
    }
    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;

bool check(int n){
  int k=0,x=n;
  while(x){
    k++;
    x/=10;
  }
  if(k==1){
    return true;
  }else if(k==2){
    if(n%10==n/10)return true;
  }else if(k==3){
    if(n%10==n/100)return true;
  }else if(k==4){
    if((n%10==n/1000)&&(n/10%10==n/100%10))return true;
  }
  return false;
}

int main(){
  int n;
  cin>>n;
  for(int i=0;i<1000;i++){
    if(check(n-i)&&n-i>0){
      cout<<n-i<<endl;
      return 0;
    }
    if(check(n+i)&&n<=10000){
      cout<<n+i<<endl;
      return 0;
    }
  }
  return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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>
#include <fstream>
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 pair<long, long> PLL;
typedef long long LL;
//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))
#define INF 10000000


int main(){
	int n;
	while(cin>>n){
		if(n==0)break;
		int t=1000-n;
		int res=0;
		while(t>=500)res++,t-=500;
		while(t>=100)res++,t-=100;
		while(t>=50)res++,t-=50;
		while(t>=10)res++,t-=10;
		while(t>=5)res++,t-=5;
		res+=t;
		cout<<res<<endl;
	}
	return 0;
}