#include<iostream>
#include<algorithm>
using namespace std;
int a[6], dp[1000000], p[6] = { 1,5,10,50,100,500 };
int main() {
	for (int i = 1; i < 1000000; i++)dp[i] = 1 << 30;
	for (int i = 0; i < 6; i++) {
		for (int j = 0; j < 700000; j++) {
			dp[j + p[i]] = min(dp[j + p[i]], dp[j] + 1);
		}
	}
	while (true) {
		int c, res = 100000; cin >> c; for (int i = 0; i < 6; i++) { cin >> a[i]; }if (c == 0)break;
		for (int i = c; i < 670000; i++) {
			int h = i, c1 = 0;
			for (int j = 5; j >= 0; j--) {
				int Z = h / p[j];
				if (Z > a[j]) { h -= a[j] * p[j]; c1 += a[j]; }
				else { h -= Z*p[j]; c1 += Z; }
			}
			if (h == 0) { res = min(res, dp[i - c] + c1); }
		}
		cout << res << endl;
	}
	return 0;
}