#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int p,n[6];
int price[]={1,5,10,50,100,500};

int f(int a,int b,int c,int d,int e){
	int pr=p+a+b*5+c*10+d*50+e*100;
	int i;
	int res=0;
	int nn[6];
	for(i=0;i<6;i++)nn[i]=n[i];
	for(i=5;i>=0;){
//cout<<i<<" "<<pr<<endl;
		if(nn[i]==0){i--;continue;}
		if(pr<price[i]){i--;continue;}
		pr-=price[i];
		nn[i]--;
		res++;
	}
//cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<res<<endl;
	if(pr)return 100100100;
	return a+b+c+d+e+res;
}

int main() {
	while(cin>>p>>n[0]>>n[1]>>n[2]>>n[3]>>n[4]>>n[5],p){
		int a,b,c,d,e,res=100100100;
		for(a=0;a<5;a++){
			for(b=0;b<2;b++){
				for(c=0;c<5;c++){
					for(d=0;d<2;d++){
						for(e=0;e<5;e++){
							chmin(res,f(a,b,c,d,e));
						}
					}
				}
			}
		}
		cout<<res<<endl;
	}
	return 0;
}