#include <cstdio>
#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) REP(i,0,n)

int a[] = { 1, 5, 10, 50, 100, 500 };
int N[6];
int ret[666667];

int min(int x, int y) { return x < y ? x : y; }

int main()
{
	REP(p, 1, 666667) {
		int t = p;
		rep(i, 6) { int k=t/a[5-i]; ret[p]+=k; t-=k*a[5-i]; }
	}
	int P;
	while (scanf("%d", &P), P) {
		rep(i, 6) scanf("%d", N+i);

		int res = (1<<29);
		REP(p, P, P+501) {
			int t = p;
			int pay = 0;
			rep(i, 6) {
				int k=min(N[5-i], t/a[5-i]);
				pay+=k;
				if (pay >= res) goto NEXT;
				t-=k*a[5-i];
			}
			if (!t) res = min(res, pay+ret[p-P]);
NEXT:;
		}
		printf("%d\n", res);
	}
	return 0;
}