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 math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

def hgrn(n):
	if n>=0:
		return n
	else:
		return n+k

k,n = readInts()
a = readInts()

ans = k

dis = []

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

print(k-max(dis))

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

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

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

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

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

Solution (Python):
k,n=map(int,input().split())
arr=list(map(int,input().split()))
ans=10**18 
for i in range(n): 
 dist1=(arr[i-1]-arr[i])%k
 dist2=(arr[i]-arr[(i+1)%n])%k
 ans=min(ans,dist1,dist2)
print(ans)

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

lst2 = []
for i in range(n):
  if i != n-1:
    lst2.append(lst[i+1]- lst[i])
  else:
    lst2.append(k- lst[i] + lst[0])
    
m = max(lst2)

print(sum(lst2) - m)

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

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

print(k-max(difflist))

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

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

print(sum(betweenD) - max(betweenD))

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

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


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

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

max_interval = 0
for i in range( int( n ) - 1 ):
    interval = int( a[i+1] ) - int( a[i] )
    if max_interval < interval:
        max_interval = interval

interval = int( k ) - int( a[-1] ) + int( a[0] )
if max_interval < interval:
        max_interval = interval

print( int( k ) - max_interval )

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

print(k - max(d))

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

roadlist.append(spotlist[0]+k-spotlist[n-1])

print(sum(roadlist)-max(roadlist))

Solution (Python):
import math

k,n = map(int,input().split())
li = list(map(int,input().split()))
cnt=k+li[0]-li[-1]

for i in range(len(li)-1):
    if li[i+1]-li[i]>=cnt:
        cnt=li[i+1]-li[i]
print(k-cnt)

Solution (Python):
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
input = sys.stdin.readline

K,N = MI()
l = LI()
max_dis = 0
for i in range(N):
    if i < N-1:
        next_dis = l[i+1]-l[i]
    else:
        next_dis = l[0]+(K-l[i])
    max_dis = max(max_dis,next_dis)
print(K-max_dis)

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

Solution (Python):
import numpy as np

k,n = map(int,input().split())
a = np.array(list(map(int,input().split())))
d = a[1:] - a[:-1]
d = np.append(d,a[0]+k-a[-1])

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

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

max1 = 0
for i in range(n-1):
    a1 = a_lst[i]
    a2 = a_lst[i+1]

    max1 = max(max1, a2-a1)

max2 = k - a_lst[-1] + a_lst[0]

max = max(max1, max2)

print(k-max)

Solution (Python):
K,N=map(int, input().split())
A=list(map(int, input().split()))
d=[0]*(N+1)
if A[0]!=0:
    tmp=A[0]
    A=list(map(lambda x: x-tmp, A))
for n in range(N+1):
    if n==0:
        d[n]=A[0]
    elif n==N:
        d[n]=K-A[n-1]
    else:
        d[n]=A[n]-A[n-1]
print(sum(d)-max(d))

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

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

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

print(K - max(B))

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

A.sort()

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

if x < A[0] + (K - A[N-1]):
    x = A[0] + (K - A[N-1])

print(K - x)

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

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

for i in range (N-1):
    
    before = A[i]
    after = A[i+1]
    diff = after - before
    if diff>=max_result:
        max_result = diff

print(K-max_result)

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

A.sort()
X = 0
for i in range(N-1):
    X_ = A[i+1] - A[i]
    if X < X_:
        X = X_

X_ = A[0] + (K - A[N-1])
if X < X_:
    X = X_

print(K - X)

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

Solution (Python):


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

total = 0
mx = 0

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

print(total - mx)

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

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

print(sum(D)-max(D))

Solution (Python):
import sys
sys.setrecursionlimit(10**6) 
import math
from copy import copy, deepcopy
from operator import itemgetter

from bisect import bisect_left, bisect, bisect_right

from collections import deque 



from collections import Counter

from itertools import accumulate

from heapq import heapify,heappop,heappush


def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds





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

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


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

print(K-max(ls))

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

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

max_span = max(span)
print(sum(span) - max_span)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
def circle_distances(A,K,N):
    ret = []
    for i in range(N):
        if i == N-1:
            d = K - abs(A[0] - A[i])
        else:
            d = A[i+1] - A[i]
        ret.append(d)
    return ret
distances = circle_distances(A,K,N)
print(K -max(distances))

Solution (Python):
K, N = [int(i) for i in input().split()]
a_list = [int(i) for i in input().split()]
behind_range = 0
max_distance = 0
a_list.append(K + a_list[0])
for a in a_list:
    distance = a - behind_range
    behind_range += distance
    if max_distance < distance:
        max_distance = distance

print(K - max_distance)

Solution (Python):
K, N = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [0]*N
total = 0
max_val = A[1]-A[0]
for n in range(N-1):
  B[n] = A[n+1]-A[n]
  total += B[n]
  if max_val<B[n]:
    max_val = B[n]
B[N-1]=A[0]+K-A[N-1]
total += B[N-1]
if max_val<B[N-1]:
  max_val = B[N-1]
print(total-max_val)

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


if __name__ == '__main__':
    main()

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

ans = 0
for i in range(0, 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()))

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

print(k-x)

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

Solution (Python):
import numpy as np

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

_a = np.append(a,a[0]+k)
_a = np.delete(_a,0)

dis = _a -a
ans = k -np.max(dis)

print(ans)

Solution (Python):
import numpy as np

N,K=map(int,input().split())
A=list(map(int,input().split()))
arr = np.array(A)
diff = np.diff(arr)

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

if max(diff) < start_to_end:
    print(sum(diff))
else:
    print(sum(diff)-max(diff)+start_to_end)

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

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

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

print(k-longest)

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

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

print(sum(cost_list) - max(cost_list))

Solution (Python):
import numpy as np

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

print(K-max(dist))

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

print(str(int(K-max(D))))

Solution (Python):
import sys

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

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



kyori = [None]*(n-1)

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

max = max(kyori)

print(k-max)

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

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

sum_l = sum(l)
sum_r = sum(r)

print(min(sum_l-max(l),sum_r-max(r)))

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

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

print(k-max(l))

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

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

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

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

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

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

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

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

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

x = a[:]
for i in a:
    x.append(i+k)

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

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

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

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

array=[]

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

print(min(array))

Solution (Python):

k, n = map(int,input().split())
ans = 1e18
arr = list(map(int,input().split()))
for i in range(n): 
    dist1=(arr[i-1]-arr[i])%k
    dist2=(arr[i]-arr[(i+1)%n])%k
    ans=min(ans,dist1,dist2)
print(ans)

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

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    K, N = read_ints()
    A = read_ints()
    distance = K-(A[0]+(K-A[N-1]))
    for i in range(N-1):
        distance = min(distance, K-(A[i+1]-A[i]))
    return distance


if __name__ == '__main__':
    print(solve())

Solution (Python):
K,N = (int(x) for x in input().split())
house = list(map(int,input().split()))

distance = []


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



print(K-max(distance))

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

s=[]
for i in range(len(a)):
    if not i==len(a)-1:
        s.append(abs(a[i+1]-a[i]))
    else:
        s.append(k-a[i]+a[0])

s.remove(max(s))
print(sum(s))

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


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


if __name__ == '__main__':
    main()

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


def main():
    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(A[0]+K-A[N-1])
    print(sum(diff)-max(diff))


if __name__ == '__main__':
    main()

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

house=list(map(int,input().split()))
longest=K-(house[N-1]-house[0])

for i in range(N-1):
    if house[i+1]-house[i]>longest:
        longest=house[i+1]-house[i]


print(K-longest)

Solution (Python):
import re
K,W = map(int,input().split())


A = list(map(int,input().split()))
a=[K-A[-1]+A[0]]
for i in range(W-1):
    l = abs(A[i]-A[i+1])
    a.append(l)

a.remove(max(a))
print(sum(a))

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

l = list()

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

ans = K - max(l)
print(ans)

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

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

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

a = [int(i) for i in a]

diff = list()
diff.insert(0,K-a[N-1]+a[0]) 
for i in range(2,N+1):       
  diff.insert(i-1,abs(a[i-1]-a[i-2]))



print (sum(diff)-max(diff))

Solution (Python):

k,n = map(int,input().split())
a = list(map(int, input().split()))
a.append(a[0])
total = 0
mx = 0
for i,item in enumerate(a):
    if(i + 1 >= len(a)):
        break
    d = a[i+1] - a[i]
    if(d < 0) :
        d = k - a[i] + a[i+1]
    total += d
    mx = max([d,mx])
print(total - mx)

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

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

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

print(sum(ans) - max(ans))

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

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

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

print(min(l_min, r_min))

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

Solution (Python):
import sys
input = sys.stdin.readline
k,n = map(int,input().split())
l = list(map(int,input().split()))
maxi = -1
for i in range(n-1):
  maxi = max(maxi,l[i+1]-l[i])
maxi = max(maxi,k-l[n-1]+l[0])
print(k-maxi)

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

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

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

Solution (Python):
k, n = map(int, input().split())
A = [int(x) for x in input().split()]
dis = [0]*n

for i in range(1, n):
    dis.append(A[i]-A[i-1])

dis.append(k-sum(dis))

print(k-max(dis))

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(A[0] + K - A[N-1])
D.sort(reverse = True)
D[0] = 0
print(sum(D))

Solution (Python):
k,n=map(int,input().split())
a = list(map(int,input().split()))
a_l = 0
a_s = 0
a_g = 0
for i in range(n):
    if i < n -1:
        a_short = min(abs(k-abs(a[i]-a[i+1])),abs(a[i]-a[i+1]))
        if a_l < a_short:
            a_l = a_short
            a_s = i
            a_g = i+1
    else:
        a_short = min(abs(k-abs(a[0]-a[n-1])),abs(a[0]-a[n-1]))
        if a_l < a_short:
            a_l = a_short
            a_s = 0
            a_g = n-1
print(max(abs(k-abs(a[a_s]-a[a_g])),abs(a[a_s]-a[a_g])))

Solution (Python):


def main():
    *N, = map(int, open(0).read().split())
    A = N[2:]
    K = N[0]
    N = N[1]

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

main()

Solution (Python):
i = list(map(int, input().split()))
k = i[0]
n = i[1]
a_list = list(map(int, input().split()))

ans = 0
for num in range(n-1):
    distance = a_list[num+1] - a_list[num]
    if distance > ans:
        ans = distance

distance_2 = k+a_list[0]-a_list[n-1]
if distance_2 > ans:
    print(k - distance_2)
else:
    print(k - ans)

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

B = sorted([(A[i+1]- A[i]) for i in range(N)])
ans = K - B[-1]
print(ans)

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

A.sort()

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

print(K - tmp)

Solution (Python):
K,N = map(int, input().split())
src = map(int, input().split())
src = list(src)
if src[-1] - src[0] <= K/2:
  print(src[-1] - src[0])
else:
  a = K - (src[-1] - src[0])
  for i in range(N-1):
    if a < src[i+1] - src[i]:
      a = src[i+1] - src[i]
  print(K-a)

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

a = sorted(a)
q = [an - ap for an, ap in zip(a[1:], a[:-1])]

nomal = max(a) - min(a)
abnomal = k - max(q)

print(min(nomal, abnomal))

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

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

Solution (Python):
from sys import stdin

k, n = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
d = max(max([abs(a[x] - a[x - 1]) for x in range(len(a)) if x > 0]), abs(a[0] + k - a[len(a)-1]))

print(k - d)

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

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

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

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

s=sum(b)
b.append(k-a[n-1]+a[0])
c =[]
for j in range(0,n):
  h=s-max(b[j+1],b[j])
  c.append(h)
print(min(c))

Solution (Python):
pro_list = input().split()
circle_distance = int(pro_list[0])
house_num = int(pro_list[1])

house_distance_list = [int(i) for i in input().split()]
max = -1
max_index = 0
for i in range(house_num):
  if i == house_num-1:
    distance_gap = (circle_distance - house_distance_list[i]) + house_distance_list[0]
  else:
    distance_gap = house_distance_list[i+1] - house_distance_list[i]
  if max < distance_gap:
    max = distance_gap
    max_index = i
print(circle_distance - max)

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

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

distance = list()

for a in range(0, N):
    if a == N - 1:
        num = K - coordinate[a] + coordinate[0]
    else:
        num = coordinate[a+1] - coordinate[a]
    distance.append(num)

print(K - max(distance))

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

Solution (Python):
k,n = map(int,raw_input().split(' '))
hs = map(int ,raw_input().split(' '))
hs.sort()
distances = [abs(u-v) for u,v in zip(hs,hs[1:])]
distances.append(k - hs[-1] + hs[0])
s = sum(distances)
print s - max(distances)

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

A = list(map(int,input().split()))
A.append(0)
A.append(K)
A = sorted(A)

S = [0]*N
S[0] = A[0]



for i in range(1,N):
    S[i-1] = A[i+1] - A[i]
S[N-1] = K-A[N]+A[1]
print(K-max(S))

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

Distance = []
for i in range(N-1):
    Distance.append(A_list[i+1]-A_list[i])
Distance.append(A_list[0]+K-A_list[N-1])
Distance.sort(reverse=True)

print(K-Distance[0])

Solution (Python):
a,b=list(map(int,input().split()))
l=list(map(int,input().split()))
maxll=0
for i in range(b-1):
    if maxll<l[i+1]-l[i]:
        maxll=l[i+1]-l[i]
if maxll<l[0]+a-l[b-1]:
    maxll=l[0]+a-l[b-1]
print(a-maxll)

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

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[0]-A[N-1])

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

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

assumption = 0
a = 0
b = 0


for i in range(N - 1):
  a += A[i +1]
 
  b += A[i]
 
  
  if assumption > A[i +1] - A[i]:
    assumption = assumption
  else:
    assumption = A[i +1] - A[i]
  

if assumption > (K - A[-1] + A[0]):
  result = a - b - assumption +  (K - A[-1] + A[0])

else:
  result = a - b 
 
print(result)

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

Solution (Python):

"""
Created on Tue Apr 28 22:05:17 2020
"""

import sys


sys.setrecursionlimit(10 ** 9)


mod = 10**9+7


K, N = 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])

ans = K - max(B)

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(sum(d)-max(d))

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

Solution (Python):
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
for i in range(a[1]):
    if i==a[1]-1:
        if b[0]==0:
            b[0]=a[0]
            c.append(abs(b[i]-b[0]))
        else:
            c.append(abs(b[i]-(b[0]+a[0])))
    elif i!=a[1]-1:
        c.append(abs(b[i]-b[i+1]))
print(a[0]-max(c))

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

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

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

D = [0] * n
for i in range(len(A)-1):
    D[i] += A[i+1]-A[i]
D[n-1] += A[0] + k - A[n-1]

D.remove(max(D))
print(sum(D))

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


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

for i in range(1, n):
    if a[i] - a[i-1] > m:
        m = a[i] - a[i-1]

ans = k - m

print(ans)

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

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(A[0] + K - A[N-1])
print(sum(sorted(ans)[:N-1]))

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[0]-a[n-1])
ans.sort()
print(sum(ans[:n-1]))

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

max = max(length)
sum1 = sum(length) 

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

sum2 = sum(length)-max 

if sum1 >= sum2:
    print(sum2)
else:
    print(sum1)

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

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

temp = K - S[N-1] + S[0]
for i in range(1, N):
    temp = max(temp, S[i]-S[i-1])

print(K-temp)

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

a = list(map(int, input().split()))
maxd = a[0] -a[n-1] + k

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

Solution (Python):

"""
Created on Tue Apr 28 22:05:17 2020
"""

import sys


sys.setrecursionlimit(10 ** 9)


mod = 10**9+7


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(A[0] + (K - A[-1]))

ans = K - max(B)

print(ans)