#include <stdio.h>
#include <stdlib.h>

#define N   6
#define MAX (1000 * 6 + 1)

int n[N];
int coin[N] = { 1, 5, 10, 50, 100, 500 };

// m????????????????????¬??¨??§??´??£?????????????????´?????????????????¬??¨????????°????????????
// ??????????????´??????-1????????????
int pay(int m) {
    int i;
    int rep = 0;

    for( i = N - 1; i >= 0; i-- ) {
        int cnt = m / coin[i];

        if( cnt > n[i] ) {
            cnt = n[i];
        }
        m -= coin[i] * cnt;
        rep += cnt;
    }

    return m == 0 ? rep : -1;
}

// m???????????£????????¬??¨??§??????????????????????????°???????°???????
int change(int m) {
    int i;
    int rep = 0;

    for( i = N - 1; i >= 0; i-- ) {
        int cnt = m / coin[i];
        rep += cnt;
        m -= cnt * coin[i];
    }

    return rep;
}

int main(void) {
    int p;
    int i;

    scanf("%d", &p);
    if( p == 0 ) return 0;
    do {
        int sum = 0;    // ??????????????¬??¨???????¨???????
        int min = MAX;
        for( i = 0; i < N; i++ ) {
            scanf("%d", n + i);
            sum += n[i] * coin[i];
        }

        for( i = p; i <= sum; i++ ) {
            int tarocnt = pay(i);
            if( tarocnt != -1 ) {
                int tmp = tarocnt + change(i - p);
                min = min < tmp ? min : tmp;
            }
        }

        printf("%d\n", min);

        scanf("%d", &p);
    } while( p != 0 );

    return 0;
}