#include <iostream>

using namespace std;

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

	return 0;
}
