/*
  AOJ 1028
  Title:ICPC: Ideal Coin Payment and Change
  @kankichi573
*/
#include <stdio.h>

#define min(x,y) (((x)<(y))?(x):(y))

int p,n1,n5,n10,n50,n100,n500;

int change_cnt(int change)
{
  int ret;

  ret = change/100;
  change %= 100;
  ret += change>50?1:0;
  change %= 50;
  ret += change/10;
  change %= 10;
  ret += change>5?1:0;
  change %= 5;
  ret += change;
  return(ret);
}
int ceiling(int n,int m)
{
  if(0 == n % m)
    return(n/m);
  else
    return(n/m + 1);
}
solve()
{
  int i,j,k,l,m,n;
  int l1,l5,l10,l50,l100;
  int min_ = 999;
  int cnt,harai;

  for(i=0;i<=min(n1,p);i++)
    for(j=0;j<=min(n5,ceiling(p,5));j++)
      for(k=0;k<=min(n10,ceiling(p,10));k++)
	for(l=0;l<=min(n50,ceiling(p,50));l++)
	  for(m=0;m<=min(n100,ceiling(p,100));m++)
	    for(n=0;n<=min(n500,ceiling(p,500));n++)
	      {
		harai=i+5*j+10*k+50*l+100*m+500*n;
		//printf("harai=%d\n",harai);		    
		if(harai < p)
		  continue;
		cnt=i+j+k+l+m+n+change_cnt(harai-p);
		if(cnt<min_)
		  {
		    /*
		    printf("harai=%d cnt=%d change=%d cnt=%d\n",
		    harai,cnt,harai-p,change_cnt(harai-p)); */
		    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);
}