#include <stdio.h>
#include <stdlib.h>

#define MIN(X,Y) ((Y) < (X) ? (Y) : (X))

static int p;
static int d[6];
static const int w[6] = {1, 5, 10, 50, 100, 500};

requsr(v)
{
  int rval = 0;
  int i;
  for (i = 5; i >= 0; --i) {
    rval += MIN(v / w[i], d[i]);
    v -= w[i] * MIN(v / w[i], d[i]);
  }
  if (v != 0) return -1;
  return rval;
}

reqsys(v)
{
  int rval = 0;
  int i;
  for (i = 5; i >= 0; --i) {
    rval += v / w[i];
    v %= w[i];
  }
  return rval;
}

main()
{
  int i;
  
  for (;;) {
    int res;
    
    scanf("%d", &p);
    for (i = 0; i < 6; ++i)
      scanf("%d", &d[i]);
    if (p == 0) break ;
    
    res = 6000;
    for (i = 1000 * (1 + 5 + 10 + 50 + 100 + 500); i >= p; --i) {
      const int rusr = requsr(i);
      const int rsys = reqsys(i - p);
      if (rusr < 0) continue ;
      res = MIN(res, rusr + rsys);
    }
    printf("%d\n", res);
  }

  return EXIT_SUCCESS;
}