import sys input = lambda: sys.stdin.readline().strip() def Map(): return list(map(int,input().split())) L,N = Map() X = Map() dp = [-1 for i in range(N)] l = L // 2 for i in range(N): x = X[i] if x <= l: dp[i] = x continue for j in range(i): xx = X[j] for k in range(j): if dp[k] == -1: continue a = dp[k] + (x - xx) if a <= l: dp[i] = max(dp[i],a) Y = [] while len(X): x = X.pop() Y.append(L-x) X = Y dp_ = [-1 for i in range(N+1)] dp_[0] = 0 l = L // 2 for i in range(N): x = X[i] if x <= l: dp_[i+1] = x continue for j in range(i): xx = X[j] for k in range(j): if dp_[k] == -1: continue a = dp_[k] + (x - xx) if a <= l: dp_[i] = max(dp_[i],a) ans = max(max(dp),max(dp_)) ans = L - 2*ans print(ans)