You have a set of items, each having some integer weight not greater than 8. You denote that a subset of items is good if total weight of items in the subset does not exceed W.

You want to calculate the maximum possible weight of a good subset of items. Note that you have to consider the empty set and the original set when calculating the answer.

Input

The first line contains one integer W (0 ≤ W ≤ 10^{18}) — the maximum total weight of a good subset.

The second line denotes the set of items you have. It contains 8 integers cnt_1, cnt_2, ..., cnt_8 (0 ≤ cnt_i ≤ 10^{16}), where cnt_i is the number of items having weight i in the set.

Output

Print one integer — the maximum possible weight of a good subset of items.

Examples

Input


10
1 2 3 4 5 6 7 8


Output


10


Input


0
0 0 0 0 0 0 0 0


Output


0


Input


3
0 4 1 0 0 9 8 3


Output


3
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EKnapsack solver = new EKnapsack();
        solver.solve(1, in, out);
        out.close();
    }

    static class EKnapsack {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            long w = in.scanLong();
            long arr[] = new long[8 + 1];
            for (int i = 1; i <= 8; i++) arr[i] = in.scanLong();
            long dp[][] = new long[9][(840 * 8) + 1];
            for (int i = 0; i <= 8; i++) Arrays.fill(dp[i], Long.MIN_VALUE / 2);


            dp[0][0] = 0;
            for (int i = 1; i <= 8; i++) {
                for (int sum = 0; sum <= 8 * 840l; sum++) {
                    for (int count = 0; count <= Math.min(arr[i], 840 / i); count++) {
                        if (sum - (count * i) >= 0 && dp[i - 1][sum - (count * i)] >= 0) {
                            dp[i][sum] = Math.max(dp[i][sum], dp[i - 1][sum - (count * i)] + ((arr[i] - count) / (840 / i)));
                        }
                    }
                }
            }


            long ans = 0;


            for (int sum = 0; sum <= 8 * 840; sum++) {
                if (sum <= w && dp[8][sum] >= 0) {
                    ans = Math.max(ans, (long) sum + (840 * Math.min(dp[8][sum], (w - sum) / 840)));
                }
            }

            out.println(ans);
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

        public long scanLong() {
            long I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

    }
}

