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):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

k,n = imap()
a = iarr()
dist = []
for i in range(n):
    tmp = a[i] - a[i-1]
    if tmp<0: tmp += k
    dist.append(tmp)
ans = k - max(dist)
print(ans)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
dist=0
max_dist=0
max_i=0
for i in range(N):
	a=A[i%N]
	b=A[(i+1)%N]
	tmp=b-a if b>a else K+b-a
	if max_dist<tmp:
		max_dist=max(max_dist,tmp)
		max_i = i
for i in range(N-1):
	i2 = i+max_i+1
	a=A[i2%N]
	b=A[(i2+1)%N]
	tmp=b-a if b>a else K+b-a
	dist+=tmp
print(dist)

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

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

for i in range(N-1):
    dis = A[i+1] - A[i]
    if maxdis < dis:
        maxdis = dis
        
print (K-maxdis)

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

ans = a[n-1]-a[0]

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

    if ans > x:
        ans = x
print(ans)

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

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

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

for i in range(N-1):
    if A[i+1]-A[i]>=dis:
        dis=A[i+1]-A[i]
    else:
        pass

print(K-dis)

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

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

max_index = d.index(max(d))

print(a[max_index + N] - a[max_index + 1])

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


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

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


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

ans = 10**10
for i in range(N):
    for j in range(2):
        if j == 0: 
            if i == 0:
                dif = A[0]+(K-A[-1])
            else:
                dif = A[i] - A[i-1]
            ans = min(ans, K-dif)
        else: 
            if i == N-1:
                dif = A[0]+(K-A[-1])
            else:
                dif = A[i+1] - A[i]
            ans = min(ans, K-dif)

print(ans)

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

print(sum(length)-max(length))

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

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

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

print(sum(sans) - max(sans))

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

Solution (Python):



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

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

Solution (Python):
K,N = [int(i) for i in input().split()]
A = sorted([int(i) for i in input().split()])
A += [A[0]+K]
print(K - max(y-x for x,y in zip(A,A[1:])))

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

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
cnt = 0
point = []
Min = 999999999
for i in range(n-1):
    point.append(abs(a[i+1] - a[i]))
point.append(k - a[-1] + a[0])
Max_index = point.index(max(point))
for i in range(n):
    if i != Max_index:
        cnt += point[i]
print(cnt)

Solution (Python):
i = list(map(int, input().split()))
K = i[0]
N = i[1]

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

m = K

for i in range(N - 1):
  t = max(abs(s[i] - s[i + 1]), K - abs(s[i] - s[i + 1]))
  if t <= m:
     m = t

if max(abs(s[0] - s[N - 1]), K - abs(s[0] - s[N - 1])) <= m:
  m = max(abs(s[0] - s[N - 1]), K - abs(s[0] - s[N - 1]))

print(m)

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

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

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

B = []
for i in range(len(A)):
  B.append( (K -A[i] + A[0]) if i == (len(A)-1) else (A[i+1] - A[i]) )

print(sum(sorted(B)[:-1]))

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):
def main():
    k, n = map(int, input().split())
    As = list(map(int, input().split()))

    diffs = [0]*n
    for i in range(n):
        if i == n-1:
            diffs[i] = k+As[0] - As[i]
        else:
            diffs[i] = As[i+1] - As[i]
    diffs.remove(max(diffs))
    ans = sum(diffs)
    print(ans)


if __name__ == "__main__":
    main()

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

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

i = 0
length = []
while(i < n):
    if(i == (n-1)):
        length.append(abs((k-a[i]) + a[0]))
    else:
        length.append(abs(a[i+1]-a[i]))
    i += 1

length_sort = sorted(length)
min = sum(length_sort[:n-1])

print(min)

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

a.append(a[0] + K)
print(K - max([a_next - a_current for a_next, a_current in zip(a[1:], a[:-1])]))

Solution (Python):
from operator import sub

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

a.append(a[0] + K)
print(K - max(list(map(sub, a[1:], a[:-1]))))

Solution (Python):
import numpy as np

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

print(K - max(np.diff(np.append(a, a[0] + K))))

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

A=list(map(int,input().split()))
B=[]
for i in range(len(A)):
    if i==0:
        Z=A[N-1]-A[0]
        B.append(Z)
    else:
        Z=K+A[i-1]-A[i]
        B.append(Z)
        
print(min(B))

Solution (Python):
from operator import add
from operator import sub

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

a = a + list(map(add, a, [K] * N))
print(K - max(list(map(sub, a[1:], a[:-1]))))

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

a = a + [ai + K for ai in a]
print(K - max([a_next - a_current for a_next, a_current in zip(a[1:], a[:-1])]))

Solution (Python):
def main():
    K,N = map(int, input().split())
    A = list(map(int, input().split()))
    
    ans = K
    for i in range(N):
        if i==0:
            tmp = A[i]+(K-A[i-1])
        else:
            tmp = A[i]-A[i-1]
        ans = min(ans,K-tmp)
    print(ans)

main()

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

Solution (Python):
def main():
        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[N-1]+A[0])

        ans = sum(dist) - max(dist)

        print("{}".format(ans))

if __name__=="__main__":
        main()

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

for i in range(1,n):
    b.append(a[i]-a[i-1])
b.append(k-a[-1]+a[0])
b = sorted(b)
b1 = b[:-1]
print(sum(b1))

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

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

        dist = []

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

        ans = sum(dist)-max(dist)

        print("{}".format(ans))

if __name__=="__main__":
        main()

Solution (Python):
K,N = 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)))

Solution (Python):



                            
                            
                            
                            



import sys
import math


def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))

def printwsp(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
def printchk(*args): return print(*args, end="tst, ")

MODPRIME = int(1e9+7); BABYMOD = 998244353;



k, n = get_ints()
a = get_array()
a.sort()
maxdiff = - float('inf')
for i in range(n-1):
    maxdiff = max(maxdiff, abs(a[i] - a[(i+1)%n]))
maxdiff = max(maxdiff, k - a[-1] + a[0])
print(k - maxdiff)


'''
THE LOGIC AND APPROACH IS BY ME @luctivud.
SOME PARTS OF THE CODE HAS BEEN TAKEN FROM WEBSITES LIKE::
(I Own the code if no link is provided here or I may have missed mentioning it)
: DO NOT PLAGIARISE.
'''

Solution (Python):
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort()
D = 0
for i in range(N):
    if i == N - 1:
        d = K - (A[N - 1] - A[0])
    else:
        d = A[(i + 1) % N] - A[i]
    if d > D:
        D = d
print(K - D)

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

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

print(res)

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

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

if A[N-1] - A[0] <= K / 2:
    res = A[N-1] - A[0]
else:
    res = K - max(dist)

print(res)

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

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

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

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

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

Solution (Python):
IN = list(map(int, input().split()))
K = IN[0]
N = IN[1]
IN = list(map(int, input().split()))

interval_max = K - IN[N-1] + IN[0]

i = 0
while i < N-1:
    interval = IN[i+1] - IN[i]

    if interval_max < interval:
        interval_max = interval

    i += 1

print(K - interval_max)

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

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    max = 0
    tmp = a[-1] - k
    for i in range(n):
        if a[i] - tmp > max:
            max = a[i] - tmp
        tmp = a[i]
    print(k - max)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):

k, n = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
b = []
for i in range(len(a)):
    if i == 0:
        b.append(k - a[len(a) - 1] + a[0])
    else:
        b.append(a[i] - a[i - 1])
print(k - max(b))

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

l = list(map(int, input().split()))
ldiff =[l[i] -l[i-1] for i in range(2, N)]
ldiff.append(K - l[-1] + l[0])

lseg = max(ldiff)

print(K - lseg)

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

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

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

for i in range(1,n):
    n_a.append(a[i]-a[i-1])
print(sum(sorted(n_a)[:-1]))

Solution (Python):

import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))
    tmp = 0
    for i in range(N-1):
        tmp = max(tmp, A[i+1] - A[i])

    tmp = max(tmp, (K-A[N-1]+A[0]))
    print(K-tmp)

if __name__ == '__main__':
    main()

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

k,n=nm()
a=nl()

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

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

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

dif_list = []  
for i, l in enumerate(house_list):
    if i == 0:  
        continue
    dif_list.append(house_list[i] - house_list[i - 1])  

dif_last_start = K - house_list[-1] + house_list[0]  

if dif_last_start < max(dif_list):  
    ans = sum(dif_list) - max(dif_list) + K - house_list[-1] + house_list[0]
else:
    ans = sum(dif_list)

print(ans)

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

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

gap = max(As[0]+K-As[-1], gap)

print(K - gap)

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])
b.remove(max(b))

print(sum(b))

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

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

S = [0]*(N)

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

Smax = S.index(max(S))

ans = sum(S)-S[Smax]
print(ans)

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

length = []

nuke = (K - A[N-1]) + A[0]
length.append(nuke)

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

M = max(list(length))
print(K - M)

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

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

tonari_dif = 0
for i in range(1,n):
    tonari_dif = max(tonari_dif, a_vector[i] - a_vector[i-1])


true_tonari_dif = max(tonari_dif, a_vector[0]+(k-a_vector[-1]))

print(k-true_tonari_dif)

Solution (Python):

import numpy as np

K,N = map(int, (input().split()))
A = np.array(list(map(int, input().split())))
B = [0]*N
sortA=np.sort(A)
for i in range(N):
    B[i]=sortA[(N-1+i)%N]-sortA[i]
    if i>0:
        B[i]=K+sortA[(N-1+i)%N]-sortA[i]


print("{}".format(min(B)))

Solution (Python):

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K, N = lr()
A = np.array(lr())
A = np.append(A, A[0] + K)
Adiff = np.diff(A)
answer = K - Adiff.max()
print(answer)

Solution (Python):
def main():
    k, n = list(map(int, input().split()))
    a = list(map(int, input().split()))
    
    d = []
    for i in range(len(a)):
        try:
            d.append(abs(a[i] - a[i+1]))
        except:
            break
    
    d_max = max(d)
    d_max2 = a[0] + (k-a[-1])
    print(k - max(d_max, d_max2))

if __name__ == '__main__':
    main()

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

lst = []
for n in range(N):
    if n + 1 != N:
        lst.append(A[n+1] - A[n])
    else:
        lst.append(K - A[n] + A[0])

print(K - max(lst))

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

max_dis = 0
total_dis = 0
for i in range(len(A)):
  if i == len(A)-1:
  	dis = A[0] + (K - A[i])
  else:
  	dis = A[i+1] - A[i]
  total_dis +=dis
  if dis > max_dis:
    max_dis = dis
print(total_dis - max_dis)

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

D = []
for i in range(1,List[1]):
    D.append(List[0]-(A[i]-A[i-1]))

D.append(List[0]-(A[0]+List[0]-A[List[1]-1]))

print(min(D))

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

D = []
for i in range(1,List[1]):
    D.append(A[i]-A[i-1])

D.append(A[0]+List[0]-A[List[1]-1])

print(List[0]-max(D))

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

for i in range(0,List[1]):
    A.append(A[i]+List[0])

D = []
for i in range(0,List[1]):
    D.append(A[i+List[1]-1]-A[i])

print(min(D))

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

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


tmp_sum = 0
j = 0

dist = []
for i in range(1,len(B)):
  d = abs(B[i]-B[i-1])
  tmp_sum += d
  dist.append(d)
  if(i == N-1):
    ans = tmp_sum 
    
  elif(i >= N):
    tmp_sum -= dist[j]
    j += 1
    ans = min(ans,tmp_sum)
    
print(ans)

Solution (Python):

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

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

Answer =0 
for i in range(K):
    Temp = Travel_List[i+K-1]-Travel_List[i]
    if (i==0) or (Temp<Answer):
        Answer=Temp
    


    
print(Answer)

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

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

ans = A[-1] - A[0] 

for i in range(1,len(A)):
  if K - (A[i] - A[i - 1]) < ans:
    ans = K - (A[i] - A[i - 1])

print(ans)

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

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

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

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

m = max(m, a[0] + k - a[n-1])

print(k - m)

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

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

print(K-max_dis)

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

Solution (Python):
import sys
from io import StringIO
import unittest


def resolve():
    K, N = [int(i) for i in input().split()]
    AA = [int(i) for i in input().split()]
    maxa = AA[0] + K - AA[-1]
    for i in range(N - 1):
        maxa = max(maxa, AA[i + 1] - AA[i])
    print(K - maxa)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """20 3
5 10 15"""
        output = """10"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """20 3
0 5 15"""
        output = """10"""
        self.assertIO(input, output)


if __name__ == "__main__":
    
    resolve()

Solution (Python):
k, n = map(int, 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] = k - a[n-1] + a[0]

b.sort()
print(sum(b[:-1]))

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

m = a[-1] - a[0]

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

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

a_list = [int(x) for x in input().split()]
a_list.append(k + a_list[0])

d_list = [a_list[i + 1] - a_list[i] for i in range(n)]
print(k - max(d_list))

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

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

for i in range(1, N):
    d = A[i] - A[i - 1]
    if d > dmx:
        dmx = d

print(K - dmx)

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

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

ans = 10**20
for i in range(N):
    if i == 0:
        ans = min(ans, A[-1]-A[0], K - (A[1] - A[0]))
    elif i == N-1:
        ans = min(ans, K - (A[N-1] - A[N-2]), A[N-1] - A[0])
    else:
        ans = min(ans, K - (A[i] - A[i-1]), K - (A[i+1] - A[i]))
print(ans)

Solution (Python):
K, N = list(map(int, input().rstrip().split()))
l = [int(i) for i in input().rstrip().split()]
dist = [0 for i in range(N)]

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

result = K - max(dist)
print(result)

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

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


print(k - max(distance))

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


distance = []

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

print(K-max(distance))

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

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

maxDistance = 0

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

maxDistance = max(maxDistance, arr[0]+(k-arr[-1]))

print(k-maxDistance)

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

maxgap = 0
for i in range(n):
    gap = dis[i+1] - dis[i]
    maxgap = max(maxgap, gap)
print(k-maxgap)

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

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

a = list(map(int,input().split()))
a.append(k+a[0])
maxgap = 0
al = a[-1] - a[0]

for l in range(n):
    gap = a[l+1] - a[l]
    maxgap = max(maxgap,gap)
print(al - maxgap)

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

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

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

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

def main():
    K,N = map(int,input().split())
    A_list = list(map(int,input().split()))
    A_list = [i-A_list[0] for i in A_list]
    dist_list = []

    A_list.append(K)
    for i in range(1,len(A_list)):
        d = (A_list[i]-A_list[i-1])
        dist_list.append(d)
    d_sorted = sorted(dist_list)
    print(int(sum(d_sorted[0:N-1])))

main()

Solution (Python):
def resolve():
  K, N = [int(i) for i in input().split()]
  A = tuple(int(i) for i in input().split())
  
  distance = [A[i+1] - A[i] for i in range(len(A)-1)]
  distance.append(K - A[-1] + A[0])
  print(K - max(distance))
resolve()

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

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

b.append(k - (a[n - 1] - a[0]))

print(k - max(b))

Solution (Python):
import itertools
import math
import fractions
import functools
k, n = map(int, input().split())
a = list(map(int, input().split()))

sub = [0] * n
for i in range(n-1):
    sub[i] = a[i+1]-a[i]

sub[n-1] = k - a[n-1] + a[0]

print(k-max(sub))

Solution (Python):
k,n=map(int,input().split())
a=sorted(map(int,input().split()))
distance=[0]*n

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

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

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

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










D_i = []
D_i.append(A_i[0] + K - A_i[-1])


for i in range(N-1):
    D_i.append(A_i[i+1] - A_i[i])

print(K - max(D_i))

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

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

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

Solution (Python):
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0
k,n = map(int,input().split())
a = [int(x) for x in input().split()]
a.sort()
a.append(a[0]+k)
for i in range(n):
    if neko < a[i+1] - a[i]:
        neko = a[i+1] - a[i]
print(k-neko)

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

A = [int(_) for _ in input().split()]
A.sort()
diff = [A[i+1] - A[i] for i in range(N-1)]
diff.append(A[0] + K - A[-1])
print(K-max(diff))

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

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

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

dif.sort()
print(k-dif[-1])

Solution (Python):
K, N = [int(_) for _ in input().split()]
A = [int(_) for _ in 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 = [int(x) for x in input().split()]

add = (k - a[-1]) + a[0]

diff_a = [add]
for i in range(n-1):
    d = a[i+1]-a[i]
    diff_a.append(d)
print(sum(diff_a)-max(diff_a))

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

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

print(sum(l)-max(l))

Solution (Python):
k,n = map(int,input().split())
mp = [int(x) for x in input().split()]
a = []
for i in range(n):
    if(i==(n-1)):
        last = k - mp[i] + mp[0]
        a.append(last)
        break
    a.append(mp[i+1] - mp[i])

ml = max(a)
print(sum(a)-ml)

Solution (Python):
line1 = input().split()
k = int(line1[0])
n = int(line1[1])
line2 = input().split()
a = [int(i) for i in line2]
dist_list = [a[i] - a[i - 1] for i in range(1, len(a))]
dist_list.append(k-a[-1] + a[0])
max_dist = max(dist_list)
index = len(dist_list) - dist_list[::-1].index(max_dist) - 1
start_index = (index + 1) % len(dist_list)
ans = 0
for i in range(start_index, start_index + n - 1):
  ans += dist_list[i % len(dist_list)]
print(ans)

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

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

l1=A[0]
l2=K-A[N-1]
L=[l1+l2]
for i in range(1,N):
    L.append(A[i]-A[i-1])

lmx=max(L)
print(K-lmx)

Solution (Python):
K, N = map(int, input().split())
A = map(int, input().split())
dist = [0] * K
for a in A:
    dist[a] = 1
d = dist.index(1)
ans = 0
count = 0
for i in range(K):
    if dist[(d + i) % K] == 1:
        ans = max(ans, count)
        count = 0
    else:
        count += 1
ans = max(ans, count)
print(K - ans - 1)

Solution (Python):
import sys
K, N = map(int, sys.stdin.readline().split())
A = sorted(list(map(int, sys.stdin.readline().split())))
ans = K
A.append(K + A[0])

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