// Arup Guha
// 9/30/2023
// Solution to SER 2022 D1 Problem: Exponent Exchange

using namespace std;
#include <bits/stdc++.h>

const int NONE = 100000000;

int main() {

    // Read in the input, with digits in reverse order.
    /*** I assume these digits are Bob's digits in the naming of my
         variables, not Alice. Understanding this will make my code
         easier to read.
    ***/
    int b, nD;
    cin >> b >> nD;
    int* digits = new int[nD];
    for (int i=nD-1; i>=0; i--)
        cin >> digits[i];

    // Set up our "bfs" search through possible states in the DP.
    int** dp = new int*[2];
    for (int i=0; i<2; i++)
        dp[i] = new int[1];
    dp[0][0] = 0; dp[1][0] = NONE;
    queue<int> q;
    q.push(0);
    int tot = 0;

    // Loop through digits.
    for (int k=1; k<=nD; k++) {

        // This is the sum of the digits so far.
        tot += digits[k-1];

        // dp2[0][j] stores the minimum score for Alice, given that the
        // last move Bob gave to Alice. dp2[1][j], is the same but given
        // that the last move is Alice giving to Bob.
        int** dp2 = new int*[2];
        for (int i=0; i<2; i++) {

            // I never figured out why this array has to be bigger than tot
            // but apparently it does. Padding it by 50 worked... Not sure
            // that is theoretically the max though...
            dp2[i] = new int[tot+50];
            for (int j=0; j<=tot; j++)
                dp2[i][j] = NONE;
        }

        queue<int> newq;

        // Iterate through all possible states.
        while (q.size() > 0) {

            // Get next state from queue and extract each of the three pieces out.
            int curS = q.front(); q.pop();

            // This is how many points Bob already has.
            int bobS = curS & ( (1<<17)-1);

            // This is whether or not the last move was B->A (0) or A->B (1).
            int onoff = curS >> 17;

            // This is my (Bob's) current digit.
            // lol, I just realized I could have done digits[k-1]+onoff.
            int myD = onoff ? digits[k-1]+1 : digits[k-1];

            // New state we reach, so add to queue.
            if (dp2[0][bobS + myD] == NONE)
                newq.push( bobS + myD );

            // If bob gives myD points to Alice, this adds to his state,
            // but not Alice's, getting us to state (0, bobS+myD). Take
            // the minimum answer here.
            dp2[0][bobS+myD] = min(dp2[0][bobS+myD],dp[onoff][bobS]);

            // New state we reach, so add to queue.
            if (dp2[1][bobS] == NONE)
                newq.push( (1<<17) + bobS);

            // Here we have Alice give to Bob, so Bob's score stays
            // the same, but Alice's increases by b-myD.
            dp2[1][bobS] = min(dp2[1][bobS], dp[onoff][bobS]+b-myD);
        }

        // This memory isn't needed.
        free(dp[0]); free(dp[1]); free(dp);

        // Reassign reference. Hope this works in C++.
        q = newq;
        dp = dp2;
    }

    int res = NONE;

    // Go through all states.
    for (int i=0; i<2; i++) {
        for (int k=0; k<=tot; k++) {

            // This is the higher of Bob or Alice's transactions.
            int tmp = max(k, dp[i][k]);

            // Our goal was to minimize this.
            res = min(res, tmp);
        }
    }

    // Ta da!
    cout << res << endl;
    free(digits);
    free(dp[0]); free(dp[1]); free(dp);
    return 0;
}
