#include<iostream>
#include<algorithm>
#include<vector>
typedef long long int ll;
using namespace std;

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