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.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 840;
        long[][] dp = new long[9][lcm * 8];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < lcm * 8; j++) {
                if (dp[i - 1][j] != -1) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + min((W - (dp[i - 1][j] * lcm + j)) / lcm, cnt[i] / (lcm / i)));
                    for (int k = 1; (k * i) % lcm != 0 && k <= cnt[i]; k++) {
                        if (dp[i - 1][j] * lcm + j + k * i <= W) {
                            dp[i][(j + k * i) % (lcm * 8)] = max(dp[i][(j + k * i) % (lcm * 8)], dp[i - 1][j] + (j + k * i) / (lcm * 8) + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) / (lcm / i)));
                        }
                    }
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < lcm * 8; i++) {
            if (dp[8][i] != -1) {
                ans = max(ans, dp[8][i] * lcm + i);
            }
        }
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}