#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;

int n[6];
int coin[]{1,10,50,100,500};
vector<P>v;
void solve(int cnt,int sum,int pos){
	if(pos==7){
		v.push_back(P(cnt,sum));
		return;
	}
	rep(i,n[pos]+1)solve(cnt+i,sum+coin[pos]*i,pos+1);	
}
int main(){
	int p;
	while(scanf("%d",&p),p){
		rep(i,6)scanf("%d",&n[i]);
		solve(0,0,0);
		int Min=INT_MAX;
		for(P p:v){
			int cnt=0;
			rep(i,6){
				cnt+=p.second/coin[i];
				p.second%=coin[i];
			}
			Min=min(Min,p.first+cnt);
		}
		printf("%d\n",Min);
	}
}