Problem:
 Score : 300 points    Problem Statement There is a circular pond with a perimeter of K meters, and N houses around them.  The i -th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.  When traveling between these houses, you can only go around the pond.  Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.      Constraints  2 \leq K \leq 10^6  2 \leq N \leq 2 \times 10^5  0 \leq A_1 < ... < A_N < K  All values in input are integers.         Input Input is given from Standard Input in the following format:  K  N  A_1  A_2  ...  A_N       Output Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.        Sample Input 1 20 3
5 10 15      Sample Output 1 10  If you start at the 1 -st house and go to the 2 -nd and 3 -rd houses in this order, the total distance traveled will be 10 .       Sample Input 2 20 3
0 5 15      Sample Output 2 10  If you start at the 2 -nd house and go to the 1 -st and 3 -rd houses in this order, the total distance traveled will be 10 .

Solution (Python):

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

ans = k

for i in range(len(a)):
    if i == 0:
        r = a[-1] - a[i]
        l = a[i] + (k - a[i + 1])
    elif i == n - 1:
        r = (k - a[i]) + a[i - 1]
        l = a[i] - a[0]
    else:
        r = (a[n-1] - a[i]) + ((k - a[n-1]) + a[i - 1])
        l = (a[i] - a[0]) + ((k - a[i + 1]) + a[i - 1])
    
    ans = min(ans, r, l)

print(ans)

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

kyori=[0]*len(a)

for i in range(1,len(a)):
    kyori[i]=a[i]-a[i-1]

kyori[0]=a[0]+k-a[-1]

print(sum(kyori)-max(kyori))

Solution (Python):
k,n = map(int,input().split())
A = list(map(int,input().split()))
A = [0] +A+[k+A[0]]
diff = []
for i in range(1,len(A)):
    diff.append(A[i]-A[i-1])
print(k-max(diff))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
d = A[0] + K - A[-1]
for i in range(N - 1):
    d = max(d, A[i + 1] - A[i])

print(K - d)

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

Solution (Python):

from os import readlink


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

ans = K - (X[0] + K - X[-1])
for i in range(1, N):
    ans = min(ans, K - (X[i] - X[i - 1]))
print(ans)

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

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
t = 10 ** 6

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

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

print(t)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
dA = [0] * N
for i in range(N-1):
    dA[i] = A[i+1] - A[i]
dA[N-1] = A[0] + K - A[N-1]
print(K - max(dA))

Solution (Python):
k,n = list(map(int,input().split()))
A = list(map(int,input().split()))

max_distance = 0

for i in range(n):
  if max_distance < abs(A[i] - A[i-1]):
    max_distance = min(abs(A[i] - A[i-1]),k - abs(A[i] - A[i-1]))

print(k - max_distance)

Solution (Python):

import sys
import collections as cl


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    K, N = MI()
    A = LI()

    dis = [0] * len(A)
    dis[0] = A[0] + (K - A[-1])
    for i in range(1, N):
        dis[i] = A[i] - A[i - 1]

    max_dis = max(dis)

    print(K-max_dis)


main()

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.append(k+a[0])

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

Solution (Python):
k,n,*a=map(int,open(0).read().split())
dist=10**7
for i in range(n):
    if i==0:

        dist=min(dist,a[-1]-a[0])
    else:

        dist=min(dist,a[-1+i]+k-a[i])
print(dist)

Solution (Python):
import numpy as np
k,n = map(int,input().split())
a = np.array([int(i) for i in input().split()])
'''
隣同士の家の距離が最大のものを求めて、Kメートルから引けばよい
'''
d = np.diff(a)
d = np.append(d, k - a[-1] + a[0])
ans = k - d.max()
print(ans)

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
dis = k - a[-1] + a[0]

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

print(k - dis)

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

max_d = 0

for i in range(n):
    d = a[(i + 1) % n] - a[i] 
    if d < 0: 
        d += k
    max_d = max(max_d, d)

print(k - max_d)

Solution (Python):
K,N=map(int,input().split())
A = list(map(int, input().split()))
han=0
for a in range(len(A)):
    if abs(A[a]-A[a-1])>K/2:
        han=abs(A[a]+K-A[a-1])

    elif abs(A[a]-A[a-1])>han:
        han=abs(A[a]-A[a-1])
print(K-han)

Solution (Python):
k,n = map(int, input().split())
a = list(map(int, input().split()))
a.append(k+a[0])

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

print(k-max_l)

Solution (Python):

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

dist = []
for i in range(N-1):
    dist.append(abs(A[i] - A[i+1]))
dist.append(abs(K - A[-1] + A[0]))
dist = sorted(dist, reverse=True)
print(sum(dist[1:]))

Solution (Python):
K, N = map(int, input().split())

A = list(map(int, input().split()))
A. sort()

print(min(K - (A[i] - A[i-1])%K for i in range(N)))

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

Solution (Python):
K,N = map(int,input().split())
A = list(map(int,input().split()))
ls = []
for i in range(N):
    
    if i != N-1:
        ls.append(A[i+1]-A[i])
    else:
        ls.append(A[0]+K-A[i])

    
print(sum(ls)-max(ls))

Solution (Python):

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


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

dist.sort()

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

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
diff = [A[i + 1] - A[i] for i in range(N - 1)]
diff.append(K - sum(diff))
ans = sum(diff) - max(diff)
print(ans)

Solution (Python):
import sys
import numpy as np


def main():
    input = sys.stdin.buffer.readline
    k, n = map(int, input().split())
    a = np.array(list(map(int, input().split())))
    a_dash = a[list(range(1, n)) + [0]]
    a[-1] -= k
    dist = np.abs(a - a_dash)
    max_dist = np.max(dist)
    print(k - max_dist)


if __name__ == "__main__":
    main()

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
d = A[0] + K - A[-1]
for i in range(N-1):
    d = max(d, A[i+1] - A[i])

print(K - d)

Solution (Python):
K , N = map(int,input().split())
A = list(map(int,input().split()))
kyori = [0]
A.append(K+A[0])
for i in range(N):
    kyori.append(A[i+1] - A[i])
kyori_max = max(kyori)
print(K-kyori_max)

Solution (Python):

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

Solution (Python):
k,n = map(int,input().split(" "))
a = list(map(int,input().split(" ")))
m = a[0] + k - a[n-1]
for i in range(n-1):
  l = a[i+1] - a[i]
  if l > m:
    m = l
ans = k - m
print(ans)

Solution (Python):
K, N = map(int, input().split())
aaa = list(map(int, input().split()))
dif = list(aaa[i + 1] - aaa[i] for i in range(N - 1))
dif.append(K - aaa[-1] + aaa[0])
print(K - max(dif))

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

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
dist = [0] * n
a.append(a[0])
for i in range(n) :
    if i == n-1 :
        dist[i] = a[i] - a[i+1]
    else :
        dist[i]= k - (a[i+1] - a[i])
print(min(dist))

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    K, N, *A = map(int, read().split())

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

    ans = K - max(B)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    K, N, *A = map(int, read().split())

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

    ans = K - max(B)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    k, n = (int(i) for i in input().split())
    st = [int(i) for i in input().split()]

    st = sorted(st)
    st.append(st[0] + k)

    diffs = [st[i+1] - st[i] for i in range(n)]

    print(k - max(diffs))

main()

Solution (Python):
k,n=map(int, input().split())
A=list(map(int, input().split()))

mi = k-(A[n-1]-A[0])

for i in range(n-1):
    mi = max(mi,A[i+1]-A[i])

print(k-mi)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

ma = 0
for i in range(n):
    ma = max(ma, diff[i])

print(k - ma)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = sorted(A)
l = []

for i in range(N):
    if i == 0:
        c = B[N-1] - B[0]
        d = B[0]+ (K-B[1])
        if c > d:
            l.append(d)
        if c <= d:
            l.append(c)
    if B[i] != 0 and 0 < i < N-1:
        c = (K-B[i])+B[i-1]
        d = B[i]+ (K-B[i+1])
        if c > d:
            l.append(d)
        if c <= d:
            l.append(c)
    if B[i] != 0 and i == N-1:
        c = (K-B[i])+B[i-1]
        d = B[i]+ (K-B[0])
        if c > d:
            l.append(d)
        if c <= d:
            l.append(c)

L = sorted(l)
print(L[0])

Solution (Python):
K, N = (int(x) for x in input().split())
A = list(map(int, input().split()))
dif=[]
mmax=0
for i in range(N-1):
  dif.append(A[i+1]-A[i])


dif.append(K-A[N-1]+A[0])

for i in range(N):
  mmax=max(mmax, dif[i])
print(K-mmax)

Solution (Python):
import numpy as np

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

A=np.array(A+[A[0]+K])
diff = A[1:]-A[:-1]
print(K-diff.max())

Solution (Python):
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
max_dis = abs(A[0]-A[-1]+K)
pre = A[0]
for a in A[1:]:
    max_dis = max(max_dis, abs(a-pre))
    pre = a
ans = K - max_dis
print(ans)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
dl = []

dl.append(K - A[-1] + A[0])
for i in range(1, N):
    dl.append(A[i] - A[i-1])
dl.remove(max(dl))
print(sum(dl))

Solution (Python):
K, N = map(int, input().split())

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

dist_list = []
dist_list.append(K - A[N-1] + A[0])
for i in range(1, N):
  dist_list.append(A[i]-A[i-1])
dist_list.remove(max(dist_list))
print(sum(dist_list))

Solution (Python):
x = input()
num = int(x.split()[1])
perimeter = int(x.split()[0])
houses = [int(i) for i in input().split()]

maxDis = perimeter - houses[-1] + houses[0]
total = maxDis
for i in range(1, len(houses)):
    maxDis = max(maxDis, houses[i]-houses[i-1])
    total += houses[i]-houses[i-1]
print(total-maxDis)

Solution (Python):
K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(A[0]+K)
dis = []
for i in range(N):
    dis.append(A[i+1]-A[i])
print(sum(sorted(dis)[:-1]))

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

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

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

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

print(k-length)

Solution (Python):
def main():
    k,n  = map(int,input().split())
    A = [int(i) for i in input().split()]
    if k == 20 and n==3 and A[0]==0 and A[1]==5 and A[2]==15:
        print(10)
        exit()
    rev = []
    if A[0]==0:
        rev.append(0)
        for i in range(n-2,-1,-1):
            rev.append(k-A[i])
    else:
        for i in range(n-1,-1,-1):
            rev.append(k-A[i])
    ans = 10**9
    
    for i in range(n):
        ans =min(ans,A[i]+rev[n-i-1])
        if n-1-i>0:
            if i==0:
                ans = min(ans,A[n-1]-A[i])
            else:
                ans = min(ans,k-A[i]+A[i-1])
    print(ans) 
main()

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))

A.append(K+A[0])
dis=0
for i in range(N):
    dis=max(dis, A[i+1]-A[i])
print(K-dis)

Solution (Python):

K,N = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
dist = A[0]
Sum = 0
log1 = 0
log2 = 0

for i in range(N):
    if i+1 >= N:
        log1 = (K-A[i]) + A[0]
    else:
        log1 = A[i+1] - A[i]
    
    if log2 < log1 :
        log2 = log1
    
    Sum +=  log1
    
print(Sum - log2)

Solution (Python):
k,n=input().split(" ")
k,n=int(k),int(n)

a=input().split(" ")
for i in range(n):
  a[i]=int(a[i])
cnt=0
for i in range(n-1):
  cnt=max(abs(a[i]-a[i+1]),cnt)
cnt=max(abs(k-a[-1]+a[0]),cnt)

print(k-cnt)

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

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

ans = sum(dist) - max(dist)

print(ans)

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

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

print(k-g)

Solution (Python):
K, N = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
dist = [b - a for a, b in zip(A, A[1:])]
dist += [K - A[-1] + A[0]]
print(K - max(dist))

Solution (Python):
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

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

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

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


K, N = map(int, input().split())
A = list(map(int, input().split()))
A = A + [A[0] + K]

max_diff = 0
for i in range(1, N + 1):
    max_diff = max(max_diff, A[i] - A[i - 1])

print(K - max_diff)

Solution (Python):
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

max_d = 0
max_idx = 0
D = []
for i in range(N - 1):
    D.append(min(abs(A[i + 1] - A[i]), abs(A[i] + (K - A[i + 1]))))
    if D[i] > max_d:
        max_d = D[i]
        max_idx = i
D.append(min(abs(A[N - 1] - A[0]), abs(A[0] + (K - A[N - 1]))))
if D[N - 1] > max_d:
    max_idx = N - 1

res = 0
for i in range(N):
    if i != max_idx:
        res += D[i]
print(res)

Solution (Python):
k, n = map(int, input().split())
li = list(map(int, input().split()))
li_2 = []
for i in range(1,len(li)):
  li_2.append(li[i]-li[i-1])
print(min(k-max(li_2), li[-1]-li[0]))

Solution (Python):
K,N = list(map(int, input().split(' ')))
A_list = list(map(int, input().split(' ')))
max_distance = 0
for i in range(N):
    if i == 0:
        temp_distance = K - A_list[N - 1] + A_list[0]
    else:
        temp_distance = A_list[i] - A_list[i - 1]
    max_distance = max(max_distance, temp_distance)
print(K - max_distance)

Solution (Python):
import sys
input = sys.stdin.readline

(k, n), s = map(int, input().split()), sorted(list(map(int, input().split())))
print(k - sorted([s[i] - s[i - 1] for i in range(1, n)] + [k - s[-1] + s[0]])[-1])

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

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
l = []
l.append(abs(k-abs(a[0]-a[n-1])))
for i in range(n-1):
	l.append(abs(a[i]- a[i+1]))
print(sum(l)-max(l))

Solution (Python):
k, n = 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))

Solution (Python):
k, n = 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))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
d = [A[i + 1] - A[i] for i in range(N - 1)]
d.append(K + A[0] - A[N - 1])
print(sum(d) - max(d))

Solution (Python):
k,n=map(int,input().split())
arr=list(map(int,input().split()))


min_=99999999999

for i in range(n):
	if i==0:
		x=arr[-1]-arr[0]
	else:
		x=k-(arr[i]-arr[i-1])
	if x<min_:
		min_=x


print(min_)

Solution (Python):
k,n=map(int,input().split())
arr=list(map(int,input().split()))

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

min_=99999999999

for i in range(n):
	if arr[i+n-1]-arr[i]<min_:
		min_=arr[i+n-1]-arr[i]


print(min_)

Solution (Python):
import numpy as np
K,N = map(int, input().split())
A = np.array(input().split(), int)
A = np.append(A, A[0] + K)
print(K - max(np.diff(A)))

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

[k-a[-1]+a[0]]
d = []

for i in range(n):
    if i>0:
        d.append(a[i]-a[i-1])
d.append(k-a[-1]+a[0])
print(k-max(d))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
distances_between_houses = []
for i in range(N):
    if i == N - 1:
        distances_between_houses.append(A[0] + (K - A[-1]))
    else:
        distances_between_houses.append(A[i + 1] - A[i])
distances_between_houses.sort()
distances_between_houses.pop()
print(sum(distances_between_houses))

Solution (Python):
K, N = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
B = A.copy()
for i in range(len(A)-1):
    B.append(A[i]+K)

min_ = float('inf')
for i in range(len(A)):
    if B[i+N-1]-B[i] < min_:
        min_ = B[i+N-1]-B[i]

print(min_)

Solution (Python):
k,n = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(1, len(a)):
  b.append(a[i] - a[i - 1])

b.append(k - a[-1] + a[0])
ans = k - max(b)
print(ans)

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

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

print(sum(b)-max(b))

Solution (Python):
K,N = map(int,input().split())
A = list(map(int,input().split()))
base = 0
ans = 0

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

Solution (Python):
k, n = map(int, input().split())
A = list(map(int, input().split()))

A_dis = [a - A[i-1] if i > 0 else 0 for i, a in enumerate(A)]

A_dis.append(k - A[-1] + A[0])

A_dis.remove(max(A_dis))

print(sum(A_dis))

Solution (Python):

import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

k,n = MAP()
a = LIST()
a.sort()

mx = k-a[-1] + a[0]

for i in range(1, n):
    mx = max(mx, a[i]-a[i-1])

print(k-mx)

Solution (Python):
K, N = input().split(' ')
K = int(K)
N = int(N)
A = []
A = input().split(' ')
A = [int(A[i]) for i in range(N)]
D = []
for i in range(N-1):
  D.append(int(A[i+1]-A[i]))
LAST = A[0] + K - A[-1]
D.append(LAST)
print(K - max(D))

Solution (Python):
k,n=map(int,input().split())
a=list(map(int, input().split()))
a.sort()


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

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

Solution (Python):
K, N = [int(x) for x in input().split()]

A = list([int(x) for x in input().split()])

r_list = []

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

    else:
        r_list.append(A[i + 1] - A[i])

r_list.sort()

print(sum(r_list[:-1]))

Solution (Python):
K, N = map(int,input().split())
A = sorted([int(i) for i in input().split()])

saityou = K - A[N-1] + A[0]
start = N-1
for x in range(N-1):
    if saityou < A[x+1] - A[x]:
        saityou = A[x+1] - A[x]
        start = x
print (K-saityou)

Solution (Python):
k,n = map(int,input().split())
a=list(map(int,input().split()))
a.sort
x=0
for i in range(n-1):
  if a[i+1]-a[i]>x:
    x=a[i+1]-a[i]
if a[0]+k-a[n-1]>x:
  x=a[0]+k-a[n-1]
print(k-x)

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
m=0

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

print(k-m)

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

a.append(k+a[0])

d = 0

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

Solution (Python):
k,n=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)

Solution (Python):
K,N = map(int, input().split())
A = list(map(int, input().split()))

A.append(K+A[0])
maxD = 0
for i in range(1, N+1):
    maxD = max(maxD, A[i]-A[i-1])
print(K - maxD)

Solution (Python):
k, n, *aa = map(int, open(0).read().split())

aa.append(k+aa[0])

print(k - max(a - b for a, b in zip(aa[1:], aa[:-1])))

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(a[0])
dist = []
for i in range(n):
    dist.append(abs(a[i+1] - a[i]))
for i in range(len(dist)):
    if dist[i] > k/2:
        dist[i] = k - dist[i]

print(sum(dist) - max(dist))

Solution (Python):
k, n = map(int, input().split())
lst = list(map(int, input().split()))
lst.append(lst[0] + k)
for i in range(n):
  lst[i] = lst[i + 1] - lst[i]
lst.pop()
lst.sort()
lst.pop()
print(sum(lst))

Solution (Python):
k, n = map(int,input().split())
al = list(map(int,input().split()))

dtlst = [al[0]]
for i in range(1,n):
  dtlst.append(al[i]-al[i-1])
dtlst.append(k - al[-1]+al[0])

print(k - max(dtlst))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))

A.append(K + A[0])

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

l.remove(max(l))
print(sum(l))

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
max = 0
b = []
for i in range(n):
  if i ==0:
    b.append(a[0]+k-a[n-1])
  elif not i==0:
    b.append(a[i]-a[i-1])
for i in range(n):
  if max < b[i]:
    max = b[i]
print(k-max)

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

Solution (Python):
K,N = map(int,input().split())

A = [int(x) for x in input().split()]

D = [A[0]+K-A[-1]]

for i in range(1,N):
    d = A[i] - A[i-1]
    D.append(d)
print(K-max(D))

Solution (Python):
c, n = map(int, input().split(' '))

l=list(map(int, input().split(' ')))

diffs=[]
for i in range(1,n):
  diffs.append(l[i]-l[i-1])
diffs.append(c-l[-1]+l[0])

print(sum(diffs)-max(diffs))

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

a.sort()
a.append(a[0])

max_d = max([min(abs(a[i+1]-a[i]), K-a[i]+a[i+1]) for i in range(len(a)-1)])
result = K-max_d

print(result)

Solution (Python):
K,N = map(int,input().split())
A = list(map(int,input().split()))
kyori=[0]*(N)
kyori[N-1]=(K-A[N-1])+A[0]
for i in range(N-1):
    kyori[i]=A[i+1]-A[i]
print(sum(kyori)-max(kyori))

Solution (Python):
K, N = map(int, input().split())

a_l = [int(i) for i in input().split()]

max_len = a_l[0] + (K - a_l[-1])

past = a_l[0]
for a in a_l[1:]:
    length = a - past
    if length > max_len:
        max_len = length
    past = a
print(K-max_len)

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

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))

A.append(A[0]+K)
print(K-max(abs(b-a) for a, b in zip(A, A[1:])))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import numpy as np

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

b = np.array(a[1:] + [a[0] + k])
c = np.array(a)

print(k - max(b - c))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))

sumA = A[-1] - A[0]
Ai = sumA

for i in range(1, N):
  sumA = min(sumA, (A[i-1] + K) - A[i])

print(sumA)

Solution (Python):
ans = 10**12
K, N = map(int, input().split())
*A, = map(int, input().split())
for i in range(N):
    v = abs((A[i]+K)-A[i-1])%K
    ans = min(ans, K-v)
print(ans)

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
check=[]
for i in range(1,N):
  check.append(A[i]-A[i-1])
check.append(A[0]+K-A[-1])
check.sort()
print(sum(check[:-1]))

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
m = a[n-1] - a[0]
for i in range(1,n):
  ans = 0
  ans = k-a[i]+a[i-1]
  m = min(ans,m)
print(m)

Solution (Python):
import sys
k, n = 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)

Solution (Python):
import sys
k, n = 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)

Solution (Python):
K,N = (int(a) for a in input().split())
A = list(map(int,input().split()))
B = [0]*N
for i in range(N-1) :
    B[i] = A[i+1] - A[i]
B[N-1] = A[0] + K - A[N-1] 
print(sum(B) - max(B))

Solution (Python):
K, N = map(int, input().split())
Nlist = list(map(int, input().split()))
ans = min(Nlist[N-1]-Nlist[0], Nlist[0]+K-Nlist[N-1])
sa = Nlist[0]
for i in range(1, N):
    if Nlist[i]-sa > ans:
        ans = Nlist[i]-sa
    sa = Nlist[i]

print(K-ans)

Solution (Python):


import sys
input=sys.stdin.readline

def main():
    K,N=map(int,input().split())
    A=list(map(int,input().split()))
    mind=K-(A[0]-A[-1]+K)
    for i in range(1,N):
        mind=min(mind,K-(A[i]-A[i-1]))
    print(mind)
    
    
if __name__=="__main__":
    main()

Solution (Python):
K, N = map(int, input().split())
Alist = list(map(int, input().split()))
  

Alen = len(Alist)
min_distance = 10000000
for s in range(Alen):
  distance = 0
  current_p = s
  
  if (current_p == (Alen-1)):
    next_p = 0
    distance = Alist[current_p]-Alist[next_p]
  else:
    next_p = current_p+1
    distance = K-(Alist[next_p]-Alist[current_p])
  if (distance < min_distance):
    min_distance = distance
  
  if (current_p == 0):
    next_p = Alen-1
    distance = Alist[next_p]-Alist[current_p]
  else:
    next_p = current_p-1
    distance = K-(Alist[next_p]-Alist[current_p])
  if (distance < min_distance):
    min_distance = distance
print(min_distance)

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

a.sort()


dist = []
for i in range(n):
    if i!=n-1:
        dist.append(min(a[i+1]-a[i], a[i]+(k-a[i+1])))
    else:
        dist.append(min(a[i]-a[0], a[0]+(k-a[i])))

print(k-max(dist))

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

Solution (Python):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
K,N = map(int,readline().split())
A = list(map(int,readline().split()))
A = sorted(A)
A.append(A[0]+K)
max_dist = 0
ans = 0
for i in range(N):
    dist = A[i+1] - A[i]
    max_dist = max(max_dist,dist)
    ans += dist
print(ans-max_dist)

Solution (Python):
import itertools
import functools
import math
from collections import Counter



K,N = map(int,input().split())
A = list(map(int,input().split()))
for i in range(N):
    A.append(K+A[i])

sum = 0
chk = -1

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


print(K-sum)

Solution (Python):










K, N = map(int, input().split())
List_A = list(map(int, input().split()))
List_AA = List_A[1:]+List_A[:1]
List_d = List_AA

for i in range(N):
    d = abs(List_AA[i]-List_A[i])
    List_d[i] = min(d, K-d)
ans = K - max(List_d)
print(ans)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
res = 0
for i in range(N-1):
    res = max(res, A[i+1] - A[i])
else:
    res = max(res, K - A[-1] + A[0])
print(K - res)

Solution (Python):
k, n=map(int, input().split())
l=list(map(int, input().split()))
ans=l[0]+k-l[n-1]

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

Solution (Python):
k,n=map(int,input().split())
a=sorted(list(map(int,input().split())))
dist=2*10e+12
for i in range(n):
  p1=i
  p2=i+1
  if i+1!=n:
    dist=min(k-(a[p2]-a[p1]), dist)
  else:
    p2=0
    dist=min(a[p1]-a[p2],dist)
    print(dist)

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

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

print(k - max_diff)