#n=int(input())

k,n=list(map(int,input().split()))

al=list(map(int,input().split()))

#l=[list(map(int,input().split())) for i in range(n)]



mn=min(al)

mx=max(al)

dis=[]

for i in range(n-1):

    dis.append(al[i+1]-al[i])

dis.append(mn+(k-mx))



dis.sort(reverse=False)

ans=0

for i in range(n-1):

    ans+=dis[i]

print(ans)

#n=int(input())

k,n=list(map(int,input().split()))

al=list(map(int,input().split()))

#l=[list(map(int,input().split())) for i in range(n)]



difl=[]



for i in range(n-1):

    difl.append(al[i+1]-al[i])

difl.append(al[0]+k-al[n-1])



print((k-max(difl)))
# n=int(input())
k, n = list(map(int, input().split()))
al = list(map(int, input().split()))
# l=[list(map(int,input().split())) for i in range(n)]
mn = min(al)
mx = max(al)
dis = []
for i in range(n - 1):
    dis.append(al[i + 1] - al[i])
dis.append(mn + (k - mx))
dis.sort(reverse=False)
ans = 0
for i in range(n - 1):
    ans += dis[i]
print(ans)

# n=int(input())
k, n = list(map(int, input().split()))
al = list(map(int, input().split()))
# l=[list(map(int,input().split())) for i in range(n)]
difl = []
for i in range(n - 1):
    difl.append(al[i + 1] - al[i])
difl.append(al[0] + k - al[n - 1])
print((k - max(difl)))

-mn = min(al) -mx = max(al) -dis = [] +difl = [] -    dis.append(al[i + 1] - al[i]) -dis.append(mn + (k - mx)) -dis.sort(reverse=False) -ans = 0 -for i in range(n - 1): -    ans += dis[i] -print(ans) +    difl.append(al[i + 1] - al[i]) +difl.append(al[0] + k - al[n - 1]) +print((k - max(difl)))