u[6]={1,5,10,50,100,500};
main(p){
	int n[6],i,b,c,m,r,t;
	for(;scanf("%d",&p)*p;){
		for(i=0;i<6;i++)
			scanf("%d",n+i);
		m=1e9;
		for(b=0;b<500;b++){//0`499~½­x¥¤
			c=0;
			r=p+b;//x¥¤àz
			//è¿ÌdÝÅx¥¦é©`FbN
			for(i=6;i--;){
				t=r/u[i];
				if(t>n[i])
					t=n[i];
				c+=t;
				r-=t*u[i];
			}
			if(r==0){//x¥¦é
				c+=b%5+b/5%2+b/10%5+b/50%2+b/100%5;
				if(c<m)
					m=c;
			}
		}
		printf("%d\n",m);
	}
}