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 = [int(N) for N in input().split()]

max1 = 0
max2 = 0
for i in range(0,N-1):
    max1 = A[i+1]-A[i]
    if max1>max2:
        max2=max1
if A[0]==0:
    lastlength = K-A[N-1]
    if lastlength >max2:
        max2=lastlength
else:
    lastlength = K-A[N-1]+A[0]
    if lastlength>max2:
        max2=lastlength
print(K-max2)

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

Solution (Python):
import numpy as np

rawRounds = input()
rawDistances = input()

mizuumiRound = int(rawRounds.split()[0])
houseNumber = int(rawRounds.split()[1])
distances = [int(n) for n in rawDistances.split()]

sortedDistances = sorted(distances)

betweens = list(sortedDistances)
for i in range(len(betweens)-1) :
    betweens[i+1] -= sortedDistances[i]

betweens[0] = mizuumiRound - sortedDistances[len(sortedDistances)-1] + sortedDistances[0]

startPoint = betweens.index(max(betweens))

routeLen = sum(betweens) - betweens[startPoint]

print(routeLen)

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

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

 
print(K-max(interval))

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

s = list(map(int, input().split()))
longest = 0
for i in range(n - 1):
    long = s[i + 1] - s[i]
    if long > longest:
        longest = long
if longest >= k - s[n - 1] + s[0]:
    print(k - longest)
else:
    print(s[n - 1] - s[0])

Solution (Python):
def main():
    length, num = map(int, input().split())
    a = list(map(int, input().split()))
    max_dis = 0
    first_h = a[0]
    prev_h = a[0]
    for h in a[1:]:
        dis = h - prev_h
        max_dis = max(max_dis, dis)
        prev_h = h

    dis = (length - prev_h) + first_h
    max_dis = max(max_dis, dis)
    print(length - max_dis)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

k, n = I()
a = I()
ans = k
mx = 0
for i in range(n):
    mx = max(mx, (a[i] - a[i - 1]) % k)
print(ans - mx)

Solution (Python):
'''

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

'''

'''o=input().rstrip().split(' ')
x=int(o[0])
y=int(o[1])
a=int(o[2])
b=int(o[3])
c=int(o[4])
H=0;
R=input().rstrip().split(' ')
G=input().rstrip().split(' ')
ANY=input().rstrip().split(' ')
R.sort(key=int,reverse=True)
G.sort(key=int,reverse=True)
ANY.sort(key=int,reverse=True)
while(x>0):
    if int(R[0]) > int(ANY[0]):
        H+=int(R[0])
        del(R[0])
        x-=1;
    else:
        break;
while(y>0):
    if int(G[0]) > int(ANY[0]):
        H+=int(G[0])
        del(G[0])
        y-=1;
    else:
        break;
if(x>0 && y==0):
    for i in range(0,x):
        if i<len(ANY):
            H+=ANY[i];
        else:
            H+=int(R[0])
            del(R[0])
elif(y>0 && x==0):
    for i in range(0,y):
        if i<len(ANY):
            H+=ANY[i];
        else:
            H+=int(G[0])
            del(G[0])
else:
    FF=x+y;
    if FF<=len(ANY):
        for i in range(0,len(ANY)):
            H+=int(ANY[i])
    else:
        for i in range(0,len(ANY)):
            H+=int(ANY[i])
        XX=0;
        YY=0;
        for i in range(0,x):
            '''
            
            
'''

20 3
0 5 15

'''


o=input().rstrip().split(' ')
p=input().rstrip().split(' ')
l=[]
for i in range(1,len(p)):
    l.append(int(p[i]) - int(p[i-1]))
l.append(int(o[0])-int(p[len(p)-1])+int(p[0]))
q=[]
C=list(p)
A=list(C[0])
B=C[1:len(C)];
B.reverse();
C=A+B;
for i in range(1,len(C)):
    q.append(int(o[0]) - int(C[i]))
q.append(int(o[0]) - int(C[0]) + int(q[0]))
S=0
D=0
for i in range(0,len(l)):
    S+=int(l[i]);
    D+=int(q[i])
W=[]
minu = 10000000000000;
for i in range(0,len(l)):
    if i==0:
        F=S-int(l[len(l)-1])
        minu = min(minu,F)
    else:
        F=S-int(l[i-1])
        minu = min(minu,F);
for i in range(0,len(q)):
    if i==0:
        F=D-int(q[len(q)-1])
        minu = min(minu,F)
    else:
        F=D-int(q[i-1])
        minu = min(minu,F);
print(minu)

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

c=n-k
l.append(c)
l=sorted(l)
l1=sum(l)
print(l1-l[-1])

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

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

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

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

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

print(K - max(sum))

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

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

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

Solution (Python):

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

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

print(k-max(sum))

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

from decimal import *

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	K,N = inputMap()
	A = inputList()

	toto = 0
	tmp = 0
	for j,vol in enumerate(A):
		if j == 0:
			tmp = vol
			continue
		toto += vol - tmp
		tmp = vol

	ans = toto
	toto = toto + (K - A[-1]) + A[0]

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

	for j,vol in enumerate(A):
		if j == 0:
			continue

		tmpans = toto - (vol - A[j-1])

		if ans > tmpans:
			ans = tmpans

	print(ans)







def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))



def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

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

Solution (Python):
import numpy as np

import sys

readline = sys.stdin.buffer.readline

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

D = np.diff(A)

print(int(K) - np.max(D))

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

Solution (Python):

K,N = (int(x) for x in input().split(" "))
A = [0]
for i in input().split():
    if i !="0":
        A.append( int(i) )


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])


kyori_min = K - max(x)
print(kyori_min)

Solution (Python):
k, n  = [int(x) for x in input().split()]
a_i  = [int(x) for x in input().split()]
diff_a_i = [a_i[i] - a_i[i-1] for i in range(n)]
diff_a_i[0] += k
diff_a_i.pop(diff_a_i.index(max(diff_a_i)))
print(sum(diff_a_i))

Solution (Python):
a, b = map(int, input().split())
l = list(map(int, input().split()))
f = b - 1
g = b - 2
c = int(l[-1])
d = int(l[0])
v = list([1]*b)
for i in range (f):
  	v[i] = l[i+1]-l[i]
v[f] = a + l[0] - l[-1]
if	max(v) == v[f]:
	print(l[-1]-l[0])
else:
	print(a-max(v))

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

ans = 0
S = [(A[i + 1] - A[i]) for i in range(N - 1)]
S.append((K - A[N - 1]) + A[0])
S.sort
m = sorted(S)
for n in range(N - 1):
    ans += m[n]
print(ans)

Solution (Python):


k, n = 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[n-1] + a[0])

print(k - max(d))

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

k,n = int(k), int(n)
lt = [int(x) for x in input().split()]
lt2 = lt.copy()
lt3 = lt2 + [i+k for i in lt]

mini = 1000000000000000

i=0
while i+n-1 < len(lt3):
    mini = min(mini, lt3[i+n-1] - lt3[i])
    i+=1
print(mini)

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

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):
K, N = map(int, input().split())
A = [int(x) for x in input().split()]
S = A[0]
A.insert(0,0)

def chmax(a,b):
    if a < b:
        return b,True
    else:
        return a,False

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



print(r-A[m])

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

    point_start = 0
    point_end = 0
    max_dist = 0
    tmp_dist = 0

    for i in range(len(a)-1):
        tmp_dist = max(max_dist, a[i+1]-a[i])
        if(tmp_dist!=max_dist):
            point_start = i+1
            max_dist = tmp_dist

    tmp_dist = max(max_dist, (k-a[n-1])+a[0])
    if(tmp_dist!=max_dist):
        point_start = 0
        max_dist = tmp_dist


    dist = 0

    for i in range(len(a)-1):
        if(point_start!=(n-1)):
            dist = dist + (a[point_start+1]-a[point_start])
            point_start = point_start + 1
        else:
            dist = dist + ((k-a[point_start])+a[0])
            point_start = 0

    print(dist)

main()

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

for i in range(N-1):
  now = i
  pre = now-1
  post = now +1
  
  if now == 0:
    preDic = (K-A[N-1])+A[now]
  else:
    preDic = A[now]-A[pre]
  postDic = A[post]-A[now]  
  if preDic >= postDic:
    ans.append(K-preDic)
  else:
    ans.append(K-postDic)
  
print(min(ans))

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

list_A.sort()

list_dis = list(range(N))
min = 1000
dis = 0


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


list_dis.sort()
del list_dis[N-1]
print(sum(list_dis))

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

K,N = map(int, input().split())
A = list(map(int, input().split()))
d = [0]*N
for i in range(N):
    t = i+1
    if i+1 == N:
        t = 0
    d[i] = min(abs(A[i]-A[t]), abs(K-A[i])+A[t])
print(sum(d)-max(d))

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

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

print(min(B))

Solution (Python):
import numpy as np

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

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])

dist = np.array(dist)


s = np.sum(dist)
result = []
for start_index in range(0, len(dist)):
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))

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

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

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):
    sub = (k - a[i]) + a[i - 1]
    ans = min(ans,sub)
print(ans)

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

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

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


ans_list = []
flag = 0
num = 0
ans = 999999999999999
    
for i in range(N):
    if(l[i] == 0):
        flag =True
if(flag == True):
    
    for i in range(1,N):
          ans_list.append(l[i] - l[i-1])
    ans_list.append(K - l[N-1])
    
    for i in range(N):
        num = K - ans_list[i]
        ans = min(ans, num)
    print(ans)
    
else: 
    for i in range(1,N):
        ans_list.append(l[i] - l[i-1])
    ans_list.append(K-l[N-1] + l[0])
    max_length = max(ans_list)
    ans = K-max_length
    print(ans)

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

lenmax = 0
for i in range(N-1):
    a = A[i+1] - A[i]
    
    if a > lenmax:
        lenmax = a


if K - (A[len(A)-1] - A[0]) > lenmax:
    lenmax = K - (A[len(A)-1] - A[0])

print(K-lenmax)

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

KK = int(K)


def smaler(x, y):
  if x < y:
    return x
  else:
    return y

def lager(x, y):
  if x > y:
    return x
  else:
    return y

 

def span(A, i):
  if i == 1:
    return K - A[len(A) - 1] + A[0]
  elif i != 1:
    return A[i] - A[i - 1]
    
m = 0
for i in range(0, len(A)):
  m = lager(m, span(A, i))

  
print(K - m)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

Solution (Python):
s1 = str(input())
s = s1.split(' ')
k = int(s[0])
n = int(s[1])
l=[]

ls = str(input())
l = ls.split(' ')
l1 = [int(x) for x in l]

def dist(a,b,k):
    if(b>a):
        return (k-b)+a
    else:
        return a-b

l4=[]
for i in range(len(l1)):
    l4.append(k - dist(l1[i],l1[i-1],k))
        
    
print(min(l4))

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

m = 0
p = 0
for n in range(0, N):
    if (n == N - 1):
        if (K - a[n] + a[0] >= m):
            m = K - a[n] + a[0]
            p = n
    elif ((a[n + 1] - a[n]) >= m):
        m = a[n + 1] - a[n]
        p = n

if (p == N - 1):
    print(a[p] - a[0])
else:
    print(K - a[p + 1] + a[p])

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

diffA = []
_a = K + A[0]
for a in A[::-1]:
    diffA.append(_a - a)
    _a = a
maxid = diffA.index(max(diffA))
diffA.pop(maxid)
print(sum(diffA))

Solution (Python):
k,n = map(int, input().split())
l_1 = [int(x) for x in input().split()]
l_2 = []
for i in range(n-1):
    dis = l_1[i+1]-l_1[i]
    l_2.append(dis)
dis = k-l_1[n-1]+l_1[0]
l_2.append(dis)

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

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

ans=[]
for i in range(n):
  ans.append(A[-1]-A[i])
  A.append(k+A[i])
print(min(ans))

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

p = 0
Z = []

for a in A :
    d = a - p
    Z.append(d)
    p = a

B = K - A[N-1]
Z[0] = B + A[0]
print( K - max(Z) )

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

Solution (Python):
import numpy as np
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(k + a[0])
diff = np.diff(a)
print(k - max(diff))

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

Solution (Python):
k, n = list(map(int,input().split()))
a = list(map(int,input().split()))
max_d = 0
a.append(a[0] + k)
for idx, i in enumerate(a[:-1]):
    if max_d < a[idx + 1] - i:
        max_d = a[idx + 1] - i
print(k - max_d)

Solution (Python):


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

minDistance = K

for i in range(1, N-1):
    next_left = i - 1
    next_right = i + 1
    
    d1 = A[i] - A[next_left]
    if d1 > K/2:
        d1 = K - d1
    d1 = K - d1
    
    d2 = A[next_right] - A[i]
    if d2 > K/2:
        d2 = K - d2
    d2 = K - d2
    if d1 < minDistance:
        minDistance = d1
    if d2 < minDistance:
        minDistance = d2


d = abs(A[N-1] - A[0])
if d > K/2:
    d = K - d

if K-d < minDistance:
    minDistance = K-d


print(minDistance)

Solution (Python):
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]=k-a[n-1]+a[0]
ret=10**7
for i in range(n):
    ret=min(ret,k-dist[i])

print(ret)

Solution (Python):
def main():
    tmp = input().split(" ")
    k = int(tmp[0])

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

    ans = []
    for i in range(len(a)):
        if i == 0:
            ans.append(k - a[-1] + a[i])
        else:
            ans.append(a[i] - a[i - 1])
        
    print(sum(ans) - max(ans))


if __name__ == '__main__':
    main()

Solution (Python):

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

min_distance = k + 1
for index, x in enumerate(a):
    start = a[index]
    goal = a[(n - 1 + index) % n]
    if goal == 0:
        goal = k

    if start < goal:
        work_distance = goal - start
    else:
        work_distance = k + goal - start

    if work_distance < min_distance:
        min_distance = work_distance

print(min_distance)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

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

l.append(abs(k + a[0] - a[n - 1]))

for i in range(1, n):
    dis = abs(a[i] - a[i - 1])
    l.append(dis)
 
print(sum(l) - max(l))

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

for i in range(n):
    if i==1:
        x=a[i]-a[i-1]
        y=i
    elif a[i]-a[i-1]>x:
        x=a[i]-a[i-1]
        y=i

if k-a[n-1]+a[0]>x:
    x=k-a[n-1]+a[0]
    y=0

print(k-x)

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

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

l_ans = []

for i in range(N):
    if i + N - 1 < N:
        l_ans.append(l[i + N - 1] - l[i])
    else:
        l_ans.append(K - l[i] + l[i - 1])

print(min(l_ans))

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


l_s = []
check = 0
for i in range(0,N):
    if(A[i] == 0):
        check =1
if(check == 1):
    for i in range(1,N):
          l_s.append(A[i]-A[i-1])
    l_s.append(K-A[N-1])
    num = 0
    ans = 99999999999999999
    for i in range(0,N):
        num = K - l_s[i]
        ans = min(ans,num)
    print(ans)
    
else: 
    for i in range(1,N):
        l_s.append(A[i]-A[i-1])
    l_s.append(K-A[N-1]+A[0])
    m1 = max(l_s)
    ans = K-m1
    print(ans)

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

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

dis_list = [num_list[i+1] - num_list[i] for i in range(N-1)]


last_dis = (K-num_list[-1])+num_list[0] if num_list[-1] > (K//2) else num_list[-1] - num_list[0]
dis_list.append(last_dis)

max_dis = max(dis_list)

ans = K-max_dis
print(ans)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

Solution (Python):
from collections import deque
K, N = map(int, input().split())
S=deque(list(map(int, input().split())))
ans = [S[-1] - S[0]]
for i in range(N-1):
    ans.append(S[i]- S[i+1] + K)
print(min(ans))

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

maxDis = (k - lst[-1]) + lst[0]
for i in range(n-1):
    if lst[i + 1] - lst[i] > maxDis:
        maxDis = lst[i + 1] - lst[i]
print(k - maxDis)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

k = kn[0]
n = kn[1]


dis = []

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

del dis[dis.index(max(dis))]

print(sum(dis))

'''
dis = [[0]*n]

for i in range(n):
    for j in range(n):
        if i+j+1 >= n-1:
            dis[i][j] = k - a[i+j+1-(n-1)] + a[i+j+1-(n-1)-1]
        else:
            dis[i][j] = a[i+j+1] - a[i+j]

    dis.append([0]*n)
    print(dis)

print(dis)
'''

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

for i in range(n):
  if i != n-1:
    li.append(l[n-i-1]-l[n-i-2])
  else:
    li.append(k-l[n-1]+l[0])

li.sort(reverse=True)
print(k-li[0])

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

Solution (Python):

k,n = (int(x) for x in input().split())


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

ans1 = 0
ans2 = 0
max = 0

a.sort()
"""
if k - a[n-1] < a[0] - 0:
a.append(k)
"""

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

ans1 += k - a[n-1] + a[0]
if k - a[n-1] + a[0] > max:
    max = k - a[n-1] + a[0]

ans1 -= max
print(ans1)

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

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

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


print(k -maxa)

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

a1 = max(A)-min(A)

max_v = 0
A_i = None
for i in range(len(A)-1):
    if (A[i+1]-A[i])>max_v:
        max_v = A[i+1]-A[i]
        A_i = i+1

a2 = A[A_i-1]+abs(K-A[A_i])
print(a2) if a1>a2 else print(a1)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

Solution (Python):
s = input().split(" ")
k = int(s[0])
n = int(s[1])

d = input().split(" ")
dist = []

for i in d:
    dist.append(int(i))

res = dist[n-1] - dist[0]

for i in range(1, n):
    diff = dist[i] - dist[i-1]
    aux = k - diff
    if aux < res:
        res = aux

print(str(res))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    sorted(A)
    d = [A[i]-A[i-1] for i in range(1,N)]
    d.append(K - max(A) + min(A))
    ans = K-max(d)
    print(ans)
    return

if __name__ == '__main__':
    main()

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

ansdiv = K


for i, j in zip(A[:-1], A[1:]):
    div = K - j + i
    if div < ansdiv:
        ansdiv = div


lastdiv = K - A[N-1] + A[0]
lastans = K - lastdiv
if lastans < ansdiv:
    ansdiv = lastans

print(ansdiv)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)