CodeEff/ECCO huggingface dataset:
# Original solution
K,N = list(map(int,input().split()))
li = list(map(int,input().split()))
#反時計回りか時計回りの合計距離を、個数出して比較
for i in range(N):#i番目の家を出発地にした時の最小距離を順に求めていく
  iti_t=i#初期位置
  iti_h=i#初期位置
  lon_t=0
  lon_h=0
  for j in range(N-1):#初期位置から進めていく
    if iti_t !=(N-1):
      lon_t+=li[iti_t+1]-li[iti_t]
      iti_t+=1
    else:#位置がN-1のとき
      lon_t+=li[0]+K-li[N-1]
      iti_t=0
    if iti_h !=0:
      lon_h+=li[iti_h]-li[iti_h-1]
      iti_h-=1
    else:#位置が0のとき
      lon_h+=li[0]+K-li[N-1]
      iti_h=N-1
  if i==0:
    res=min([lon_h,lon_t])
  else:
    res=min(res,min([lon_h,lon_t]))
print(res)

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

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

# tmp1 = A[-1] - A[0]
# tmp2 = A[-1] - (20 - A[0] - A[-1])
# tmp2 = A[-1] - A[1]

# print(min(tmp1, tmp2))
# # print(A[-1]-A[0])

# ans = []
ans = -1
for i in range(N):
    tmp = 0
    I = A[i]
    for j in range(N-1):
        index = i + j + 1
        if index >= N:
            index -= N
        J = A[index]
        if I < J:
            tmp += J - I
        else:
            tmp += K - I + J
        # print(I,J, tmp)
        I = J
    if ans == -1:
        ans = tmp
    elif ans > tmp:
        ans = tmp
    # ans.append(tmp)
print(ans)
# print(ans)
# print(min(ans))

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

# tmp1 = A[-1] - A[0]
# tmp2 = A[-1] - (20 - A[0] - A[-1])
# tmp2 = A[-1] - A[1]

# print(min(tmp1, tmp2))
# # print(A[-1]-A[0])

# ans = []
ans = -1
for i in range(N):
    # 右方向
    tmp = A[-1] - A[i]
    if i != 0:
        tmp += K - A[-1] + A[i-1]
    
    if ans == -1:
        ans = tmp
    elif ans > tmp:
        ans = tmp
print(ans)
# Original solution
def main():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    ans = float('inf')
    for i in range(N):
        if A[i] > A[i-1]:
            tmp = (K + A[i-1]) - A[i]
        else:
            tmp = A[i-1] - A[i]
        ans = min(ans, tmp)

    for i in range(N):
        if i == N - 1:
            tmp = A[i] - A[0]
        else:
            tmp = K - (A[i+1] - A[i])
        ans = min(ans, tmp)

    print(ans)



if __name__ == "__main__":
    main()

# Optimized solution
def main():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = [0] * (2 * N)

    for i in range(2*N):
        if i < N:
            B[i] = A[i]
        else:
            B[i] = K + A[i - N]

    l = 0
    for i in range(N):
        tmp = B[i+1] - B[i]
        l = max(l, tmp)

    ans = K - l
    print(ans)



if __name__ == "__main__":
    main()
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
max_dis = 0
for i in range(N - 1):
    for j in range(i+1, N):
            tmp = A[j] - A[i]
            if tmp > K // 2:
                tmp -= K//2
            max_dis = max(max_dis, tmp)

print((K - max_dis))

# Optimized solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = K - (A[-1] - A[0])
for i in range(N - 1):
    tmp = A[i + 1] - A[i]
    ans = max(tmp,ans)
print((K - ans))
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
max_dist = 0

for i in range(N):
  if i == N - 1:
    dist = A[0] + K - A[i]
  else:
    dist = A[i+1] - A[i]
  
  if dist > max_dist:
    max_dist = dist
    
print((K - max_dist))

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

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

diff = []

for i in range(n - 1):
    diff.append(a[i + 1] - a[i])
diff.append(k - a[-1] + a[0])

print((k - max(diff)))


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

a.append(k + a[0])
diff = [a1 - a0 for a0, a1 in zip(a, a[1:])]

print((k - max(diff)))
# Original solution
def around(K,A):
    N2 = A.pop(0)
    A.append(N2+K)
    return A

K,N = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))

X = 0
X_min = 1000000000
for j in range(0,len(A)):
    X=0
#     print(A)
    X = A[len(A)-1] - A[0]
#     print(X)
#     for i in range(0,len(A)-1): #i番目の家からスタート
#         X += A[i+1] - A[i]
#         if X > X_min:
#             i=0
#             break
#     if i == len(A)-2:
#     print(X)
    if X_min > X:
        X_min = X
    A=around(K,A)
#     print(X_min)
    
print(X_min)

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

X = 0
space = 0
idx = 0
A.append(A[0]+K)
for j in range(0,len(A)-1):
    X=0
    X = A[j+1] - A[j]
    if space < X:
        space = X
        idx = j
    
print((A[idx] + K - A[idx+1] ))
# Original solution
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))
b=[]
for i in range(N):
  if A[i]==0:
    b.append(A[N-1])
  else:
    for j in range(i+1,N):
      b.append(K-(A[j]-A[i]))
    

print((min(b)))
    

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

print((min(b)))
# Original solution
k, n = list(map(int, input().split()))
A = list(map(int, input().split()))

D = [j - i for i, j in zip(A, A[1:])] + [k-A[-1]+A[0]]

md = max(D)
ans = k - md

print(ans)

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

D = [j - i for i, j in zip(A, A[1:])]

ans = k - max(D)

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

# 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(a[0]+k-a[n-1])    
print((sum(d)-max(d)))
# Original solution
import sys
input = sys.stdin.readline
k, n = [int(w) for w in input().split()]
a_li = [int(w) for w in input().split()]

dist = k - a_li[-1] + a_li[0]

for i in range(1, n):
    dist = max(dist, a_li[i] - a_li[i - 1])

print((k - dist))


# Optimized solution
import sys
input = sys.stdin.readline
k, n = [int(w) for w in input().split()]
a_li = [int(w) for w in input().split()]

dist = k - a_li[-1] + a_li[0]

for a1, a2 in zip(a_li[:-1], a_li[1:]):
    dist = max(dist, a2-a1)

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

if A[0] != 0:
    B = A + [K] 
else:
    B = A
#print(B)

for i in range(N):
    B.append(A[i] + K)
#print(B)

ans = 10000000
C =[]
for i in range(N):
    cnt = 0
    for j in range(i, i + N - 1):
        cnt +=B[j+1] - B[j]
    ans = min(ans,cnt)

print(ans)

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

a = K + A[0]
A = A + [a]

#print(A)

B = []
for i in range(N):
    B.append(A[i+1] - A[i])
B = sorted(B,reverse = False)

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

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

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

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

# Optimized solution
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))
l=[]
A.append(A[0]+K)
for i in range(N):
    l.append(A[i+1]-A[i])
print((K-max(l)))
# Original solution
import itertools
K,N = list(map(int,(input().split())))
A = list(map(int,(input().split())))

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

ans.append(K-A[N-1])

fin = []
for i in itertools.combinations(ans,N-1):
    fin.append(sum(i))
print((min(fin)))

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

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

ans.append(K-A[N-1]+A[0])
print((K-(max(ans))))
# Original solution
K, N = list(map(int, input().split()))
lis = list(map(int, input().split()))
dis=[]
for _ in range(N):
  p = lis.pop(0)
  lis.append(p)
  d = 0
  for i in range(N-1):
    if lis[i+1] > lis[i]:
      d += (lis[i+1]-lis[i])
    else:
      d += (K - lis[i]) + lis[i+1]
  dis.append(d)
print((min(dis)))

# Optimized solution
K, N = list(map(int, input().split()))
lis = list(map(int, input().split()))
dis=[]
lis.append(lis[0])
for i in range(N):
  if lis[i+1] > lis[i]:
    dis.append(lis[i+1]-lis[i])
  else:
    dis.append((K - lis[i]) + lis[i+1])
print((sum(dis)-max(dis)))
# Original solution
# -*- coding: utf-8 -*-


def calc(k, kiten, a):
    koho1 = abs(kiten -a)
    koho2 = kiten + k - a
    koho3 = a + k - kiten

    return min(koho1, koho2, koho3)


def main():
    k, n = list(map(int, input().split()))
    a_list = list(map(int, input().split()))

    max_kkn = a_list[0] + k - a_list[-1]
    for i in range(n - 1):
        max_kkn = max(max_kkn, a_list[i + 1] - a_list[i])

    print((k - max_kkn))


if __name__ == '__main__':
    main()


# Optimized solution
# -*- coding: utf-8 -*-


def calc(k, kiten, a):
    koho1 = abs(kiten -a)
    koho2 = kiten + k - a
    koho3 = a + k - kiten

    return min(koho1, koho2, koho3)


def main():
    k, n = list(map(int, input().split()))
    a_list = list(map(int, input().split()))

    max_kkn = a_list[0] + k - a_list[-1]
    for a, b in zip(a_list, a_list[1:]):
        max_kkn = max(max_kkn, b - a)

    print((k - max_kkn))


if __name__ == '__main__':
    main()
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
li = []
for i in range(N):
    if i == 1:
        cadA = A[N-1] - A[0]
    else:
        cadA = K - A[i] + A[i-1]
    if i == N-1:
        cadB = A[N-1] - A[0]
        cadB = 100000000000
    else:
        cadB = A[i] + (K - A[i+1])
        cadB = 100000000000
    li.append(min(cadA, cadB))
print((min(li)))

# Optimized solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
distance = [0] * N
for i in range(N):
    if not i == N-1:
        distance[i] =  A[i+1] - A[i]
    else:
        distance[N-1] = A[0] + (K - A[N-1])
distance.sort()
distance.pop()
print((sum(distance)))
# Original solution
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    K,N = list(map(int, input().split()))
    a = list(map(int, input().split()))

    res = 10**20
    for i in range(1, N-1):
        res = min(K-(a[i+1]-a[i]), K-(a[i]-a[i-1]), res)
    res = min(K-(a[1]-a[0]), res, a[N-1]-a[0])
    res = min(res, K-(a[N-1]-a[N-2]))
    print(res)


if __name__ == '__main__':
    main()

# Optimized solution
#どっちで出すか注意, rstrip注意
#提出前に見返すこと！
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()
# Original solution
def calc_sub(arg, start):
  global n,k
  sub = 0
  for i in range(n):
    if i != 0:
      idx = start + i
      if idx < n:
        sub += arg[idx] - arg[idx-1]
      else:
        idx = idx - n
        if idx == 0:
          sub += arg[0] + (k - arg[n-1])
        else:
          sub += arg[idx] - arg[idx-1]
        
  return sub

def calc_sub_r(arg, start):
  global n,k
  sub = 0
  for i in reversed(list(range(0, n))):
    idx = start + i
    idx = idx - n if idx >= n else idx
    if idx == n-1:
      sub += (k - arg[idx]) + arg[0]
    else:
      sub += arg[idx+1] - arg[idx]
  return sub


k, n = list(map(int, input().split()))
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

#aaa = calc_sub(l_si_i, 2)
#bbb = calc_sub_r(l_si_i, 2)
#print(aaa)
#print(bbb)


min = 10000000
for i in range(n):
  ret = calc_sub(l_si_i,i)
  if min > ret:
    min = ret
    
  ret = calc_sub_r(l_si_i, i)
  if min > ret:
    min = ret
#  l_si_i.append(l_si_i.pop(0))

print(min)


# Optimized solution
k, n = list(map(int, input().split()))
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

num_list = []
for i in range(n):
  if i == 0:
    sub = l_si_i[0] + (k - l_si_i[n-1])
  else:
    sub = l_si_i[i] - l_si_i[i-1]
  
  num_list.append(sub)
  
#print(num_list)
#print(sum(num_list))

idx = num_list.index(max(num_list))
#print(idx)
num_list.pop(idx)
print((sum(num_list)))
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
max_diff = A[0] + (K - A[-1])
for i in range(1, N):
    max_diff = max(max_diff, A[i] - A[i - 1])

print((K - max_diff))

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

# Optimized solution
def main():
    k, n = list(map(int, input().split()))
    a = list(map(int, input().split()))
    cost = 10**10
    for i in range(n-1):
        cost = min(cost, k - abs(a[i]-a[i+1]))
    print((min(cost, k-abs(a[0]+k-a[n-1]))))

if __name__ == "__main__":
    main()
# Original solution
def main():
    k, n = list(map(int, input().split()))
    a = list(map(int, input().split()))
    cost = 10**10
    for i in range(n-1):
        cost = min(cost, k - abs(a[i]-a[i+1]))
    print((min(cost, k-abs(a[0]+k-a[n-1]))))

if __name__ == "__main__":
    main()

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

if __name__ == "__main__":
    main()
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

max = k + a[0] - a[n-1]
for i in range(n-1):
    tmp = a[i+1] - a[i]
    if tmp > max:
        max = tmp

print((k-max))

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

max = k + a[0] - a[n-1]
prev = a[0]
for i in a[1:]:
    tmp = i - prev
    if tmp > max:
        max = tmp

    prev = i

print((k-max))
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = []
ans.append(max(a) - min(a))
for i in range(n):
    if a[i] > k // 2:
        a[i] = (k - a[i]) * -1
    ans.append(max(a) - min(a)) 
print((min(ans)))

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
l = [a[0] + k - a[-1]]
for i in range(1, n):
    l.append(a[i] - a[i - 1])
print((sum(l) - max(l)))
# Original solution
P, N = list(map(int, input().split()))
arr = list(map(int, input().split()))
arr.sort()
mn = arr[-1] - arr[0]
for i in range(N):
    val = arr.pop(0)
    arr.append(val + P)
    mn = min([mn, arr[-1] - arr[0]])
print(mn)


# Optimized solution
P, N = list(map(int, input().split()))
arr = list(map(int, input().split()))
mn = 100000000
l, r = 0, len(arr) - 1
for i in range(N):
    if (arr[r] - arr[l]) < mn:
        mn = arr[r] - arr[l]
    arr[l] += P
    l = (l + 1) % N
    r = (r + 1) % N
print(mn)
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

ans = max(A) - min(A)

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

print(ans)

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

dist = [0] * (N)

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

print((K - max(dist)))
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
 
A += [A[0] + K]
gap = max(y-x for x,y in zip(A, A[1:]))
print((K - gap))

# Optimized solution
import sys
read = sys.stdin.buffer.read
 
K, N, *A = list(map(int, read().split()))
A.append(A[0] + K)
gap = max(y - x for x, y in zip(A, A[1:]))
print((K - gap))
# Original solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline

K, N, *A = list(map(int, read().split()))
A.append(K + A[0])
D = 0
for i, j in zip(A, A[1:]):
    d = j - i
    if d > D:
        D = d

print((K - D))


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline

K, N, *A = list(map(int, read().split()))
A.append(K + A[0])
d = [j - i for i, j in zip(A, A[1:])]

print((K - max(d)))
# Original solution
k, n = list(map(int, input().split()))
 
An = list(map(int, input().split()))
 
mn = 0
Amax = max(An)
Amin = min(An)
for i in range(len(An)):
    cal = 0
    A = An[i:]  + An[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
        
        if mn > 0 and cal > mn:
            break
    
    if mn ==0 or mn > cal:
        mn = cal
        
rAn = An[:]
rAn.reverse()
for i in range(len(rAn)):
    cal = 0
    A = rAn[i:]  + rAn[:i]
    for i in range(len(A)-1):
        if A[i] == Amax and A[i+1] == Amin:
            cal += A[i+1] + k - A[i]
        elif A[i] == Amin and A[i+1] == Amax:
            cal += A[i] + k - A[i+1]
        elif A[i+1] >= A[i]:
            cal += A[i+1] - A[i]
        else:
            cal += A[i] - A[i+1]
            
        if mn > 0 and cal > mn:
            break
 
    if mn ==0 or mn > cal:
        mn = cal
print(mn)

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

prev = a[0]

for i, v in enumerate(a):
    if i == 0:
        ans = min(ans, k-b)
    ans = min(ans, k-abs(v-prev))
    prev = v
    
print(ans)

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

prev = a[0]

for i, v in enumerate(a):
    if i == 0:
        ans = min(ans, k-b)
    ans = min(ans, k-(v-prev))
    prev = v
    
print(ans)
# Original solution
K, N = list(map(int,input().split()))
A = list(map(int,input().split()))
for i in range(len(A)):
    min_this = 0
    for j in range(len(A)-1):
        now_point = A[i+j-len(A)]
        next_point = A[i+j+1-len(A)]
        if next_point - now_point < 0:
            min_this += (K-now_point) + next_point
        else:
            min_this += next_point - now_point
    if i==0:
        total_min = min_this
    else:
        if min_this < total_min:
            total_min = min_this
print(total_min)

# Optimized solution
K, N = list(map(int,input().split()))
A = list(map(int,input().split()))
for i in range(len(A)):
    now_point = A[i]
    last_point = A[i-1]
    if last_point == 0:
        last_point = K
    if last_point - now_point <= 0:
        now_min = (K-now_point) + last_point
    else:
        now_min = last_point - now_point
    if i==0:
        total_min = now_min
    else:
        if now_min < total_min:
            total_min = now_min
print(total_min)
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

A2 = [K+a for a in A]
A3 = A + A2

dist = [0] * N
for i in range(N):
    j = i + N - 1
    dist[i] = A3[j] - A[i]

print((min(dist)))



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

A3 = A + [K+a for a in A]
dist = [A3[i+N-1] - A[i] for i in range(N)]

print((min(dist)))
# Original solution
import sys
input = lambda: sys.stdin.readline().rstrip()

k, n = list(map(int, input().split()))
A = list(map(int, input().split()))
A.append(A[0]+k)

l = 0
for i in range(n):
    l = max(l, A[i+1]-A[i])
ans = k - l
print(ans)

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

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

x = c[n-1]-c[0]
if x < answer:
  answer = x

y = c[0]+k-c[1]
if y < answer:
  answer = y

z = k-c[n-1]+c[n-2]
if z < answer:
  answer = z

w = c[n-1] - c[0]
if w < answer:
  answer = w

for i in range(1,n-1):
  a = c[i-1]+k-c[i]
  b = c[i]+k-c[i+1]
  if a < answer:
    answer = a
  elif b < answer:
    answer = b

print(answer)

# Optimized solution
k,n = list(map(int,input().split()))
lst = list(map(int,input().split()))
dst = []

for i in range(n-1):
  dst.append(lst[i+1]-lst[i])
dst.append(lst[0] + k-lst[n-1])

dst_rev = sorted(dst)

ans = 0

for i in range(n-1):
  ans += dst_rev[i]

print (ans)
# Original solution
# ABC160_C
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
# mid=(a[0]+a[-1])/2
d=[]
for i in range(n-1):
    d.append(a[i+1]-a[i])
# d.append(k-a[-1])
# d=[k-a[-1]]+d

# print(d)
ans=10**10
d=[k-a[-1]+a[0]]+d
d+=[k-a[-1]+a[0]]
# print(d)
for i in range(1,n):
    t=min(k-d[i-1],k-d[i+1])
    ans=min(t,ans)
print(ans)


# Optimized solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
d=[]
d.append(k-a[-1]+a[0])
for i in range(1,n):
  d.append(a[i]-a[i-1])
ans=10**18
for di in d:
  t=k-di
  ans=min(ans,t)
print(ans)
# Original solution
I=lambda:list(map(int,input().split()))
p,n=I()
l=I()
mi=10**9+7
for i in range(n):
    te=0
    j=i
    while (j+1)%n!=i%n:
        if j==n-1:
            te+=abs(l[-1]-sum(l))
        else:
            te+=abs(l[j%n]-l[(j+1)%n])
        j+=1
    mi=min(te,mi)
l=l[::-1]
for i in range(n):
    te=0
    j=i
    while (j+1)%n!=i%n:
        if j==n-1:
            te+=abs(l[-1]-sum(l))
        else:
            te+=abs(l[j%n]-l[(j+1)%n])
        j+=1
    mi=min(te,mi)


print(mi)
        


# Optimized solution
I=lambda:list(map(int,input().split()))
p,n=I()
l=I()
ne=[]
ma=10**9+7
for i in range(n-1):
    ne.append(abs(l[i]-l[i+1]))
if l[-1]==p:
    ne.append(l[0]-l[-1])
else:    
    ne.append(l[0]+abs(p-l[-1]))
g=sum(ne)
for i  in ne:
    ma=min(ma,g-i)
print(ma)
# Original solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
d=0
for i in range(n-1):
    d=max(sorted(a)[i+1]-sorted(a)[i],d)
d=max(k+sorted(a)[0]-sorted(a)[n-1],d)
print((k-d))

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