/*
  AOJ 1028
  Title:ICPC: Ideal Coin Payment and Change
  @kankichi573
  14/02/21 TLE
  14/03/19 WA
*/
#include <stdio.h>

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

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);
}
int limit_5(int n5,int z500,int z100,int z50,int z10)
{
  int ret;

  if(z10)
    ret=1;
  else if(z50)
    ret=9;
  else if(z100)
    ret=19;
  else if(z500)
    ret=99;
  else
    ret=200;
  return(min3(n5,ret,200));
}
int limit_1(int n1,int z500,int z100,int z50,int z10,int z5)
{
  int ret;

  if(z5)
    ret=4;
  else if(z10)
    ret=9;
  else if(z50)
    ret=49;
  else if(z100)
    ret=99;
  else if(z500)
    ret=499;
  else
    ret=1000;
  return(min3(n1,ret,1000));
}
solve()
{
  int i,j,k,l,m,n;
  int z5,z10,z50,z100,z500,l500;
  int min_ = 999;
  int cnt,harai[6],yoryo[6];
  int min_t,min_c;
  

  yoryo[4]=n1;   // 1yen nomi no shiharai yoryoku
  yoryo[3]=n1+n5*5; // 1yen + 5 yen
  yoryo[2]=yoryo[3]+n10*10; //1,5,10yen
  yoryo[1]=yoryo[2]+n50*50; //1,5,10,50
  yoryo[0]=yoryo[1]+n100*100; //1,5,10,50,100
  l500=ceiling(p,500);
  //printf("l500=%d n500=%d min=%d\n",l500,n500,min(n500,l500));

  for(n=min(n500,l500);n>=0;n--)
    {
      harai[0]=500*n;
      if(harai[0]+yoryo[0]<p)
	break;
      z500=n500-n;
      for(m=min(n100,10);m>=0;m--)
	{
	  harai[1]=harai[0]+100*m;
	  if(harai[1]+yoryo[1]<p)
	    break;
	  z100=n100-m;
	  for(l=min(n50,20);l>=0;l--)
	    {
	      harai[2]=harai[1]+50*l;
	      if(harai[2]+yoryo[2]<p)
		break;
	      z50=n50-l;
	      for(k=min(n10,100);k>=0;k--)
		{
		  harai[3]=harai[2]+10*k;
		  if(harai[3]+yoryo[3]<p)
		    break;
		  z10=n10-k;
		  for(j=limit_5(n5,z500,z100,z50,z10);j>=0;j--)
		    {
		      harai[4]=harai[3]+5*j;
		      if(harai[4]+yoryo[4] < p)
		      	break;
		      z5=n5-j;
		      for(i=limit_1(n1,z500,z100,z50,z10,z5);i>=0;i--)
			{
			  harai[5]=harai[4]+i;
#ifdef DEBUG
			  printf("harai=%d\n",harai[5]);		    
#endif
			  if(harai[5] < p)
			    break;
			  cnt=i+j+k+l+m+n+change_cnt(harai[5]-p);
			  if(cnt<min_)
			    {
			      min_=cnt;
			      min_t=harai[5];
			      min_c=(harai[5]-p); 
#ifdef DEBUG			      
			      printf("harai=%d cnt=%d change=%d cnt=%d\n",
			          harai[5],cnt,harai[5]-p,change_cnt(harai[5]-p)); 
#endif
			    }
			}
		    }
		}
	    }
	}
    }
#ifdef DEBUG
  printf("harai=%d (%d)\n",min_t,min_c);
#endif
  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);
}