#include <iostream>
using namespace std;
int main(void){
    int m,n;
    cin >> m;
    while(m!=0){
       n = 1000 - m;
       cout << n/500 + (n%500)/100 +(n%100)/50+ (n%50)/10 +(n%10)/5 + (n%5) << endl;
       cin >> m;
    }
}
