u[]={1,5,10,50,100,500},n[6],i,b,c,m,r,t;main(p,s){for(;scanf(s="%d\n",&p)*p;printf(s,m)){for(i=0;i<6;m=1e9)scanf(s,n+i++);for(b=500;b--;c+=b%5+b/5%2+b/10%5+b/50%2+b/100%5,m=r||c>m?m:c)for(c=0,r=p+b,i=6;i--;r-=t*u[i])t=fmin(r/u[i],n[i]),c+=t;}}