#include<iostream>
#include<vector>
using namespace std;

#define NCOIN 6
const int coins[NCOIN] = {1,5,10,50,100,500};
const int infty = 1<<28;

int PaymentAndChange(int N, int v[], bool &possible){
  int ret = 0;
  for(int i = NCOIN-1; i >= 0; --i){
    int t = N/coins[i];
    if( t > v[i] ){
      N -= coins[i] * v[i];
      ret += v[i];
    }else{
      N -= coins[i] * t;
      ret += t;
    }
  }
  if( N == 0 ) possible = true;
  else possible=false;
  return ret;
}

int main()
{
  while(true){
    int ans = infty;
    int P,maxP=0;
    int vc[NCOIN]={0,};
    scanf("%d",&P);
    if(P==0)break;
    for(int i = 0; i < NCOIN; ++i){
      scanf("%d",&vc[i]);
      maxP+=vc[i]*coins[i];
    }
    for(int iP = P; iP <= maxP; ++iP){
      bool pos;
      int t = PaymentAndChange(iP,vc,pos);
      if( !pos ) continue;
      if( t >= ans ) continue;
      t += PaymentAndChange(iP-P,vc,pos);
      if( !pos ) continue;
      ans = min( ans, t );
    }
    printf("%d\n",ans);
  }
  return 0;
}