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

#define rep(i,n) for(int i=0;i<n;i++)
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e10,EPS=1e-9;

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

int pay(int a)
{
	int ret=0;
	rep(i,6)
	{
		int t=min(n[i],a/coin[i]);
		a-=coin[i]*t; ret+=t;
	}
	return a?inf:ret;
}
int change(int a)
{
	int ret=0;
	rep(i,6)ret+=a/coin[i],a%=coin[i];
	return ret;
}

int main()
{
	while(cin>>p,p)
	{
		rep(i,6)cin>>n[5-i];
		int ans=inf;
		for(int i=p;i<=p+1000;i++)ans=min(ans,pay(i)+change(i-p));
		cout<<ans<<endl;
	}
	return 0;
}