l,n=map(int,input().split()) x=[0]+list(map(int,input().split()))+[l] for i in reversed(range(1,n+2)): x[i]-=x[i-1] q=[[0]*(2*l+1),[0]*(2*l+1)] q[0][x[1]]=1 for v in x[2:]: nq=[[0]*(2*l+1),[0]*(2*l+1)] for j in range(-l,l+1): if q[0][j]: nq[0][j+v]=1 nq[1][j-v]=1 if q[1][j]: nq[0][j+v]=1 nq[1][j-v]=1 q=nq g=l for i in range(-l,l+1): if q[1][i]: g=min(g,abs(i)) print(g)