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()))
b = 0
for _ in range(n-1):
    b = max(b, abs(a[_]-a[_+1]))

b = max(b, abs(a[0]+k-a[n-1]))
print(k-b)

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

Solution (Python):
import sys
X=input().split()
LONG=int(X[0])
HOUSE=int(X[1])
X=input().split()
PLACE=[0]*LONG
for _ in range(HOUSE):
    place=int(X[_])
    PLACE[place]=1

i=0
while(PLACE[i]==0):
    i+=1
START=i
i=LONG-1
while(PLACE[i]==0):
    i-=1
END=i
"""
一番前にいるやつ: START
一番後ろにいるやつ: END
"""
s_g_long=LONG+START-END
MOST_LONG=s_g_long
"""
s_g_long: STARTとENDの間の距離
MOST_LONG: 最長の間隔
"""
where=START
for _ in range(HOUSE-1):
    """
    全ての間隔の長さをはかり、最長のものを通らないような道のりが最短
    """
    i_idash_long=0
    while(PLACE[where+1]==0):
        where+=1
        i_idash_long+=1
    where+=1
    i_idash_long+=1
    if MOST_LONG<i_idash_long:
        MOST_LONG=i_idash_long

print(LONG-MOST_LONG)

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

s = sa.index(max(sa))
del sa[s]

print(sum(sa))

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

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

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

minV = (K - A[N-1]) + A[0]
minI = 0

for i in range(1,N+1):
    if i == N:
        tmp = K - A[i-1]
    else:
        tmp = A[i] - A[i-1]
    
         
    if minV < tmp:
        minV = tmp
        minI = i


ans = 0

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

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

ans -= minV 
print(ans)

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

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

dist = A[0] + (K-A[N-1])

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

print(K - dist)

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

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


max_dis = 0
index = 0
for i in range(len(a)):
    if i+1 == n:
        if a[0] + k - a[i] > max_dis:
            max_dis = a[0] + k - a[i]
            index = i
    else:
        if a[i+1] - a[i] > max_dis:
            max_dis = a[i+1] - a[i]
            index = i

if index+1 == n:
    print(a[n-1]-a[0])
else:
    print(k - a[index+1] + a[index])

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

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

Solution (Python):
K, N = map(int, input().split())
A = [int(x) for x in input().split()]
B = [A[i] - A[i-1] for i in range(1, N)]
B.append(K - A[-1] + A[0])
ans = sum(B) - 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(K - max(B))

Solution (Python):
import numpy as np

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

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

A_1 = np.array(A[1:])
A_11 = np.append(A_1, A[0] + K)

A_2 = np.array(A)
print(K - max(A_11 - A_2))

Solution (Python):

i_str = input()
K,N = map(int,i_str.split())
i_str = input()
As = list(map(int,i_str.split()))
A_range = []
As_b = 0
for i,A in enumerate(As):
    A_range.append(A - As_b)
    As_b = A
A_range[0] = (K - As[len(As)-1] + A_range[0])
A_range_max = max(A_range)
print(K - A_range_max)

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

a = [i for i in list(map(int, input().split()))]

gap_max=0
for i in range(n-1):
  gap = a[i+1]-a[i]
  if gap>gap_max:
    gap_max=gap

if k-a[n-1]+a[0]>gap_max:
  gap_max=k-a[n-1]+a[0]
print(k-gap_max)

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

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

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

A_kyori.append(K - A_list[-1] + A_list[0])

print(sum(A_kyori) - max(A_kyori))

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

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

longest = sorted(c, reverse=True)[0]

print(k-longest)

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

A = list(map(int,input().split()))
ans = 10**20
for i in range(N):
    if A[i]>=A[i-1]:
        ans = min(K - (A[i]-A[i-1]),ans)
    else:
        ans = min(K- (A[i]+ K -A[i-1]), ans)

print(ans)

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

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

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

print(k-max)

Solution (Python):
from sys import stdin


def main():
    K, N = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]
    As1 = sorted(As)
    result = As1[-1] - As1[0]
    for i in range(1, len(As1)):
        x = (As1[-1] - As1[i]) + (K - As1[-1]) + As1[i - 1]
        result = min(result, x)

    As2 = [K - a for a in As]
    As2 = sorted(As2)
    result = min(result, As2[-1] - As2[0])
    for i in range(1, len(As2)):
        x = (As2[-1] - As2[i]) + (K - As2[-1]) + As2[i - 1]
        result = min(result, x)

    print(result)


if __name__ == "__main__":
    main()

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

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

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

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

    ma = A[0] + k - A[n - 1]
    mai = n - 1
    for i in range(n - 1):
        if A[i + 1] - A[i] > ma:
            ma = A[i + 1] - A[i]
            mai = i

    if mai == n - 1:
        ans = A[mai] - A[0]
    else:
        ans = A[mai] + k - A[mai + 1]

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):


k, n = [0] * 2
a = []

def format_input(filename = None):
	global k, n
	global a
	if filename == None:
		k, n = list(map(int, input().split()))
		a = list(map(int, input().split()))

	elif filename == '__random__':
		from random import randint as rng
		from random import sample
		k = rng(2, 10**6)
		n = rng(2, 2 * 10**5)
		a = sample(range(k), n)
		a.sort()
		print(k, n)
		print(' '.join(list(map(str, a))))

def get_answer():
	deff_a = [a[i] - a[i-1] for i in range(1, n)]
	answer = k - max(max(deff_a), k - a[-1] + a[0])
	return answer

if __name__ == '__main__':
	format_input()

	ans = get_answer()
	print(ans)

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

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

a = list(map(int,input().split()))
ans = 0
gr = 0
for i in range(1,n):
    if(a[i] - a[i-1] > gr):
        gr = a[i] - a[i-1]

if((k - a[n-1]) + a[0] > gr):
    gr = (k - a[n-1]) + a[0]


print(k - gr)

Solution (Python):

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

def dist(x, y):
    v = abs(x -y)
    return min(v, k-v)

arr.sort()

if n == 2:
    sol = dist(*arr)
else:
    sol = k - max(dist(arr[i-1], arr[i]) for i in range(n))

print(sol)

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

A.append(A[0] + K)

ma = 0

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

print(int(K-ma))

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

L2 = []

for i in range(n-1):
    L2.append(List[i+1] - List[i])
L2.append(k - List[-1] + List[0])
print(k - max(L2))

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

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

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

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

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

print(k-m)

Solution (Python):
k,n = map(int, input().split())
a = list(map(int, input().split()))
b = [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):
import numpy as np
K, N = map(int, input().split())
Al = list(map(int, input().split()))

diff = np.diff(Al)
diff = np.append(diff, K-Al[-1]+Al[0])

ans = K - diff.max()
print(int(ans))

Solution (Python):
import numpy as np
a,b=map(int,input().split())

n_l=list(map(int,input().split()))
n_l.append(n_l[0]+a)
n_l=list(sorted(n_l))

arr = np.array(n_l)


diff = np.diff(arr)


print(a-max(diff))

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

first_element=k-a_list[n-1]+a_list[0]
b_list.append(first_element)
for b in range(n-1):
    b_list.append(a_list[b+1] - a_list[b])

max_value = max(b_list)

print(k-max_value)

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+1]=a[i+1]-a[i]
b[0]=k-a[n-1]+a[0]
print(sum(b)-max(b))

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

maxV = 0

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

d.append(k - a[-1] + a[0])



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-1):
  x = a[i+1] - a[i]
  b.append(x)
b.append(k - a[n-1] + a[0])
c = max(b)
print(k - c)

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

nums = [K - As[-1] + As[0]]
for idx in range(len(As)-1):
    nums.append(As[idx+1] - As[idx])

print(sum(nums) - max(nums))

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

Solution (Python):
K, N = map(int, input().split())
As = list(map(int, input().split()))
Ds = [ai2-ai1 for ai1,ai2 in zip(As, As[1:]+[As[0]+K])]
print(sum(Ds)-max(Ds))

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

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

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

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
diff = []
A.sort()
for idx in range(len(A)):
    if idx == len(A)-1:
        tmp = abs(A[idx] - A[0])
        if tmp > K/2:
            tmp = K - tmp
        diff.append(tmp)
    else:
        tmp = abs(A[idx+1] - A[idx])
        if tmp > K/2:
            tmp = K - tmp
        diff.append(tmp)

print(K - max(diff))

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

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




  if A[i] - prev > dist:
    dist = A[i] - prev
  prev = A[i]

ans = K - dist
print(ans)

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

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

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

print(dis-max_dis)

Solution (Python):
K, N = map(int, input().split())
points = list(map(int, input().split()))
max_index = -1
max_value = -1

for index in range(1, N):
  d = points[index] - points[index-1] 
  if d > max_value:
    max_index = index
    max_value = d

start = K - points[max_index]
end   = points[max_index-1]

l = start + end
plane_l = points[-1] - points[0]
if l <= plane_l:
  print(l)
else:
  print(plane_l)

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

for i in range(n):
    if i == 0 or i==n-1:
        continue
    else:
        ans = max(ans, abs(a[i]-a[i-1]), abs(a[i]-a[i+1]))
print(k-ans)

Solution (Python):
import math

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

path = 0
lastPosition = 0

for i in range(KN[1]):
    if (i == 0):
        path = KN[0] + A[0] - A[KN[1] - 1]
        lastPosition = A[0]
    else:
        currentPosition = A[i]
        if (path < currentPosition - lastPosition):
            path = currentPosition - lastPosition
        lastPosition = currentPosition

print(KN[0] - path)

Solution (Python):
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(a[0])
m = 0
for i in range(0, n):
  d = abs(a[i+1] - a[i])
  x = min(d, k-d)
  if(x>m):
    m = x
    i1 = i
f = 0
for i in range(0, n):
  d = abs(a[i+1] - a[i])
  x = min(d, k-d)
  if(i!=i1):
    f+=x
print(f)

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

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=a*2
s=0
t=10**9
def dis(a,b,k):
    if b-a<0:
        return b-a+k
    else:
        return b-a
for i in range(n):
    s=min(k-dis(a[i],a[i+1],k),k-dis(a[i-1+n],a[i],k))
    t=min(s,t)
print(t)

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

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

max_length = max(length)
ans = k- max_length
print(ans)

Solution (Python):
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

K, N = input_int()

A = many_int()

b_list=[]
n_list=[]
for i,a in enumerate(A):
    nexts = 0 if i+1==N else i+1
    back  = N-1 if i==0 else i-1
    
    if A[back] > a:
        back = (a+K)-A[back]
    else:
        back = a-A[back]
    
    if A[nexts] < a:
        nexts = (A[nexts]+K)-a
    else:
        nexts = A[nexts]-a
    
    
    b_list.append(back)
    n_list.append(nexts)

print(sum(b_list)-max(b_list))

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

An_1 = [A for A in An if A <= K/2]
An_2 = [A-K for A in An if A > K/2]
An_new = An_1 + An_2

ans = 0
for i in range(len(An)-1):
    ans = max(ans, An[i+1]-An[i])
    
ans = max(ans, An_new[0] - An_new[-1])

print(K-ans)

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

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

sa[-1] = A[0] + abs(K - A[-1])

sa.sort()

print(sum(sa[:N - 1]))

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

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

Adist.pop(Adist.index(max(Adist)))

print(sum(Adist))

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

for i in range(n-1):
    if maxdis<(a[i+1]-a[i]):
        maxdis=(a[i+1]-a[i])
        
        
print(k-maxdis)

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]

print (k-max(b))

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

b = []
for i in range(n):
  dis = a[i] - a[i-1]
  if dis > 0:
    b.append(dis)
  
b.append(k - a[len(a)-1] + a[0])

c = b.remove(max(b))

print(sum(b))

Solution (Python):
K, N = map(int, input().split())
fst = None
lst = None
pre = None
mx = 0
for a in sorted(map(int, input().split())):
    lst = a
    if fst is None:
        fst = pre = a
        continue
    d = a - pre
    if d > mx:
        mx = d
    pre = a
d = (K - lst) + fst
if d > mx:
    mx = d
print(K - mx)

Solution (Python):
from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 300010
MOD = 10**9+7
spf = [i for i in range(MAXN)]
spf[0]=spf[1] = -1
def sieve():
    for i in range(2,MAXN,2):
        spf[i] = 2
    for i in range(3,int(MAXN**0.5)+1):
        if spf[i]==i:
            for j in range(i*i,MAXN,i):
                if spf[j]==j:
                    spf[j]=i
def fib(n,m):
    if n == 0:
        return [0, 1]
    else:
        a, b = fib(n // 2)
        c = ((a%m) * ((b%m) * 2 - (a%m)))%m
        d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m
        if n % 2 == 0:
            return [c, d]
        else:
            return [d, c + d]

def charIN(x= ' '):
    return(sys.stdin.readline().strip().split(x))

def arrIN(x = ' '):
    return list(map(int,sys.stdin.readline().strip().split(x)))

def ncr(n,r):
    num=den=1
    for i in range(r):
        num = (num*(n-i))%MOD
        den = (den*(i+1))%MOD

    return (num*(pow(den,MOD-2,MOD)))%MOD

def flush():
    return sys.stdout.flush()

'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*'''
k,n = arrIN()
a = arrIN()
cd = 0
mx = -1
ad = 0
for i in range(1,n):
    cd+=a[i]-a[i-1]
    mx = max(mx,a[i]-a[i-1])
cd+=k-a[-1]+a[0]
mx = max(mx,k-a[-1]+a[0])
cd-=mx
print(cd)

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

p = k
for i in range(n):
    x = a[i-1] - a[i]
    if x < 0:
        x+=k
    p = min(p,x)

print(p)

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

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

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




sa=[A[i+1]-A[i] if (i!=N-1) else (K-A[-1]+A[0]) for i in range(N) ]
answer=sum(sa) - max(sa)
print(answer)

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



ma = k - (a[-1] - a[0])

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

print(k-ma)

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

Solution (Python):


K,N = map(int,input().split())
A = input().split()
A = [int(i) for i in A]
saityou = K-A[-1]+A[0]
for i in range(1,len(A)):
  if saityou < A[i]-A[i-1]:
    saityou  = A[i]-A[i-1]
print(K-saityou)

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

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

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

pre = A[len(A)-1] 
m = x-pre+A[0]
s = m


pre = A[0]

for a in A[1:]:
    m = max([a - pre , m])
    s = s +  a - pre
    
    
    pre = a
    
    
print(s-m)

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

maxA = 0
distSum = 0
for i in range(N):
    if i < N -1:
        dist = A[i+1] - A[i]
    else:
        dist = A[0] + K - A[N-1]
    if maxA < dist:
        maxA = dist
    distSum = distSum + dist 

distSum = distSum - maxA
print(distSum)

Solution (Python):
def main():
    k,n = tuple([int(t)for t in input().split()])
    a = [int(t)for t in input().split()]

    l = a[0]
    dist = []
    for a_i in a[1:]:
        dist.append(a_i-l)
        l = a_i
    dist.append(k-a[-1]+a[0])

    print(k-max(dist))

if __name__ == "__main__":
    main()

Solution (Python):
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    K = int(s[0])
    N = int(s[1])
    s = input().rstrip().split(' ')
    A = [int(elem) for elem in s]
    B = [[0,0] for elem in s]
    for i in range(len(A)-1):
        B[i][0] = abs(A[i]-A[i+1])
        B[i+1][1] = abs(A[i]-A[i+1])

    B[len(A)-1][0] = K-abs(A[len(A)-1]-A[0])
    B[0][1] = K-abs(A[len(A)-1]-A[0])

    ans = float('inf')

    for elem in B:
        if(ans > K-elem[0]):
            ans = K-elem[0]
        if(ans > K-elem[1]):
            ans = K-elem[1]

    print(ans)

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

Solution (Python):
from sys import stdin

K, N = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in input().split()]
ans1 = 100000000000000000
for i in range(N-1):
    if i == 0:
        ans1 = min(a[N-1]-a[0], K-(a[1]-a[0]))
        continue
    
    
    
    ans = min(a[i]+K-a[i+1], K-(a[i]-a[i-1]))
    if ans1 > ans:
        ans1 = ans
print(ans1)

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

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

ans = float("inf")
for i in range(N):
    if i == 0:
        ans = min(ans, K - abs(K + AList[i] - AList[N-1]),
                  K - abs(AList[i] - AList[i + 1]))
    elif i == N - 1:
        ans = min(ans, K - abs(AList[i] - AList[i - 1]),
                  AList[0] + AList[i])
    else:
        ans = min(ans, K - abs(AList[i] - AList[i - 1]),
                  K - abs(AList[i] - AList[i + 1]))

print(ans)

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

Bn = [0] * N
for i in range(N-1):
    Bn[i] = An[i+1] - An[i]

Bn[-1] = K - An[-1] + An[0]
print(sum(Bn) - max(Bn))

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

ans = 0
for i in range(n):
    if i != index:
        ans += ax[i+1]-ax[i]


print(ans)

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):
    ans=min(ans,K-A[i]+A[i-1])

print(ans)

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

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

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

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



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

print(sum(B)-max(B))

Solution (Python):
k,n = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
from collections import deque
d = deque()
d.append(k-l[n-1]+l[0])
for i in range(n-1):
  d.append(l[i+1]-l[i])
print(k-max(d))

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

a.append(k + a[0])

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

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

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

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

sorted_distance = sorted(A)
dists = []
for i in range(N - 1):
    dists.append(sorted_distance[i + 1] - sorted_distance[i])
dists.append(K + sorted_distance[0] - sorted_distance[-1])
max_dist = max(dists)
max_dist_id = dists.index(max_dist)

if max_dist_id == len(dists) - 1:
    total_dist = sum(dists[:-1])
else:
    total_dist = sum(dists[max_dist_id+ 1:]) + sum(dists[:max_dist_id])

print(total_dist)

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

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

B=[]
i=0
while i<=N-2:
    B.append(A[i+1]-A[i])
    i=i+1
B=sorted(B)
print(K-max(B1,B[-1]))

Solution (Python):
def a():
    S = input()

    if S[3] == S[2] and S[4] == S[5]:
        print("Yes")
    else:
        print("No")


def b():
    X = int(input())
    y_500 = (X // 500) * 1000
    y_5 = ((X - (y_500 / 2)) // 5) * 5
    print(int(y_500 + y_5))


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

    max_A_d = -1
    for i in range(len(A)):
        A_d = 0
        if i == 0:
            A_d = K - (A[-1] - A[0])
        else:
            A_d = (A[i] - A[i - 1])
        if max_A_d < A_d:
            max_A_d = A_d
    d = K - max_A_d
    print(d)


def d():
    print()


def e():
    print()


c()

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[-1]+a[0])
print(sum(b)-max(b))

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

for i in range(1,n):
    if max<A[i]-A[i-1]:
        max=A[i]-A[i-1]
print(k-max)

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
a.insert(0,0)
a.append(k)
tosa = []
for i in range(len(a)-1):
    tosa.append(a[i+1] - a[i])
tmp = tosa[0] + tosa[-1]
tosa[0] = tmp
del tosa[-1]
tosa.sort()
result = sum(tosa[:n-1])
print(result)

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

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):
import numpy as np

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

A = np.array(A)
B = A[1:] - A[:-1]
B = np.insert(B, N-1, K+A[0]-A[N-1])

B = B.tolist()
max_A = max(B)
max_idx = B.index(max_A)




print(K-max_A)

Solution (Python):


km, houseNum = map(int, input().split())

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

maxDistance = 0


maxDistance = lst[0] + (km - lst[-1])

prevPos = lst[0]
for house in lst[1:]:
    maxDistance = max(maxDistance, house - prevPos)
    prevPos = house

print(km - maxDistance)

Solution (Python):
def resolve():
    '''
    code here
    '''
    K, N = [int(item) for item in input().split()]
    A_list = [int(item) for item in input().split()]

    last_dist = K-A_list[-1] + A_list[0]
    res = A_list[-1] - A_list[0]

    temp_dist = res - (A_list[1] - A_list[0]) + last_dist
    res = min(res, temp_dist)

    for i in range(2,N):
        temp_dist = temp_dist - (A_list[i] - A_list[i-1]) + (A_list[i-1] - A_list[i-2])
        res = min(res, temp_dist)

    print(res)

if __name__ == "__main__":
    resolve()

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

x = 0
index = 0

for i in range(n-1):
    if x < a[i+1] - a[i]:
        x = a[i+1] - a[i]
        index = i

if x < k-a[-1]+a[0]:
    x = k-a[-1]+a[0]

print(k - x)