#include <iostream>
#include <map>
#include <vector>

using namespace std;

void ins(map<int, int>& m, int k, int v) {
  auto it = m.upper_bound(k);
  if(it != m.begin()) {
    it--;
    if(it->second <= v) return;
  }
  m[k] = v;
  while(true) {
    auto it = m.upper_bound(k);
    if(it != m.end() && it->second >= v) m.erase(it);
    else break;
  }
}
int main() {
  int b, p;
  cin >> b >> p;
  vector<int> v(p);
  for(auto& x: v) cin >> x;
  map<int, int> nocarrydp; ins(nocarrydp, 0, 0);
  map<int, int> carrydp;
  while(v.size()) {
    int currhave = v.back(); v.pop_back();
    map<int, int> nnocarrydp, ncarrydp;
    for(auto [k, v]: nocarrydp) {
      ins(nnocarrydp, k + currhave, v);
      ins(ncarrydp, k, v + (b - currhave));
    }
    currhave++;
    for(auto [k, v]: carrydp) {
      if(currhave == b) {
        ins(ncarrydp, k, v);
        continue;
      }
      ins(nnocarrydp, k + currhave, v);
      ins(ncarrydp, k, v + (b - currhave));
    }
    nocarrydp.swap(nnocarrydp);
    carrydp.swap(ncarrydp);
  }
  int ret = 1e9;
  for(auto [k,v]: nocarrydp) ret = min(ret, max(k, v));
  for(auto [k,v]: carrydp) ret = min(ret, max(k, v));
  cout << ret << "\n";
}