CodeEff/ECCO huggingface dataset:
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int,input().split()))
for i in range(N):
  A.append(A[i]+K)
d = 0; ans = float("INF")
for i in range(N):
  d = 0
  for j in range(N-1):
    d += A[i+j+1] - A[i+j]
  ans = min(ans, d)
print(ans)

# Optimized solution
K, N = list(map(int, input().split()))
A = list(map(int,input().split()))
for i in range(N):
  A.append(A[i]+K)
d = [0]*(N)
max_d, max_i = 0, -1
for i in range(N):
  d[i] = A[i+1] - A[i]
  if d[i] > max_d:
    max_d = d[i]
    max_i = i + 1
ans = 0
for i in range(N-1):
  ans += A[i+max_i+1] - A[i+max_i]
print(ans)
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int,input().split()))
for i in range(N):
  A.append(A[i]+K)
d = [0]*(N)
max_d, max_i = 0, -1
for i in range(N):
  d[i] = A[i+1] - A[i]
  if d[i] > max_d:
    max_d = d[i]
    max_i = i + 1
ans = 0
for i in range(N-1):
  ans += A[i+max_i+1] - A[i+max_i]
print(ans)

# Optimized solution
K, N = list(map(int, input().split()))
A = list(map(int,input().split()))
A.append(A[0] + K)
d = 0
for i in range(N):
	d = max(d, A[i+1] - A[i])
print((K - d))
# Original solution
k , n = (int(x) for x in input().split())
a = [int(i) for i in input().split()]

b = []
  
for i in range(n-1):
    b.append(a[i+1] - a[i])
    
b.append(a[0] + k -a[n-1])
 
lans=0
ans = 999999999999
 
for i in range(len(b)):
    lans = 0
    for j in range(len(b)):
        if i != j:
            lans = b[j] + lans
    ans = min(ans,lans)
    
print(ans)

# Optimized solution
k , n = (int(x) for x in input().split())
a = [int(i) for i in input().split()]
b = []
 
for i in range(n-1):
    b.append(a[i+1] - a[i])
    
b.append(a[0] + k -a[n-1])

ans=0 
b.sort()
 
for i in range(len(b)-1):
    ans = b[i] + ans

print(ans)
# Original solution
(k,n),(*a,) = [list(map(int,i.split())) for i in open(0)]
a.append(a[0]+k)
print((k - max(a[i+1]-a[i] for i in range(n))))

# Optimized solution
k,n,*a = list(map(int, open(0).read().split()))
a.append(a[0]+k)
print((k - max(a[i+1]-a[i] for i in range(n))))
# Original solution
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = 0
a.reverse()
for i in range(n-1):
    a[i]-=a[i+1]
a[n-1] = 0
a.append(k-sum(a))
ans = k-max(a)
print(ans)

# Optimized solution
#!/usr/bin/env python3
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = 0
a.append(k+a[0])
a.reverse()
for i in range(n):
    a[i]-=a[i+1]
ans = k-max(a)
print(ans)
# Original solution
# 初期入力
K,N = (int(x) for x in input().split(" "))
A = [0]
for i in input().split():
    if i !="0":
        A.append( int(i) )

# 1軒ずつ距離のリスト
x =[A[i+1] - A[i] for i,v in enumerate(A[:-1]) ]
if N == len(A):
    x.append(K -A[-1])
else:
    x.append(K - A[-1] +A[1])

#　求めるのは　K　－　max(隣と離れた家の距離)
kyori_min = K - max(x)
print(kyori_min)

# Optimized solution
# 初期入力
K,N = (int(x) for x in input().split(" "))
A = list(map(int, input().split()))
A.append(0) # 基準地点を最後に追加、スタートが０でもうまくいく

# 1件となりまでの距離
kyori = [ A[i+1] -A[i] for i,v in enumerate(A[:-1]) ]
kyori[-1] = kyori[-1] + K # n番目から基準までは全体から「引く」
kyori.append(A[0] -A[-1] ) # 基準から1番目までの距離
x_saisyo_saigo = kyori[-2] +kyori[-1]
kyori.append(x_saisyo_saigo) # n番目から1番目の距離

# 最短距離は、全体からmax(隣の家との距離)を引く
kyori_min = K - max(kyori)
print(kyori_min)
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
x = a[0] + k-a[-1]
ans = 0
for i in range(n-1):
	ans = max(ans,a[i+1]-a[i])
ans = max(ans,x)
print((k-ans))

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
b = 0
for i in range(n-1):
  b = max(b,a[i+1]-a[i])
b = max(b,a[0]+k-a[-1])
print((k-b))
# Original solution
k,n,*l=list(map(int,open(0).read().split()));print((min((l[i-1]-l[i])%k for i in range(n))))

# Optimized solution
k,n,*l=list(map(int,open(0).read().split()))
print((k-max((l[i]-l[i-1])%k for i in range(n))))
# Original solution
f=lambda:[*list(map(int,input().split()))]
k,n=f()
l=f()
l+=[l[0]+k]
a=0
for i in range(n):
  a=max(a,l[i+1]-l[i])
print((k-a))

# Optimized solution
k,n,*l=list(map(int,open(0).read().split()))
l+=[l[0]+k]
print((k-max(l[i+1]-l[i] for i in range(n))))
# Original solution
import itertools

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

ans = float('inf')
roots = list(itertools.permutations(a))

for i in range(n):
    sum_d = 0
    root = list(roots[i])
    for j in range(1, n):
        sum_d += min(abs(root[j] - root[j - 1]), abs(k - root[j] + root[j - 1]))
    if sum_d < ans:
        ans = sum_d
print(ans)


# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
d_list = [] * n
for i in range(1, n):
    d_list.append(a[i] - a[i - 1])
d_list.append(k - a[n - 1] + a[0])
ans = sum(d_list) - max(d_list)
print(ans)
# Original solution
import sys
k, n = list(map(int, sys.stdin.readline().split()))
an = list(map(int, sys.stdin.readline().split()))

dai = an[0]+k-an[-1]

for i in range(1, n):
    dai = max(dai, an[i]-an[i-1])

print((k-dai))

# Optimized solution
import sys
k, n = list(map(int, sys.stdin.readline().split()))
an = list(map(int, sys.stdin.readline().split()))

dai = an[0]+k-an[-1]

for i in range(1, n):
    if an[i]-an[i-1]>dai:
        dai = an[i]-an[i-1]

print((k-dai))
# Original solution
k,n = list(map(int, input().split()))
a = list(map(int,input().split()))

k2 = k // 2
bet = [210000] * (n - 1) + [0] * 1
for i in range(n):
    for j in range(n):
        if i != j:
            if (0 <= a[i] < 10) and (10 < a[j] < 20):
                bet[i] = min(bet[i], abs(a[i]) - (a[j] - k))
            elif (0 <= a[j] < 10) and (10 < a[i] < 20):
                bet[i] = min(bet[i], abs(a[j]) - (a[i] - k))
            else:
                bet[i] = min(bet[i], abs(a[i] - a[j]))
#print(bet)
print((sum(bet)))


# Optimized solution
k,n = list(map(int, input().split()))
a = list(map(int,input().split()))
max_dist = 0
for i in range(n):
    nxt = (i + 1) %n
    distance = a[nxt] - a[i]
    if distance < 0:
        distance += k
    max_dist = max(max_dist, distance)
print((k - max_dist))
# Original solution
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: list(map(int, INPUT().split()))
S_MAP = lambda: list(map(str, INPUT().split()))
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: list(zip(*(MAP() for _ in range(n))))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    K, N = MAP()
    A = LIST()

    lis = []
    for i in range(N-1):
        lis.append(A[i+1] - A[i])
    lis.append(K + A[0] - A[N-1])

    ans = INF
    for i in range(N-1):
        ans = min(ans, sum(lis[i:i+N-1]))
        
    ans = min(ans, sum(lis[N-1:]) + lis[0])
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: list(map(int, INPUT().split()))
S_MAP = lambda: list(map(str, INPUT().split()))
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: list(zip(*(MAP() for _ in range(n))))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    K, N = MAP()
    A = LIST()

    ans = INF
    for i in range(N):
        if i == 0:
            ans = A[i+N-1] - A[i] if A[i+N-1] - A[i] < ans else ans
        else:
            ans = K + A[(i+N-1)%N] - A[i] if K + A[(i+N-1)%N] - A[i] < ans else ans
    
    print(ans)
    

if __name__ == '__main__':
    main()
# Original solution
# coding: utf-8
from itertools import permutations


def solve(*args: str) -> str:
    k, n = list(map(int, args[0].split()))
    A = list(map(int, args[1].split()))

    ret = k+1
    for P in permutations(A):
        total = 0
        cur = P[0]
        for a in P[1:]:
            total += min(abs(k-a)+cur, abs(a-cur))
            cur = a
        ret = min(ret, total)

    return str(ret)


if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))


# Optimized solution
# coding: utf-8


def solve(*args: str) -> str:
    k, n = list(map(int, args[0].split()))
    A = list(map(int, args[1].split()))

    D = [k-A[-1]+A[0]] + [A[i+1]-A[i] for i in range(n-1)]

    return str(k-max(D))


if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

a.append(a[0] + k)
m = 0
s = 0
for i in range(n):
    m = max(m, a[i+1] - a[i])
    s += a[i+1] - a[i]
print((s - m))

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

a.append(a[0] + k)
m = 0
for i in range(n):
    m = max(m, a[i+1] - a[i])
print((k - m))
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

a.append(a[0] + k)
m = 0
for i in range(n):
    m = max(m, a[i+1] - a[i])
print((k - m))

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(a[0] + k)
m = []
for i in range(n):
    m.append(a[i+1] - a[i])
print((k - max(m)))
# Original solution
#coding:utf-8
import itertools

k, n = list(map(int, input().split()))
a = tuple(map(int, input().split()))
ans = 100000000000000000000

dist = []
pattern = sorted(list(itertools.permutations(a)))
#print(pattern)
#print(len(pattern))

for i in range(len(pattern)):
    tmp = 0
    for j in range(0,n-1):
        right = abs(pattern[i][j+1] - pattern[i][j])
        left = abs(k - pattern[i][j+1])
        #print(right, left)
        tmp += min(right, left)
    if tmp < ans:
        ans = tmp

#print(dist)
print(ans)

# Optimized solution
#coding:utf-8

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
dist = []

#逆周りの方が距離が短く済むので、距離を2倍にして考える
#池の一周の長さ+a[0]を座標として追加
a.append(a[0] + k)

#距離が最大になる経路は通らないので除外する
for i in range(n):
    dist.append(a[i+1] - a[i])

#print(a, dist)
#池の一周の長さから、距離が最大になるところを除外したものが最短
print((k - max(dist)))
# Original solution
from collections import deque
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = deque()
for i in range(n):
    for j in range(1,n):
        if a[j]-a[i] >= abs(a[j]-a[i]-k):
            ans.append(abs(a[j]-a[i]-k))
        else:
            ans.append(a[j]-a[i])
print((max(ans)))

# Optimized solution
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = a[0]+(k-a[-1])
for i in range(n-1):
    ans = max(ans,a[i+1]-a[i])
print((k-ans))
# Original solution
k,n = list(map(int, input().split()))
a = list(map(int, input().split()))

x = [0]*n
for i in range(n):
  t = a[i] - a[i-1]
  if i == 0:
    t += k
    x[i] = t
  x[i] = t

print((sum(i for i in sorted(x)[:-1])))

# Optimized solution
k,n = list(map(int, input().split()))
a = list(map(int, input().split()))

l = [0]*n
l[-1] = k+a[0]-a[-1]
for i in range(n-1):
  l[i] = a[i+1] - a[i]
print((sum(sorted(l)[:-1])))
# Original solution
k, n = list(map(int,input().split()))
a = list(map(int,input().split()))

cost = 10**9
y = min(a)
for i in range(n):
    cost_r = 0
    cost_l = 0
    r = a[i:] + a[:i]
    l = list(reversed(a[:i+1])) + list(reversed(a[i+1:]))
    for j in range(n-1):
        if r[j+1] < r[j]:
            cost_r += k - r[j]
            if l[j+1] < l[j]:
                cost_l += k - l[j]
                continue
            cost_l += l[j+1] - l[j]
            continue
        if l[j+1] < l[j]:
            cost_l += k - l[j]
            cost_r += r[j+1] - r[j]
            continue
        cost_r += r[j+1] - r[j]
        cost_l += l[j+1] - l[j]
    #print(cost_r,cost_l)
    #print(r,l)
    cost = min(cost,cost_l,cost_r)
print(cost)

# Optimized solution
k, n = list(map(int,input().split()))
a = list(map(int,input().split()))

cost = max(a[0]+k-a[-1],a[-1]-a[-2])
for i in range(1,n-1):
    cost = max(a[i]-a[i-1],a[i+1]-a[i],cost)
print((k-cost))
# Original solution
#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)


# Optimized solution
#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)))
# Original solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))

d=[]
for i in range(len(a)-1):
    d.append(abs(a[i]-a[i+1]))

d.append(k-a[-1]+a[0])
d+=d[:n-2]
ans=[]
for i in range(n):
    total=sum(d[i:i+n-1])
    ans.append(total)

print((min(ans)))  

# Optimized solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))

d=[]
for i in range(n-1):
    d.append(a[i+1]-a[i])

d.append(k-a[-1]+a[0])
d=sorted(d,reverse=True)
print((sum(d[1:])))
# Original solution
K, N = list(map(int,input().split()))
A = list(map(int,input().split()))

max = K - A[N-1] + A[0]

A.sort()

for i in range(N-1):
  if(max < A[i+1] - A[i]):
    max = A[i+1] - A[i]

  
print((K - max))

# Optimized solution
K, N = list(map(int,input().split()))
A = list(map(int,input().split()))

max = K - A[N-1] + A[0]

for i in range(N-1):
  if(max < A[i+1] - A[i]):
    max = A[i+1] - A[i]

  
print((K - max))
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

m  = k
for i in range(n):
    b = a[i::] + a[0:i]
    r = 0
    for j in range(n-1):
        #print(r, b[j+1], b[j])
        if b[j] > b[j+1]:
            r = r + b[j+1] + (k - b[j])
        else:
            r = r + b[j+1] - b[j]
    m = min(m, r)
    #print(m,"kkkkk")
    
d = a[::-1]
for i in range(n):
    b = d[i::] + d[0:i]
    r = 0
    for j in range(n-1):
        #print(r, b[j+1], b[j])
        if b[j] > b[j+1]:
            r = r + b[j+1] + (k - b[j])
        else:
            r = r + b[j+1] - b[j]
    m = min(m, r)
print(m)

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

maxdist = 0
for i in range(n-1):
    maxdist = max(maxdist, a[i+1] - a[i])
maxdist = max(maxdist, (k - a[-1]) + a[0])
    
print((k - maxdist))
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

for i in range(1, n+1):
    if i == 1:
        distance = k - (a[0] + (k - a[-1]))
        mindistance = distance
        a.append(a[0])
        a.pop(0)
    else:
        distance = k - (a[0] - a[-1])
        if distance < mindistance:
            mindistance = distance
        a.append(a[0])
        a.pop(0)

print(mindistance)

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

a.append(a[0] + k)

distance = []

for i in range(len(a) - 1):
    distance.append(a[i+1] - a[i])

mindistance = 0
for j in distance:
    mindistance += j

print((mindistance - max(distance)))
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

li = []
for i in range(n - 1):
    li.append(a[i + 1] - a[i])

if a[0] == 0:
    li.append(k - a[n - 1])
    li.sort()
    li.pop(-1)
else:
    li.append(k - a[n - 1] + a[0])
    li.sort()
    li.pop(-1)
print((sum(li)))

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

li = []
for i in range(n - 1):
    li.append(a[i + 1] - a[i])
li.append(k + a[0] - a[n - 1])
print((k - max(li)))
# Original solution
import itertools
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.append(k)
a.reverse()
l = []
for i in range(n):
  l.append(a[i]-a[i+1])
k = []
for i in itertools.permutations(l, r=n-1):
  k.append(sum(i))
print((min(k)))

# Optimized solution
K,N = list(map(int,input().split()))	
A = list(map(int,input().split()))
D=[A[0]-A[-1]+K]
for i in range(N-1):
    D.append(A[i+1]-A[i])
print((K-max(D)))
# Original solution
p,q= input().split()
a,b =(int(p), int(q))
f = list(map(int,input().split()))
f_list = list(f)
out_dis =f_list[0] + a -f_list[b-1]
dis_list=[out_dis]
for i in range(b-1):
    dis = f_list[i+1]-f_list[i]
    dis_list.append(dis)

print((int((sum(dis_list)-max(dis_list)))))

# Optimized solution
k , n= list(map(int, input().split()))
a_list = list(map(int, input().split()))
diff_list = []
for i in range(n-1):
    diff_list.append(a_list[i+1]- a_list[i])
diff_list.append(k-a_list[n-1] + a_list[0])
print((sum(diff_list)-max(diff_list)))
# Original solution
K,N=[int(x) for x in input().rstrip().split()]
A=[int(x) for x in input().rstrip().split()]

flag=1
now=0
mae=[]
back=[]
for i in range(N-1):
  mae.append(A[i+1]-A[i])
mae.append(A[len(A)-1]-A[0])

for i in range(N-1,0,-1):
  back.append(A[i]-A[i-1])
back.append(abs(A[0])+abs(K-A[N-1]))

back.sort()
mae.sort()
ans=min(sum(mae[:len(mae)-1]),sum(back[:len(back)-1]))
print(ans)

# Optimized solution
K,N=[int(x) for x in input().rstrip().split()]
A=[int(x) for x in input().rstrip().split()]

A14=K-A[N-1]+A[0]
sa=[A14]

for i in range(N-1):
  sa.append(A[i+1]-A[i])
sa.sort()
ans=0
for i in range(N-1):
  ans+=sa[i]
print(ans)
# Original solution
k,n = list(map(int,input().split()))
a=list(map(int,input().split()))
a.append(a[0]+k)
longd=0
for i in range(n):
    tmp=abs(a[i+1]-a[i])
    longd=max(longd,tmp)
print((k-longd))

# Optimized solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
a.append(a[0]+k)

x=0
for i in range(len(a)-1):
    dis=abs(a[i+1]-a[i])
    x=max(x,dis)

print((k-x))
# Original solution
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))

A.append(K+A[0])

maxdist,maxindx = 0,0
for i,a in enumerate(A):
    dist = -a
    if i+1 == len(A):
        dist += K
    else:
        dist += A[i+1]
    if dist > maxdist:
        maxdist = dist
        maxindx = i
ans = 0
num=0
for x,a in enumerate(A):
    if num==len(A)-2:
        break
    if x == maxindx:
        continue
    num+=1
    if x+1 == len(A):
        ans += K-a
    else:
        ans += A[x+1]-a
print(ans)

# Optimized solution
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))

A.append(K+A[0])

ans=0
maxdist=0
for i,a in enumerate(A):
    if i == len(A)-1:
        break
    dist = A[i+1]-a   
    ans += dist
    maxdist=max(maxdist,dist)
print((ans-maxdist))
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

distance = []
for i in range(N-1):
    distance.append(A[i+1] - A[i])
distance.append(K - A[-1] + A[0])

for i in range(N):
    if distance[i] == max(distance):
        start = i

ans = K - max(distance)
print(ans)

# Optimized solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

distance = []
for i in range(N-1):
    distance.append(A[i+1] - A[i])
distance.append(K - A[-1] + A[0])

ans = K - max(distance)
print(ans)
# Original solution
k,n = list(map(int,input().split()))
L = list(map(int,input().split()))
li = []
for i in range(n-1):
    li.append(L[i+1]-L[i])
li.append(k-L[n-1]+L[0])
#print(L,li)

min1 =10**10
#min1 = k-L[n-1]+L[0]
for i in range(n):
   min1 = min(min1,(k-li[i]))
   #print(min1)
print(min1)

# Optimized solution
k,n = list(map(int,input().split()))
L = list(map(int,input().split()))

li = []
for i in range(n-1):
    li.append(L[i+1]-L[i])
li.append(k-L[n-1]+L[0])
li.sort()
print((k-li[n-1]))
# Original solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
for j in range(n-1):
	a.append(a[j]+k)
sum=[0]*n
for I in range(n):
	for i in range(n-1):
		sum[I]=sum[I]+a[I+i+1]-a[I+i]
print((min(sum)))

# Optimized solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
a.append(a[0]+k)
dist=[0]*n
for i in range(1,len(a)):
	dist[i-1]=a[i]-a[i-1]
print((sum(dist)-max(dist)))
# Original solution
import sys

# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))

    ans = INF

    temp1 = a[-1] - a[0]
    temp2 = k - (a[1] - a[0])
    ans = min(ans, temp1, temp2)

    for i in range(1, n - 1):
        temp1 = k - (a[i + 1] - a[i])
        temp2 = k - (a[i] - a[i - 1])
        ans = min(ans, temp1, temp2)

    temp1 = k - (a[-1] - a[-2])
    temp2 = a[-1] - a[0]
    ans = min(ans, temp1, temp2)

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
import sys

# import bisect
# from collections import Counter, deque, defaultdict

# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

# import numpy as np

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))

    d = [0] * (n - 1)
    d[0] = (k - a[-1]) + a[0]

    for i in range(1, n - 1):
        d[i] = a[i + 1] - a[i]

    print((k - max(d)))


if __name__ == '__main__':
    main()
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
A += [i+K for i in A]
diff = [A[i+1] - A[i] for i in range(N*2-1)]

length = sum(A)

for i in range(len(diff) - N + 1):
    length = min(length, sum(diff[i:i+N-1]))
print(length)

# Optimized solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
diff = [A[i+1] - A[i] for i in range(N-1)]
if 0 in A:
    diff.append(K- A[-1])
else:
    diff.append(A[0] + K - A[-1])


print((sum(diff) - max(diff)))
# Original solution
k, n = list(map(int, input().split()))
house = list(map(int, input().split()))
load = [0]*k

for i in house:
    load[i] = 1


def check(lst):
    return k - lst.index(1) -1


best_way = k
for i in house:
    right = load[i+1:]
    left = load[:i]
    a_way = right + left
    way = min(check(a_way), check(a_way[::-1]))
    if way < best_way:
        best_way = way

print(best_way)


# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
 
max_dist = 0
for i in range(n):
    nxt = (i+1) % n
    distance = a[nxt] - a[i]
    if distance < 0:
        distance += k
    max_dist = max(max_dist, distance)
 
print((k-max_dist))
# Original solution


K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(K)
B = []
for i in range(N):
    B.append(abs(A[i]-A[i+1]))
#print(B)
ans = K
for i in range(N):
    ans = min(ans,sum(B)-B[i])
print(ans)


# Optimized solution


K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
B = []
for i in range(N-1):
    B.append(abs(A[i+1]-A[i]))
B.append(K+A[0]-A[-1])
#print(B)
print((K-max(B)))