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+1)] dp[0] = 0 l = L // 2 for i in range(N): x = X[i] if i == 0: if x <= l: dp[i+1] = x continue for j in range(i): xx = X[j] if j == 0: if x <= l: dp[i+1] = x for k in range(1,j+1): if dp[k] == -1: continue a = dp[k] + (x - xx) if a <= l: dp[i+1] = max(dp[i+1],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 i == 0: if x <= l: dp_[i+1] = x continue for j in range(i): xx = X[j] if j == 0: if x <= l: dp_[i+1] = x for k in range(1,j+1): if dp_[k] == -1: continue a = dp_[k] + (x - xx) if a <= l: dp_[i+1] = max(dp_[i+1],a) ans = max(max(dp),max(dp_)) ans = L - 2*ans print(ans)