/*
  AOJ 1028
  Title:ICPC: Ideal Coin Payment and Change
  @kankichi573
  14/02/21 TLE
  14/03/19 WA
*/
#include <stdio.h>
#include <limits.h>
#define min(x,y) (((x)<(y))?(x):(y))

int p,n1,n5,n10,n50,n100,n500;
int coin[6]={500,100,50,10,5,1};

int change_count(int x)
{
  int i,ret;
  ret=0;
  for(i=0;i<6;i++)
    {
      ret += x / coin[i];
      x %= coin[i];
    }
  return(ret);
}

solve()
{
  int c[6]; //500 100 50 10 5 1
  int sum,change,cnt,min_;
  min_=INT_MAX;
  for(c[0]=0;c[0]<=min(n500,2);c[0]++)
    for(c[1]=0;c[1]<=min(n100,10);c[1]++)
      for(c[2]=0;c[2]<=min(n50,20);c[2]++)
	for(c[3]=0;c[3]<=min(n10,100);c[3]++)
	  for(c[4]=0;c[4]<=min(n5,200);c[4]++)
	    for(c[5]=0;c[5]<=min(n1,1000);c[5]++)
	      {
		sum=c[0]*500+c[1]*100+c[2]*50+c[3]*10+c[4]*5+c[5];
		change=sum-p;
		if(change<0)
		  continue;
		cnt=c[0]+c[1]+c[2]+c[3]+c[4]+c[5]+change_count(change);
		min_=min(min_,cnt);
	      }
  return(min_);
}

main()
{
  while(scanf("%d %d %d %d %d %d %d",&p,&n1,&n5,&n10,&n50,&n100,&n500)
	&& (p || n1 || n5 || n10 || n50 || n100 || n500 ))
      printf("%d\n",solve());

  return(0);
}