L,N=map(int, input().split()) X=list(map(int, input().split())) A=[] for i in range(N-1): A.append(X[i+1]-X[i]) dp=[0]*(2*10**5+10) dp[0]=1 for a in A: ndp=[0]*(2*10**5+10) mod=2*10**5+10 for i in range(2*10**5+10): if dp[i]==1: #print(a,i+a,i-a) ndp[(i+a)%mod]=1 ndp[i-a]=1 dp=ndp ans=10**10 for i in range(-10**5-4,10**5+4): if dp[i]==1: d=i+X[0]-(L-X[-1]) ans=min(ans,abs(d)) print(ans)