#include <iostream>
#include <algorithm>

using namespace std;

int change(int a){
  int cc = 0;
  while(a>0){
    if (a%10>=5){
      cc++;
      a-=5;
    }
    cc+=a%10;
    a/=10;
  }
  return cc;
}

int main(){
  int p,n[6];
  int money[6] = {500,100,50,10,5,1};
  while(true){
    cin >> p;
    if (p==0)
      break;
    cin >>n[5] >>n[4] >>n[3] >>n[2] >>n[1] >>n[0]; 
    int count = 0;
    if (p/money[0]<=n[0]){
      count += p/money[0];
      n[0] -= p/money[0];
      p%=money[0];
    }
    else{
      count += n[0];
      p -= n[0]*money[0];
      n[0] = 0;
    }
    int min = 10000;
    int nn[6];
    int m,c;
    for(int i=p;i<=p+500;i++){
      m = i;
      c = 0;
      copy(n,n+6,nn);
      for(int j=0;j<6;j++){
	if (m/money[j]<=nn[j]){
	  c += m/money[j];
	  nn[j] -= m/money[j];
	  m %= money[j];
	}
	else{
	  c += nn[j];
	  m -= nn[j]*money[j];
	  nn[j] = 0;	
	}
      }
      if (m==0&&min>(c+count+change(i-p))){
	min = c+count+change(i-p);
      }
    }
    cout << min << endl;
  }
  return 0;
}