#define INF 1e9
N[6],U[6]={1,5,10,50,100,500};
C[7][666000*2+1];//-666000`+666000
R(r,k){//r~ðkíÞÌdÝÅx¥¢{¨ÞèÌÅ¬
	int i,c,m=INF,b;
	if(C[k][r+666000]!=-1)
		return C[k][r+666000];
	if(k>0){
		for(i=0;i<=N[k-1];i++){
			c=R(r-U[k-1]*i,k-1)+i;
			if(c<m)
				m=c;
			if(r-U[k-1]*i<=0)
				break;
		}
	}else{
		b=-r;
		if(b>=0)
			m=b%5+b/5%2+b/10%5+b/50%2+b/100%5+b/500;
	}
	return C[k][r+666000]=m;
}
main(P){
	int i;
	for(;scanf("%d",&P)*P;){
		memset(C,-1,sizeof(C));
		for(i=0;i<6;i++)
			scanf("%d",N+i);
		printf("%d \n",R(P,6));
	}
}