Go: package main

import "fmt"

func main() {
	var n, coins, change int
	for {
		fmt.Scan(&n)
		if n == 0 {
			return
		}
		coins = 0
		change = 1000 - n
		for change > 0 {
			coins++
			switch {
			case change >= 500:
				change -= 500
			case change < 500 && change >= 100:
				change -= 100
			case change < 100 && change >= 50:
				change -= 50
			case change < 50 && change >= 10:
				change -= 10
			case change < 10 && change >= 5:
				change -= 5
			default:
				change--
			}
		}
		fmt.Println(coins)
	}
}

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

C++: #include <cstdio>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <random>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
#include <chrono>
#include <cstdlib>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
#define OUTPUT(i) (cout << (ll)i << endl)
#define ALL(a) ((a).begin(), (a).end())

bool operator<(const pair<ll, ll> &a, const pair<ll, ll> &b)
{

    if (a.first == b.first)
        return a.second < b.second;

    return a.first < b.first;
}

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int N, K;
int ub, lb;
int ans[1008];
vector<vector<int>> v(1001);

bool check()
{

    for (int i = 0; i < K; ++i)
    {
        int cnt = 0;
        for (int j = 0; j < N / 2; ++j)
        {
            cnt += ans[v[i][j]];
        }
        if (cnt < lb || cnt > ub)
            return false;
    }
    return true;
}

void solve()
{
    for (int i = 0; i < N / 2; ++i)
        ans[i] = 1;
    while (1)
    {
        std::random_device seed_gen;
        std::mt19937 engine(seed_gen());
        shuffle(ans, ans + N, engine);
        if (check())
            break;
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    //cout << fixed << setprecision(15);

    int N;
    while (cin >> N, N)
    {

        int t = 1000 - N;

        int cnt = 0;
        while (t >= 500)
        {
            t -= 500;
            cnt++;
        }
        while (t >= 100)
        {
            t -= 100;
            cnt++;
        }
        while (t >= 50)
        {
            t -= 50;
            cnt++;
        }
        while (t >= 10)
        {
            t -= 10;
            cnt++;
        }
        while (t >= 5)
        {
            t -= 5;
            cnt++;
        }

        while (t >= 1)
        {
            t -= 1;
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

int in,cnt;
const int coin[6] = {500,100,50,10,5,1};

int count(int* x,int y,int* c){
   *c += *x / y;
   *x  = *x % y;
}

int main(){
   while(cin >> in,in){
      cnt = 0;
      in  = 1000 - in;
      for(int i=0;i<6;i++)
	 count(&in,coin[i],&cnt);
      cout << cnt << endl;
   }
   return 0;
}

C++: #include<iostream>

using namespace std;

int main()
{
	for (int a = 0; a == 0;)
	{
		int price;
		int oturi;
		int count = 0;
		cin >> price;

		oturi = 1000 - price;

		if (price != 0)
		{
			while (oturi > 0)
			{
				if (oturi >= 500)
				{
					count++;
					oturi -= 500;
				}
				else if (oturi >= 100)
				{
					count++;
					oturi -= 100;
				}
				else if (oturi >= 50)
				{
					count++;
					oturi -= 50;
				}
				else if (oturi >= 10)
				{
					count++;
					oturi -= 10;
				}
				else if (oturi >= 5)
				{
					count++;
					oturi -= 5;
				}
				else if (oturi >= 1)
				{
					count++;
					oturi -= 1;
				}
			}
			cout << count << endl;
		}
		else
		{
			break;
		}

	}
	
	return 0;
}

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

C++: #include <iostream>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

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

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

int main() {
	int a,b=0;
	while(1){
		cin >> a;
		a=1000-a;
		if(a==1000) break;
		while(a>500){
			a-=500;
			b++;
		}
		while(a>100){
			a-=100;
			b++;
		}
		while(a>10){
			a-=10;
			b++;
		}
		while(a>5){
			a-=5;
			b++;
		}
		while(a<0){
			a--;
			b++;
		}
		cout << b << endl;
	}
	return 0;
}

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

int main(void){
	int n;
	while(cin >> n && n){
		int v = 1000 - n;
		int ret = 0;
		int c[] = {500,100,50,10,5,1};
		for(int i=0;i<6;i++){
			ret += v / c[i];
			v -= (v/c[i])*c[i];
		}
		cout << ret << endl;
	}
}

C++: #include <iostream>

int main() {
	int a,coin = 0;
	std::cin >> a;
	a = 1000 - a;

	coin += a / 500;
	a = a - ((a / 500) * 500);
	coin += a / 100;
	a = a - ((a / 100) * 100);
	coin += a / 50;
	a = a - ((a / 50) * 50);
	coin += a / 10;
	a = a - ((a / 10) * 10);
	coin += a / 5;
	a = a - ((a / 5) * 5);
	coin += a / 1;
	a = a - ((a / 1) * 1);

	std::cout << coin;




}

C++: #import<cstdio>
int main(){
	int a,i,n,c[]={0,1,5,10,50,100,500};
	for(;a=0,scanf("%d",&n),n;printf("%d\n",a)){
		for(n=1000-n,i=6;i;i--){
			for(;n>=c[i];a++){
				n-=c[i];
			}
		}
	}
}

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

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

using namespace std;

#define rep(i,n) for(int i = 0; i<n ; i++)
#define FOR(i,a,n) for(int i=a; i<n; i++)
#define REPSTR(i,s) for(int i=0; (s)[i]; i++)
#define REPITR(i,s) for(auto i=(s)begin(); i!=(s).end();i++)

#define RANGE(a,i,b) ((a)<=(i)&&(i)<=(b))

#define pb push_back
#define mp make_pair

#define ALL(a) (a).begin(), (a).end()

#define EXIST(s,x) ((s).find(x)!=(s).end())

#define SET(a,c) memset(a,c,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))

#define DEBUG(x) cerr<<#x<<":"<<x<<endl
#define PRINTJOIN(x,n) rep(i,n){if(i)cout<<" ";cout<<x[i];}cout<<endl 

#define INF 1001001001
#define LLINF 1001001001001001001LL

typedef long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef stringstream SS;

#define PQ priority_queue

#define FST first
#define SEC second

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

//------------------HEAD---------------------

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