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 numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))

A = np.array(sorted(A))

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

print(np.sum(A_div)-np.max(A_div))

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

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

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

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

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

d = h_list[0] + k - h_list[n - 1]
for i in range(n - 1):
    d = max(d, h_list[i + 1] - h_list[i])
out = k - d
print(out)

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

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

two_loops = As + [A+K for A in As]

max_dist = 0
max_dist_index = 0
for i in range(len(two_loops)-1):
    new_dist = two_loops[i+1]-two_loops[i]
    if new_dist > max_dist:
        max_dist = new_dist
        max_dist_index = i+1
print(two_loops[max_dist_index+N-1]-two_loops[max_dist_index])

Solution (Python):
pond, house = map(int, input().split())
li = list(map(int, input().split()))

li.append(pond+li[0])
maxl=0

for i in range(len(li)-1):
    maxl=max(maxl, li[i+1]-li[i])
print(pond-maxl)

Solution (Python):
input_line = input()
Line = input_line.split()
Line = [int(s) for s in Line]

K,N = Line[0],Line[1]



input_line = input()
Line = input_line.split()
Line = [int(s) for s in Line]



sinLine = [y - x for (x, y) in zip(Line[0:], Line[1:])]

sinLine.append(Line[0]+K-Line[-1])

print(sum(sinLine)-max(sinLine))

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

Solution (Python):
import numpy as np

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

a[-1] += a[0]
diff = np.diff(a)
print(np.sum(diff) - np.max(diff))

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

Solution (Python):
import sys 

K, N = [int(i) for i in sys.stdin.readline().strip().split(" ")]
A = [int(i) for i in sys.stdin.readline().strip().split(" ")]

s = 0 
m = -1
a = []
for i in range(0, N-1):
    n = A[i+1] - A[i]
    m = max(m, n)
    s += n

n = K-A[-1] + A[0]
m = max(m, n)
s += n

print(s - m)

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

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

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

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


print(K-max)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 10 ** 9
mx = max(A)
mi = min(A)
tmp = mx - mi
ans = tmp
for i in range(N):
    A.append(A[i]+K)



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

print(ans)

Solution (Python):

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


mn=min(al)
mx=max(al)
dis=[]
for i in range(n-1):
    dis.append(al[i+1]-al[i])
dis.append(mn+(k-mx))

dis.sort(reverse=False)
ans=0
for i in range(n-1):
    ans+=dis[i]
print(ans)

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

B = [None] * N


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

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

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

def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    a += [k + a[0]]

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

    for i in range(ind+1, n):
        yield a[i+1] - a[i]

    for i in range(0, ind):
        yield a[i+1] - a[i]


print(sum(main()))

Solution (Python):
import sys
import os
from io import BytesIO, IOBase






'''def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r'''


'''def divs(n, start=1):
    divisors = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors'''


'''def divn(n, primes):
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t
    return divs_number'''


'''def flin(d, x, default=-1):
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return (default, default)
    else:
        return (left, right)'''


'''def modpow(b,p,m):
    res=1
    b=b%m
    while(p):
        if p&1:
            res=(res*b)%m
        b=(b*b)%m
        p>>=1
    return res'''


'''def mod_inv(a,m):
    return modpow(a,m-2,m)'''


'''def ncr(n,r,m):
    res=1
    if r==0:
        return 1
    if n-r<r:
        r=n-r
    p,k=1,1
    while(r):
        res=((res%m)*(((n%m)*mod_inv(r,m))%m))%m
        n-=1
        r-=1
    return res'''


'''def ncrlis(n,r,m,fact):
    return (fact[n]*(mod_inv(fact[r],m)*mod_inv(fact[n-r],m))%m)%m'''


def xor1_n(n):
    d={0:n,1:1,2:n+1,3:0}
    return d[n&3]

def ceil(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def ddd(): return collections.defaultdict(collections.defaultdict(int))
 


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

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













for _ in range(1):
    k,n=mi()
    a=li()
    mx=k-a[-1]+a[0]
    res=k-a[-1]+a[0]
    for i in range(n-1):
        res+=a[i+1]-a[i]
        mx=max(mx,a[i+1]-a[i])
    print(res-mx)

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

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

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

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

print(K-maxdist)

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

buf_1 = 0
buf_2 = 0
for i in range(1,N) :
    buf_1 = max(buf_1,A[i] - A[i-1])

buf_2 = max(buf_1,A[0] + K - A[N-1])

if buf_2 > buf_1 :
    print(A[N-1] - A[0])
else :
    print(K - buf_1)

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

print(K-np.max(np.diff(A)))

Solution (Python):
import numpy as np
def main():
    k, n = map(int, input().split(" "))
    As = list(map(int, input().split(" ")))

    As = np.array(As)

    As_dists = As[1:] - As[:-1]
    
    As_dists = np.append(As_dists, As[0] + (k - As[-1]))
    max_dists = np.max(As_dists)
    
    

    ans = k - max_dists
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
import numpy as np

def solve(K: int, N: int, A: "List[int]"):

    a = np.array(A)
    p = a[1:] - a[:-1]
    q = K - a[-1] + a[0]
    print(K -max(np.max(p),q))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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

longest = 0

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

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

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

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

print(K-max(B))

Solution (Python):
from sys import stdin,setrecursionlimit
k,n = map(int,stdin.readline().rstrip().split())
a = list(map(int,stdin.readline().rstrip().split()))
b = []
for i in range(n-1):
    b.append(a[i+1]-a[i])
b = [0]+b
c = b.index(max(b))
print(min(a[-1]-a[0],a[c-1]+k-a[c]))

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

inter = 0


for i in range(n-1):
    inter = max(inter, a[i+1]-a[i])
    if inter == int(k/2):
        break

inter = max(inter, a[0]+k-a[n-1])

print(k-inter)

Solution (Python):
def LI():
	return [ int(s) for s in input().split() ]

K,N = LI()
A = LI()

A_diff = [0]*N
A_diff[0] = A[0] + K-A[N-1]

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

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

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

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

  tmp = max(tmp,a[i+1]-a[i])

print(k - tmp)

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

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

print(K-max(d))

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

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

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

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

print(K-max(L))

Solution (Python):
[k,n] = [int(x) for x in input().split()]
data = [int(x) for x in input().split()]
data.append(data[0]+k) 

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

target_distance = k - max(kankaku_list)

print(target_distance)

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(A[0] + k-A[n-1])
print(k - max(L))

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

k,n = mi()
a=np.array(li())

b = a[1:] - a[:n-1]
b_max = np.max(b)
c = a[0] + k-a[n-1]

print(k-max(b_max,c))

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

print(K - max(sa))

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

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

print(tmp)

Solution (Python):
KNlist = list(map(int, input().split()))
Alist = list(map(int, input().split()))
K = KNlist[0]
N = KNlist[1]
northMiddleS = K - (Alist[-1]-Alist[0])
distances = []
for i in range(N-1):
    difference = Alist[i+1]-Alist[i]
    distances.append(difference)
distances.append(northMiddleS)
maxDistanceS = max(distances)
shortest = K-maxDistanceS
print(shortest)

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


i_max=0
d_max=0

for i in range(N):
    im1=(i-1)%N
    d=(As[i]-As[im1])%K
    if d > d_max:
        d_max=d
        i_max=i


ans=(As[(i_max-1)%N]-As[i_max])%K
print(ans)

Solution (Python):
n= [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
lake = n[0]
num_ie = n[1]
kyori = []
for i in range(num_ie-1):
    kyori.append(l[i+1]-l[i])
kyori.append(lake - l[num_ie-1] + l[0])
output = 0
output += sum(kyori) 
output -= max(kyori)

print(output)

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

tmp = []
for i in range(len(A)-1):
    tmp.append(abs(A[i]-A[i+1]))
tmp.append(K-A[-1]+A[0])
print(sum(tmp) - max(tmp))

Solution (Python):
def main():
    import sys
    from collections import defaultdict
    K,N=list(map(int,sys.stdin.readline().split()))
    AL=list(map(int,sys.stdin.readline().split()))
    diff=[AL[i]-AL[i-1] for i in range(1,N)]
    diff.append(K-AL[-1]+AL[0])
    print(K-max(diff))


main()

Solution (Python):
circle_meter, N = [int(i) for i in input().split()]
house = [int(i) for i in input().split()]

max_distance = 0

for i in range(N):
	if i == N-1:
		if max_distance < (house[0] + circle_meter - house[i]):
			max_distance = house[0] + circle_meter - house[i]
		else:
			break
	elif max_distance < (house[i+1] -house[i]):
		max_distance = (house[i+1]-house[i])
	
result = circle_meter - max_distance
print(result)

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

between_distances = []
for i, a in enumerate(A):
    if i == 0: continue
    old_a = A[i-1]
    between_distances.append(a - old_a)
between_distances.append(K - A[-1] + A[0])

print(sum(between_distances) - max(between_distances))

Solution (Python):
kn = input('').split(' ')
k = int(kn[0])
n = int(kn[1])
a = input('').split(' ')

d = []
d.append(k - int(a[n-1])+int(a[0]))
xd = d[0]
for i in range(1,n):
  d.append(int(a[i]) - int(a[i-1]))
  if xd < d[i]:
    xd = d[i]
print(k - xd)

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

distance = [A[i+1]-A[i] for i in range(len(A)-1)]

max_distance = max(distance)




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


max_distance = max(max_distance,candidate)



ans = K - max_distance
print(ans)

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

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

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

m = 0
for i in range(N-1):
    l = A[i+1] - A[i]
    if m <= l:
        m = l
        
if m <= K-A[N-1]+A[0]:
    m = K-A[N-1]+A[0]
    
print(K-m)

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

max_dist = 0
for i in range(len(A)):
    if i != len(A) - 1:
        dist = A[i + 1] - A[i]
    else:
        dist = K - A[i] + A[0]
    if dist > max_dist:
        max_dist = dist
print(K - max_dist)

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

max_dis = 0
for i in range(n):
    if i < n - 1:
        dis = abs(a[i] - a[(i+1)%n])
    else:
        dis = a[0] + k - a[i]
    if max_dis < dis:
        max_dis = dis
print(k - max_dis)

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

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

max = 0

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

print(K-max)

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

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

print(res - max_num)

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

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

Solution (Python):
import sys

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

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

diff_max=A[0]+K-A[N-1]
imax=N
for i in range(N-1):
    d=A[i+1]-A[i]
    if d>diff_max:
        diff_max=d
        imax=i

print(K-diff_max)

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

x = c[n-1]-c[0]
if x < answer:
  answer = x

y = c[0]+k-c[1]
if y < answer:
  answer = y

z = k-c[n-1]+c[n-2]
if z < answer:
  answer = z

w = c[n-1] - c[0]
if w < answer:
  answer = w

for i in range(1,n-1):
  a = c[i-1]+k-c[i]
  b = c[i]+k-c[i+1]
  if a < answer:
    answer = a
  elif b < answer:
    answer = b

print(answer)

Solution (Python):
K, N, *A = map(int, open(0).read().split())

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

Solution (Python):



from sys import stdin


def main():
    K, N = list(map( int, stdin.readline().split() ))
    A = list(map(int, stdin.readline().split()))
    B = []
    B.append( A[0] + (K - A[-1]) )
    for i in range(N - 1):
        B.append(A[i + 1] - A[i])

    furthest = max(B)
    print(K - furthest)


if __name__ == "__main__":
    main()

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

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

Solution (Python):
import numpy as np
K,N=map(int, input().split())  
A=np.array(input().split(),np.int64)
A_diff = np.diff(A, n=1)
A_diff = np.insert(A_diff, 0, A[0]+(K-A[-1]))

ans = np.sum(A_diff)-np.max(A_diff)
print(ans)

Solution (Python):
num = input().split(' ')
num_data = [int(num[i]) for i in range(2)]
data = input().split(' ')
data_m = [int(data[i]) for i in range(num_data[1])]
data_m.append(num_data[0] + data_m[0])
key = data_m[0]
locate = 0
for i in range(num_data[1]):
    if(key < data_m[i + 1] - data_m[i]):
        key = data_m[i + 1] - data_m[i]
        locate = i + 1
print(num_data[0] - key)

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

Solution (Python):
import math
k,n=map(int,input().split())
arr=list(map(int,input().split()))
right_dis=k-arr[-1]
ans=1000000000000000
ans=min(ans,k-right_dis-arr[0])

for i in range(1,len(arr)):
	dis=k-abs(arr[i]-arr[i-1])
	
	ans=min(ans,dis)
print(ans)

Solution (Python):

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

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

A_K = [A[x]-K for x in range(len(A))] 
newA = A_K + A

kyorist = [newA[x+len(A)-1] - newA[x] for x in range(len(A))]

print(min(kyorist))

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

if N == 2:
    ans = AList[1] - AList[0]
else:
    lengths = [AList[-1] - AList[0]]
    for i in range(1, N):
        lengths.append(K - AList[i] + AList[i-1])
    ans = min(lengths)
print(ans)

Solution (Python):

from itertools import permutations, combinations,combinations_with_replacement,product
s = input().split()
q = [int(p) for p in s]

K = q[0]
N = q[1]

t = input().split()
r = [int(p) for p in t]





dist = []

def calc_dist(a, b):
    if(a - b < 0):
        return a - b + K
    else:
        return a - b

for i in range(N):
    dist.append(calc_dist(r[i], r[i-1]))


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

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

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

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


print(k-amax)

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

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

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

print(sum(distance) - max(distance))

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

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

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

A = [0]*n

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

A_dif = [0]*n

for i in range(n):
    a_bak = i-1
    a_f = i+1
    if(i == 0):
        a_bak = n-1
    if(i == n-1):
        a_f = 0
    
    
    A_dif[i] 
    tmp = max(abs(A[a_f]-A[i]),abs(A[a_bak]-A[i]))
    
    if(tmp > k//2):
        tmp = k - tmp
    
    A_dif[i] = tmp
    


print(k - max(A_dif))

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

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

print(sum(dis) - max(dis))

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




max_distance = K

for start_house_index in range(N) :
    if (start_house_index == 0) :
        
        d = A_list[N-1] - A_list[0]
    elif (start_house_index == N-1) :
        
        d = (K - A_list[N-1]) + A_list[N-2]
    else :
        
        d = (K - A_list[start_house_index]) + A_list[start_house_index-1]
        
    
    
    if (d < max_distance) :
        max_distance = d

print(max_distance)

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

dp = [0]*N
dp[0] = K-As[N-1]+As[0]

for i in range(1,N):
    dp[i] = max(dp[i-1],As[i]-As[i-1])

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

Solution (Python):

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

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

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

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

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

for i in range(N):
	right = 0
	left  = 0
	if i == 0:
		left  = inList[0]+(K - inList[1])
		right = inList[N-1] - inList[0]
	elif i == N-1:
		left  = (K - inList[i]) + inList[i - 1]
		right = inList[i] + (K - inList[0])
	else:
		left  = (K - inList[i]) + inList[i - 1]
		right = inList[i] + (K - inList[i + 1])

	if(left < right):
		distList.append(left)
	else:
		distList.append(right)

distList.sort()
print(distList[0])

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

kyori=[0]*n

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

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

print(k-max(kyori))

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

dis = A + int(Tree[0]) - int(Tree[-1])

for i in range(N - 1):
    dis = max(dis, int(Tree[i + 1]) - int(Tree[i]))

print(A - dis)

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

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

print(min(B))

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

Solution (Python):
import sys


k, n = map(int, sys.stdin.readline().split())
homes = list(map(int, sys.stdin.readline().split()))
M = -1
for i in range(n-1):
    if homes[i+1] - homes[i] > M:
        M = homes[i+1] - homes[i]
if homes[0] + k - homes[-1] > M:
    M = homes[0] + k - homes[-1]

print(k - M)

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

ans = k
max_dis = 0

for i in range(n-1):
    s = li[i+1] - li[i]
    if max_dis < s:
        max_dis = s

tmp = li[0] + (k - li[-1])

max_dis = max(tmp, max_dis)

print(k - max_dis)

Solution (Python):

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

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

total  = 0
maxx = -99999999999

for i in range(0, line[1]-1):
    total = total + (l[i+1] - l[i])
    if maxx < (l[i+1] - l[i]):
        maxx = (l[i+1] - l[i])

yeet = (l[0] + (line[0]-l[line[1]-1]))
total = total + yeet

if maxx < yeet:
        maxx = yeet


print(total - maxx)

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

Solution (Python):

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

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

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

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


print(K-maxlen)

Solution (Python):
import bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())

K,N = LI()
A = LI()
ans = float('INF')
_A = []
for x in A:
    _A.append(x-K)
allA = _A+A
for i in range(N*2-(N-1)):
    ans = min(ans,allA[i+N-1]-allA[i])
print(ans)

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

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

print(k - max(diff))

Solution (Python):
def solve():
    K, N = map(int,input().split())
    An = list(map(int,input().split()))
    dsum = 0
    dmax = 0

    for i in range(N):
        if i == 0:
            d = An[i] + K - An[N-1]
        else:
            d = An[i] - An[i-1]
        if d > dmax:
            dmax = d
        dsum += d
    print(dsum-dmax)

solve()

Solution (Python):
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
dist = []
dist.append(N - A[-1] + A[0])
ans = 10**18
for i in range(0,K-1):
    dist.append(A[i+1]-A[i])

s = sum(dist)
for i in dist:
    ans = min(ans,s-i)
print(ans)