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

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    k, n = [int(s) for s in input().strip().split()]
    A = [int(s) for s in input().strip().split()]
    A.append(A[0] + k)
    maxdiff = max(b - a for a, b in zip(A, A[1:]))
    result = k - maxdiff
    print(result)




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

r=[]

for i in range(1,len(n)):
    r.append(n[i]-n[i-1])

r.append(k-n[len(n)-1]+n[0])

result=int(k-max(r))

print(result)

Solution (Python):
import numpy as np

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

dist = np.diff(loc)
dist = np.append(dist, K - loc[N - 1] + loc[0])

most_far = np.max(dist)
print(K - most_far)

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

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

max_dis = 0
sum_dis = 0
i = 0
while i < n:
  if i + 1 == n:
    dis = k - a_list[i] + a_list[0]
    
  else:
    dis = a_list[i + 1] - a_list[i]
    
  sum_dis += dis
  if max_dis < dis:
    
    max_dis = dis
  i += 1


print(sum_dis - max_dis)

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

d = [0] * n
for i in range(n-1):
    d[i] = a[i+1] - a[i]
d[n-1] = 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()))

ans=float("inf")
for i in range(N):
  g=A[i-1]
  if g>A[i]:
    ans=min(ans,g-A[i])
  else:
    ans=min(ans,K-(A[i]-g))
  g=A[(i+1)%N]
  if g>A[i]:
    ans=min(ans,K-(g-A[i]))
  else:
    ans=min(ans,A[i]-g)
    
print(ans)

Solution (Python):




import numpy as np

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

    va = np.array(A)
    vb = np.array(A[1:] + [A[0] + K])

    vv = vb - va

    print(K - max(vv))


if __name__ == '__main__':
    main()

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

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

print(K - max(A_diff))

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

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

diff = []
res = abs(a[0]-a[1])
ld = abs(k-a[-1])+abs(a[0]-0)
for i in range(1,n):
    res = max(res,abs(a[i-1]-a[i]))

res = max(res, ld)
res = k-res
print(res)

Solution (Python):

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)



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


def II():
    return int(input())

K,N=acinput()

A=acinput()
A.append(A[0]+K)
B=np.array(A)+K

a=np.array(A)

print(K-np.max(a[1:]-a[:-1]))

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

delta = [A[i+1]-A[i] for i in range(N-1)]
delta += [K-A[N-1]+A[0]]
maxd = max(delta)

print(sum(delta)-maxd)

Solution (Python):
K, N = map( int, input().split(" "))
A =[]
ss = input().split(" ")
for sss in ss:
  A.append( int(sss))

mindis = K
for i in range( N ):
  startpt = A[i]
  endpt = A[(i+N-1)%N]
  if(endpt < startpt):
    endpt += K
  if(mindis>(endpt-startpt)):
    mindis = endpt-startpt

print( mindis )

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

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

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

for i in range(1, 2*n):
    right.append(right[-1] + a[i] - a[0])

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

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=[]
for i in range(N):
  if i!=0 and i!=N-1:
    if A[i+1]-A[i]>=A[i]-A[i-1]:
      x=K-(A[i+1]-A[i])
    else:
      x=K-(A[i]-A[i-1])
  elif i==0:
      if (K-A[N-1])+A[0]>=A[1]-A[0]:
        x=A[N-1]-A[0]
      else:
        x=K+A[0]-A[1]
  else:
    if (K-A[N-1])+A[0]>=A[N-1]-A[N-2]:
      x=A[N-1]-A[0]
    else:
      x=K-(A[N-1]-A[N-2])
  l.append(x)
print(min(l))

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

dis=[]
now = 0

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

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

distance = K - max(dis)

print(distance)

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

for i in range(2, 2*n):
    P.append(P[i-2]+(A[i]-A[i-1]))

'''
i = n-1
while True:
    sm -= A[i]-A[i-1]
    sm += A[(i+1)%n]-A[i]'''
mn = 10000000000000
for i in range(n):
    if i > 0:
        mn = min(mn, P[i+n-1]-P[i])
    else:
        mn = min(mn, P[i+n-1])
print(mn)

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

ans = 10**10
for i in range(N):
    if i == 0:
        tmpa = A[N-1] - A[i]
    else:
        tmpa = K - (A[i]-A[i-1])

    if i == N-1:
        tmpb = A[i] - A[0]
    else:
        tmpb = K - (A[i+1]-A[i])

    if ans > min(tmpa, tmpb):
        ans = min(tmpa, tmpb)

print(ans)

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

Solution (Python):
def main():
    import numpy as np
    K, N = map(int, input().split())
    
    As = np.array(list(map(int, input().split())))
    

    
    new_A = As[1:]-As[:-1]
    
    a = K-As[-1]+As[0]
    print(sum(new_A) + a - max(max(new_A), a))


if __name__ == '__main__':
    main()

Solution (Python):

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

print(min(D))

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

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

ma = 0

for i in range(1,n):
    if a[i]-a[i-1] > ma:
        ma = a[i]-a[i-1]
if a[0]+k-a[-1] > ma:
    ma = a[0] + k - a[-1]
print(k-ma)

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

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

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

print(K - min_dist)

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

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


D.sort()
print(sum(D[:N-1]))

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

listA.append(K + listA[0])

max_now = 0

for i in range(N):
    kyori = abs(listA[i] - listA[i+1])
    max_now = max(max_now, kyori)

print(K - max_now)

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

longest = 0
dist = 0
for i in range(n):
    temp_dis = a_list[i+1] - a_list[i]
    if longest < temp_dis:
        longest = temp_dis
    dist += temp_dis
temp_dis_last = k - a_list[n] + a_list[0]
if longest < temp_dis_last:
    longest = temp_dis_last
dist += temp_dis_last

ans = dist - longest
print(ans)

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

for i in range(len(a)-1):
    l = a[i]-a[i+1]
    if l > max:
        max = l
print(k-max)

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

k, n, *lst = map(int, read().split())
lst.sort()
dis = k
for i in range(n):
    if i == 0:
        tmp=lst[-1] - lst[0]
    else:
        tmp=k - lst[i] + lst[i - 1]
    dis=min(dis, tmp)
print(dis)

Solution (Python):
def calc(K, N, A):
    B = [A[i + 1] - A[i] for i in range(N - 1)] + [A[0] + K - A[N - 1]]
    return K - max(B)


(K, N) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
print(calc(K, N, A))

Solution (Python):
def get_maximam_distance(k, positions):
    distance_tmp = 0
    for i in range(1, len(positions)):
        distance_tmp = max(distance_tmp, positions[i] - positions[i - 1])
    return max(distance_tmp, positions[0] + k - positions[len(positions) - 1])

params_0 = input().split()
params_1 = input().split()
param_K = int(params_0[0])
positions = list(map(lambda x: int(x), params_1))
print(param_K - get_maximam_distance(param_K, positions))

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

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

print(sum(lst) - max(lst))

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

p = []
p.append(a[-1] - a[0])
for i, _ in enumerate(a[:-1]):
    p.append(K - (a[i+1] - a[i]))
print(min(p))

Solution (Python):

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

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

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

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

print(K-x)

Solution (Python):
k, n, *a_n = map(int, open(0).read().split())

dif = [0] * n

for i in range(n - 1):
    dif[i] = a_n[i + 1] - a_n[i]

dif[n - 1] = k - a_n[n - 1] + a_n[0]

dif.pop(dif.index(max(dif)))

print(sum(dif))

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

main()

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

last = -(K - list[-1])
betw = 0

for i in list:
    b = i - last
    betw = max(betw, b)
    last = i

print(K - betw)

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

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

print(K-max(sa))

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def main():
	k , n = IN()
	al = list(IN())
	max_dist=k-al[-1]+al[0]
	for pa, pb in zip(al, al[1:]):
		max_dist = max(max_dist, pb-pa)
	print(k-max_dist)
	
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

kyori = [0] * n
for i in range(n):
    if (i != n - 1):
        kyori[i] = a[i + 1] - a[i]
    elif (i == n - 1):
        kyori[i] = k - a[n - 1] + a[0]
ans = sum(kyori) - max(kyori)
print(ans)

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

d = 0
pre = a[0]
for i in a[1:]:
    if i - pre > d:
        d = i - pre
    pre = i
if k - a[-1] + a[0] > d:
    d = k - a[-1] + a[0]
print(k - d)

Solution (Python):
k, n = (int(x) for x in input().split())
seq = sorted((int(x) for x in input().split()))
ans = float('inf')
for i in range(n):
    if seq[i] > seq[i-1]:
        d =  k - (seq[i] - seq[i-1])
    else:
        d = seq[i-1] - seq[i]
    ans = min(ans, d)
print(ans)

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

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

Solution (Python):
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): 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 LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	K,N = MI()
	A = np.array(LI())
	A_LAST = K+A[0] - A[N-1]
	B = A[1:]-A[:N-1]
	B = np.sort(B)
	print(K-max(B[N-2],A_LAST))

if __name__ == '__main__':
	main()

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

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

Solution (Python):
from itertools import chain
K, N = map(int, input().split())
S = 0
M = 0

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

prev = next(A)
for a in chain(A, [prev+K]):
  d = a - prev
  S += d
  M = max(M, d)
  prev = a

print(S-M)

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

distances = [0] * N
max_dist = 0

for i in range(N):
    if i != N-1:
        distances[i] = A_list[i+1] - A_list[i]
        max_dist = max(max_dist, distances[i])
    else:
        distances[i] = A_list[0] + K - A_list[i]
        max_dist = max(max_dist, distances[i])

print(K - max_dist)

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

def solve(K, N, A):
    
    hoge = A[0]
    B = []
    for i in range(1, N):
        B.append(A[i] - hoge)
        hoge = A[i]
    
    B.append( K - A[N-1] + A[0])
    return sum(B) - max(B)

print(solve(K, N, A))

Solution (Python):
k, n = map(int, input().split())
a = [int(i) for i in 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):
R = lambda: map(int, input().split())

k, n = R()
a = list(R())
print(k - max(y - x for x, y in zip(a, a[1:] + [a[0] + k])))

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

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

mn = 10 ** 10

for i in range(n):
    if i == 0:
        mn = min(mn, a[n - 1] - a[0])

    else:
        mn = min(mn, k - a[i] + a[i - 1])

for i in range(n):
    a[i] = k - a[i]

a.sort()

for i in range(n):
    if i == 0:
        mn = min(mn, a[n - 1] - a[0])

    else:
        mn = min(mn, k - a[i] + a[i - 1])

print(mn)

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
b=[0]*n
for i in range(n):
  b[i]=a[i]-k
b.sort()
a=b+a
s=0
e=1000000000000000000
for i in range(1,n+n):
  s+=(a[i]-a[i-1])
  if i>=n:
    s-=(a[i-n+1]-a[i-n])
  if e>s and i>=n-1:
    e=s
print(e)

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

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

a.sort()
al = {a[i+1]-a[i] for i in range(n-1)}
al.add(a[0]+k-a[n-1])

print(k-max(al))

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

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

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

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

    A.sort()

    

    min_d = 1000000
    for i in range(1, N):
        d = K - (A[i] - A[i-1])
        min_d = min(min_d, d)

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

    print(min_d)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

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

if __name__ == '__main__':
    main()

Solution (Python):
lake_m, house_num = map(int, input().split())
house_m_li = list(map(int, input().split()))

house_between_distance = list()

for i in range(house_num):
    if i == 0:
        house_between_distance.append(house_m_li[i] + (lake_m - house_m_li[-1]))
    else:
        house_between_distance.append(house_m_li[i] - house_m_li[i-1])

print(lake_m - max(house_between_distance))

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

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

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

if k - a[-1] + a[0] > max:
    max = k - a[-1] + a[0]
    max_i = len(a) - 1

print(k - max)

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

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

k,n = mi()
a = li()

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

print(k-am)

Solution (Python):

def inputlist(): return [int(j) for j in input().split()]


K,N = inputlist()
A = inputlist()
lia = [0]*N
for i in range(1,N):
    k = A[i] - A[i-1]
    lia[i] = k
lia[0] = (K+A[0]-A[-1])
lia.sort()
ans = 0
for i in range(N-1):
    ans += lia[i]
print(ans)

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

b = [0] * n

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

max_b = max(b)

print(k - max_b)

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

print(K-a)

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

B = []

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


print(K-max(B))

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

print(sum(sabun)-max(sabun))

Solution (Python):
def travel(k, n, al):
    min = 10**6+1
    for i in range(n):
        if i==0:
            if al[i-1]-al[i] > k/2.0:
                if min > al[i-1]-al[i]:
                    min = al[i-1]-al[i]
            else:
                if min > k-(al[i-1]-al[i]):
                    min = k-(al[i-1]-al[i])
        else:
            if al[i]-al[i-1] >= k/2.0:
                if min > al[i]-al[i-1]:
                    min = al[i]-al[i-1]
            else:
                if min > k-(al[i]-al[i-1]):
                    min = k-(al[i]-al[i-1])
    print(min)

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

travel(k, n ,al)

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

b = sorted(a)





l = len(b)
dmin = 9999999999
for i in range(l - 1):
    
    d = b[i] + k - b[i + 1]
    if d < dmin:
        dmin = d

if b[l - 1] - b[0] < dmin:
    dmin = b[l - 1] - b[0]
print(dmin)

Solution (Python):

import sys
input = sys.stdin.readline




'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''




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

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

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
temp = 0
ls = []
for i in range(n-1):
    temp = a[i+1]-a[i]
    ls.append(temp)
    ans += temp
ls.append((k-a[-1])+a[0])
ans += (k-a[-1])+a[0]
lsm = max(ls)
lsi = ls.index(lsm)
if lsi+1 <= k:
    ans = ans - lsm
else:
    ans = ans - lsm + a[ls.index(lsm)+1]-a[ls.index(lsm)]
print(ans)

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

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

first = 0
pre_x = None
max_num = 0 

for x in input().split():
  x = int(x)
  if pre_x != None:
    num = x - pre_x
    max_num = max(max_num,num)
  else:
    first = x
   
  pre_x = x

num = k - x + first 
max_num = max(max_num,num)
print(k-max_num)

Solution (Python):


            
def main():
    k, n = map(int, input().split())
    li = list(map(int, input().split()))
    maxv = li[0]
    for i in range(1, n):
        sub = abs(li[i] - li[i-1])
        maxv = max(maxv, sub)
    maxv = max(maxv, k + li[0] - li[n-1])
    print(k - maxv)
    
        
        
    
if __name__ == '__main__':
    main()

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

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

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

if __name__ == "__main__":
  solve()

Solution (Python):

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

Solution (Python):
import numpy as np

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

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

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

diff_list.pop(diff_list.index(max(diff_list)))
print(sum(diff_list))

Solution (Python):

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


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


An_diff_list = []
for i in range(N-1):
    tmp_diff = abs(An_list[i+1] - An_list[i])
    An_diff_list.append(tmp_diff)

tmp_diff = An_list[0] + K - An_list[N-1]
An_diff_list.append(tmp_diff)


An_max = max(An_diff_list)

print(K-An_max)

Solution (Python):
data = input().split()

k, n = int(data[0]), int(data[1])
data = input().split()
tab = []
for i in range(n):
    tab.append(int(data[i]))
tab.append(k + tab[0])
biggest = 0
for i in range(1, n+1):
    if (tab[i] - tab[i-1] > biggest):
        biggest = tab[i] - tab[i-1]


print(k - biggest)

Solution (Python):
from itertools import tee


def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


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

lengths = [abs(p - q) for p, q in pairwise(A + [A[0] + K])]
sum_length = sum(lengths)
max_length = max(lengths)

print(sum_length - max_length)

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

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


minimum = 10**8
for i in range(N):
    if minimum > (A[N - 1 + i] - A[i]): minimum = A[N - 1 + i] - A[i]
print(minimum)

Solution (Python):



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

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

dif = []

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

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

print(k - max(dif))

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

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

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

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())
import numpy as np
a = np.array(list(map(int, input().split())))
dist = np.abs(a -np.roll(a, 1))
dist[0] = min(k-a[n-1] + a[0], a[n-1]-a[0])
ans_a = np.sum(dist)
ans = ans_a
for i in range(n):
  ans_sub = ans_a-dist[i]
  if ans_sub < ans:
    ans = ans_sub
print(ans)

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

for i in range(n-1):
    d = A[i+1]-A[i]
    if d > dmax:
        dmax = d
dmax = max(k-A[-1]+A[0], dmax)

print(k-dmax)

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

dummy = list(map(int, input().split(" ")))
K = dummy[0]
N = dummy[1]
A = np.array(list(map(int, input().split(" "))))
Distance = A - np.roll(A, 1)
Distance[0] = K - sum(Distance[1:])


out = K - max(Distance)
print(out)