#include<bits/stdc++.h>
using namespace std;
int main(){
	int m;
	while(cin>>m,m){
    	int c[]={1,5,10,50,100,500};
    	m=1000-m;
    	int ans=0;
    	for(int i=5;i>=0;i--){
    		ans+=m/c[i];
    		m%=c[i];
    	}
    	cout<<ans<<endl;
	}
}

