#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(),n.end()
#define pb push_back
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define INF (1<<27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<P,P> PP;
int C[6] = {1,5,10,50,100,500};
int Mine[667000],Mine2[667000];
int RR[7],Ps;
int enu[7];
 
 
int getChangeBy2(int sum){// tenin
    int ret = 0;
     
   
     
    assert(sum >= 0);
    for(int i=5;i>=0;i--){
      if(sum >= C[i]){
    ret += sum/C[i];
    sum -= sum/C[i]*C[i];  
      }
    }
    return ret;
  }
  
  
  int getChange2(int sum){
  
    rep(i,6)RR[i] = 0;
    int ret = 0;
    for(int i=5;i>=0;i--){
      if(sum >= C[i] && enu[i] != 0){
    ret += min(sum/C[i],enu[i]);
    RR[i] = min(sum/C[i],enu[i]);
    sum -= min(sum/C[i]*C[i],enu[i]*C[i]);
  
      }
    }
    return ret;
  }
 
void CreateMines(){
  rep(i,667000)Mine[i] = Mine2[i] = 0;
 
  for(int i=1;i<=66500;i++){
    
    
    int B = getChangeBy2(i);
    Mine2[i] = B;
  }
 
}
 
class AOJ1037{
private:
  int P,total;
  int *N;
  int *R;
  //int *Mines;
  //int *Mine2;
public:
  AOJ1037(int P,int *lis):P(P){
    N = new int[7];
    R = new int[7];
    //Mines = new int[667000];
    // Mine2 = new int[667000];
    total = 0;
    rep(i,6)*(N+i) = *(lis+i);
    rep(i,6)*(R+i) = 0;
    //rep(i,667000)Mines[i] = Mine2[i] = -1;
  }
  
  ~AOJ1037(){
    delete [] N;
    delete [] R;
    //delete [] Mines;
  }
  
  int getChangeBy(int sum){// tenin
    int ret = 0;
     
    sum -= P;
     
    assert(sum >= 0);
    for(int i=5;i>=0;i--){
      if(sum >= C[i]){
    ret += sum/C[i];
    sum -= sum/C[i]*C[i];  
      }
    }
    return ret;
  }
  
  
  int getChange(int sum){
  
    rep(i,6)*(R+i) = 0;
    int ret = 0;
    for(int i=5;i>=0;i--){
      if(sum >= C[i] && N[i] != 0){
    ret += min(sum/C[i],N[i]);
    *(R+i) = min(sum/C[i],N[i]);
    sum -= min(sum/C[i]*C[i],N[i]*C[i]);
  
      }
    }
    return ret;
  }
  
  
  
  
  
  
  
  void run(){
    int limit = 0;
    int MINE = INT_MAX;
    rep(i,6)limit += C[i]*N[i];
   
  
    for(int i = P;i<=limit;i++){
      int bb;
      bb = getChange(i);
  
      if(bb >= MINE)continue;
      int pay = 0;
      rep(j,6)pay += (*(R+j))*C[j];
      //cout << "pay = "<< pay << endl;
      int A;
       A = Mine2[pay-P];
      //if(i == 1000)cout << "A= "<< A << ", B = "<< B << endl;
      if(MINE > A+bb){
    MINE = min(MINE,A+bb);
  
    if(MINE == 1)break;
      }    
    }
    cout << MINE << endl;
    
  } 
  
};
  
  
  
int main(){
  CreateMines();
  while(true){
    int p;
    cin >> p;
if(p == 0)break;
    int sim = p;
    Ps = p;
    rep(i,6){
      cin >> enu[i];
      sim += enu[i];
    }
    if(sim == 0)break;
    
    AOJ1037 ans = AOJ1037(p,enu);
    ans.run();
  }
return 0;
}

Measured memory_bytes: 6404