#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int B, P;
  while (cin >> B >> P) {
    vector<int> N(P);
    for (auto& x : N) cin >> x;

    vector<vector<pair<int,int>>> dyn{{{0,0}},{}};
    for (int i = P-1; i >= 0; i--) {
      vector<vector<pair<int,int>>> dyn2(2);
      for (int carry = 0; carry < 2; carry++) {
        sort(dyn[carry].begin(), dyn[carry].end());
        int bmin = 1e9;
        for (auto [acost, bcost] : dyn[carry]) {
          if (bcost >= bmin) continue;
          bmin = bcost;
          dyn2[0].push_back({acost + (N[i]+carry), bcost});
          dyn2[1].push_back({acost, bcost + (B-N[i]-carry)});
        }
      }
      dyn.swap(dyn2);
    }

    int ret = 1e9;
    for (auto const& v : dyn) for (auto [acost, bcost] : v) ret = min(ret, max(acost, bcost));
    cout << ret << endl;
  }
}
