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

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

print(K - l)

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

Solution (Python):
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
max_dist = 0
for i in range(N):
    nxt = (i + 1 ) % N
    dist = A_ls[nxt] - A_ls[i]
    if dist < 0:
        dist += K
    max_dist = max(max_dist, dist)
print(K - max_dist)

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

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






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

ans = K -l
print(ans)

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

print(K-l)

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

l=[]

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

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

b=max(l)

print(k-b)

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

a = sorted(a)

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

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

print(k-max(d))

Solution (Python):
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

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

theta.remove(max(theta))

s=0
for j in range(len(theta)):
    s+=theta[j]

l_min=s
print(l_min)

Solution (Python):
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

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



theta.remove(max(theta))

s=0
for j in range(len(theta)):
    s+=theta[j]

l_min=s
print(l_min)

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

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

    else:
        tar2 = a[i + 1]
        tar1 = a[i]
        distance = tar2 - tar1

    diff.append(distance)


index = diff.index(max(diff))
ans = sum(diff) - max(diff)

print(ans)

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

min_dist = K
for i in range(N):
	if i == 0:
		dist = houses[-1] - houses[i]
	else:
		dist = houses[i] - houses[i-1]
		dist = K - dist
			
	if dist < min_dist:
		min_dist = dist
		
print(min_dist)

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

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

Solution (Python):

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

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

LL = max(L)

print(K - LL)

Solution (Python):

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

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

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

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


a=list(map(int,input().split()))
max=0
a.sort()

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

if max<k-abs(a[0]-a[-1]):
    max=k-abs(a[0]-a[-1])

print(k-max)

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

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

    d_lst = [0 for _ in range(n)]

    for i in range(n - 1):
        d_lst[i] = a_lst[i + 1] - a_lst[i]

    d_lst.append(a_lst[0] + k - a_lst[n - 1])

    ans = k - max(d_lst)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
k,n=[int(i) for i in input().strip().split(" ")]
arr=[int(i) for i in input().strip().split(" ")]

maxDist=arr[0]+k-arr[-1]

for i in range(n-1):
    maxDist=maxDist if arr[i+1]-arr[i] <=maxDist else arr[i+1]-arr[i]
print(k-maxDist)

Solution (Python):
k,n=[int(i) for i in input().strip().split(" ")]
arr=[int(i) for i in input().strip().split(" ")]
maxDist=arr[0]+min(k-arr[-1],arr[-1]) 

for i in range(n-1):
    maxDist=maxDist if arr[i+1]-arr[i] <=maxDist else arr[i+1]-arr[i]
print(k-maxDist)

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

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

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

Solution (Python):
import sys
from sys import exit
from math import gcd, factorial, ceil, floor, sqrt
from bisect import bisect_left, bisect_right
from copy import deepcopy
from heapq import heapify, heappop, heappush
from itertools import permutations, combinations, product, accumulate
from collections import defaultdict, deque
sys.setrecursionlimit(10**7)

ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

K, N = mi()
A = li()

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

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

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

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

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


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


print(K - max(path))

Solution (Python):
def main():
    x = list(map(int, input().split()))
    k = x[0]
    n = x[1]
    a = list(map(int, input().split()))

    max_distance = 0

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

    answer = k - max_distance
    print(answer)


if __name__ == "__main__":
    main()

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

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

for i in range(N):
    sp.append(abs(now-A[i]))
    now=A[i]

print(K-max(sp))

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

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

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

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

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

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

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

dmax = K + A[0] - A[-1]
for i in range(1,N):
    if A[i]-A[i-1] > dmax:
        dmax = A[i]-A[i-1]

print(K-dmax)

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

Solution (Python):
def main():
    k, n = map(int, input().split())
    A = [int(x) for x in input().split()]
    
    minv = A[n-1] - A[0]
    
    for i in range(n-1):
        minv = min(minv, k - (A[i+1] - A[i]))
    print(minv)

if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
A=list(map(int,input().split()))
S=[0 for _ in range(k)]

for i in range(1,k+1):
    if i!=k:
        S[i-1]=A[i]-A[i-1]
    else:
        S[i-1]=A[0]+n-A[k-1]

print(n-max(S))

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

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

print(k - max_len)

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

A_list = list(map(int, input().split()))
A_gap_max = 0
ans = 0

for i in range(N-1):
    A_gap_temp = A_list[i+1] - A_list[i]
    if A_gap_temp > A_gap_max:
        A_gap_max = A_gap_temp
    ans += A_gap_temp 
A_gap_temp = A_list[0] + K - A_list[N-1]
if A_gap_temp > A_gap_max:
    A_gap_max = A_gap_temp
ans += A_gap_temp 
    
ans -= A_gap_max
print(ans)

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

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

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

for i in range(N-1):
  d = abs(A[i] - A[i+1])
  if c < d:
    c = d
  else:
    c = c
    
print(K-c)

Solution (Python):
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    dists = set([])
    for idx in range(len(A) - 1):
        dists.add(A[idx + 1] - A[idx])
    dists.add(A[0] + K - A[-1])
    print(K - max(list(dists)))


if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
a=sys.stdin.buffer.read
K,N,*A=map(int,a().split())
A+=[A[0]+K]
print(K-max(y-x for x,y in zip(A,A[1:])))

Solution (Python):
import sys
 
read = sys.stdin.buffer.read
K,N,*A = map(int, read().split())
A += [A[0]+K]
gap = max(y-x for x,y in zip(A, A[1:]))
 
print(K-gap)

Solution (Python):
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
A += [A[0] + k]
far = max(y - x for x, y in zip(A, A[1:]))
print(k - far)

Solution (Python):
import sys


def main():
    read = sys.stdin.buffer.read

    k, n, *A = map(int, read().split())
    A += [A[0] + k]
    far = max(y - x for x, y in zip(A, A[1:]))
    print(k - far)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


def main():
    read = sys.stdin.buffer.read
    k, n, *A = map(int, read().split())
    A += [k + A[0]]
    far = max(x - y for x, y in zip(A[1:], A))
    print(k - far)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


def main():
    read = sys.stdin.buffer.read
    k, n, *A = map(int, read().split())
    far = k + A[0] - A[-1]
    y = A[0]
    for x in A[1:]:
        dis = x - y
        if far < dis:
            far = dis
        y = x
    print(k - far)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
far = k + A[0] - A[-1]
y = A[0]
for x in A[1:]:
    dis = x - y
    if far < dis:
        far = dis
    y = x
print(k - far)

Solution (Python):
import sys
from array import array
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
A += [k + A[0]]
far = max(array("l", [x - y for x, y in zip(A[1:], A)]))
print(k-far)

Solution (Python):
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
far = k + A[0] - A[-1]
y = A[0]
for x in A:
    if far < x - y:
        far = x - y
    y = x
print(k-far)

Solution (Python):
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
far = k + A[0] - A[-1]
for x, y in zip(A[1:], A):
    if far < x - y:
        far = x - y
print(k-far)

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

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

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

print(k - max(l))

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

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

print(k - max(l))

Solution (Python):
import sys
read = sys.stdin.buffer.read
k, n, *A = read().split()
far = int(k) + int(A[0]) - int(A[-1])
for x, y in zip(A[1:], A):
    if far < int(x) - int(y):
        far = int(x) - int(y)
print(int(k)-far)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
k, n = readline().rsplit()
A = readline().rsplit()
far = int(k) + int(A[0]) - int(A[-1])
for x, y in zip(A[1:], A):
    if far < int(x) - int(y):
        far = int(x) - int(y)
print(int(k)-far)

Solution (Python):
k, n, *A = map(int, open(0).read().split())
far = k + A[0] - A[-1]
for x, y in zip(A[1:], A):
    if far < x - y:
        far = x - y
print(k-far)

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

farthest = (k-A[-1])+A[0]
for i in range(len(A)-1):
    farthest = max(farthest, A[i+1]-A[i])
print(k-farthest)

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

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

print (K - max(B))

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

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

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

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

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

Solution (Python):
def resolve():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    maxdiff = 0
    idx = None
    for i in range(N):
        if i == N-1:
            diff = (K+A[0]) - A[i]
        else:
            diff = A[i+1] - A[i]
        if maxdiff < diff:
            idx = i
            maxdiff = max(maxdiff, diff)
    print(K - maxdiff)

if '__main__' == __name__:
    resolve()

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

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

print(ans)

Solution (Python):

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

if __name__=='__main__':
  main()

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


import numpy as np

def main():
	k,n=MI()
	A=LI()
	A=np.array(A)
	A=np.append(A,A+k)

	B=A[n-1:]-A[:n+1]
	print(min(B))
















if __name__ == "__main__":
	main()

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

print(K-max(a))

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

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

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

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

dis.sort
print(sum(dis) - max(dis))

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

max_range = 0

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

ans = K - max_range
print(ans)

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

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math












import sys
sys.setrecursionlimit(10000000)


mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math












import sys
sys.setrecursionlimit(10000000)


mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
k,n = readInts()
A = sorted(readInts())
A.append(A[0])
long = 0
for i in range(n):
    if i != n-1:
        long = max(long,abs(A[i+1] - A[i]))
    else:
        long = max(long, abs((k-A[i]) + A[i+1]))
print(k - long)

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math












import sys
sys.setrecursionlimit(10000000)


mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
k,n = readInts()
A = readInts()
max_dist = 0
for i in range(n):
    next = (i+1)%n
    dist = A[next] - A[i]
    if dist < 0:
        dist += k
    max_dist = max(max_dist, dist)
print(k - max_dist)

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(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()))
D = []
for i in range(n-1):
    D.append(A[i+1]-A[i])
else:
    D.append(k-(A[n-1]-A[0]))
D.sort(reverse=True)
print(k-D[0])

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

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

result = []

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

result.sort()

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

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

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

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

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

Solution (Python):
K, N, *A = map(int, open(0).read().split())
A = [A[-1] - K] + A

max_gap = 0
for a1, a2 in zip(A, A[1:]):
    max_gap = max(max_gap, a2 - a1)
print(K - max_gap)

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

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

print(k-d)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

K, N = MI()
A = LI()
A_dif = []

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

print(K - max(A_dif))

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

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

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

Solution (Python):








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

a.append(a[0]+k)

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

print(k-l)

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

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

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

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

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

Solution (Python):
k,n = map(int, input().split())
arr = list(map(int, input().split()))
prev = 0
m = 0
for i in arr:
    m = max(m, abs(prev-i))
    prev = i

m = max(m, k+arr[0]-arr[n-1])
ans = k-m
print(ans)

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

Solution (Python):
import math
K,N = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(N):
    a.append(a[i] + K)
minimum = math.inf 
for i in range(N):
    minimum = min(minimum,a[i+N-1]-a[i])
print(minimum)

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

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

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

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

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

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

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

dif = [A[i+1] - A[i] for i in range(N-1)]

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

print(K - max(dif))

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = a[0]+(k-a[-1])
for i in range(n-1):
    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()))
aa = a + a

for j in range(n,2*n):
    aa[j] = aa[j] + k

dis = k

for i in range((2*n)-1):
    tonari = aa[i+1]-aa[i]
    dis = min(dis,k-tonari)

print(dis)

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

A.sort()

dist = []

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

print(K - max(dist))

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

    max_dist = 0
    for i in range(1, N):
        dist_tmp = A[i] - A[i-1]
        if(dist_tmp > max_dist):
            max_dist = dist_tmp
    dist_over_zero = K - (A[N-1] - A[0])
    if(dist_over_zero > max_dist):
        ans = K - dist_over_zero
    else:
        ans = K - max_dist
    print(ans)


if __name__ == "__main__":
    solve()

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

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

Solution (Python):


def main():
    k, n = map(int, input().split())
    A_array = list(map(int, input().split()))
    A_array.sort()
    
    
    
    ans = float("inf")
    for i in range(n):
        if i == 0:
            d_r = A_array[-1] - A_array[0]
            d_l = k - (A_array[1] - A_array[0])
            d = min(d_r, d_l)
        elif i == n - 1:
            d_r = k - (A_array[-1] - A_array[-2])
            d_l = A_array[-1] - A_array[0]
            d = min(d_r, d_l)
        else:
            d_r = k - (A_array[i] - A_array[i-1])
            d_l = A_array[i] + (k - A_array[i+1])
            d = min(d_l, d_r)
        
        ans = min(ans, d)
    print(ans)
main()

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

b.sort()
b[-1]=0
print(sum(b))

Solution (Python):

def main():
    K, N = map(int, input().split())
    A = [int(x) for x in input().split()]

    max_length = A[0] + K - A[-1]
    for i in range(1, len(A)):
        max_length = max(max_length, A[i] - A[i - 1])
    print(K - max_length)


if __name__ == '__main__':
    main()

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

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

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

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

D = [A[i + 1] - A[i] if i < len(A) - 1 else K - A[i] + A[0] for i in range(len(A))]
D.sort()
print(K - D[-1])

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

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

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

if len(A) == 2:
    dist = A[1] - A[0]
    if 2 * dist > K:
        dist = K - dist

minDist = float("inf")
for i in range(N-1):
    if K - (A[i+1] - A[i]) < minDist:
        minDist = K - (A[i+1] - A[i])

minDist = min(minDist, A[N-1]-A[0])

print(minDist)