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

diff = [0] * N
for x in range(N):
    if x + 1 >= N:
        x -= N
    if A[x + 1] < A[x]:
        a = A[x + 1] + K
    else:
        a = A[x + 1]
    diff[x] = a - A[x]

ans = 0
temp = 0
for x in range(N - 1):
    ans += diff[x]
    temp += diff[x]

for x in range(N):
    idx = x + N - 1
    if idx >= N:
        idx -= N
        
    temp = (temp - diff[x] + diff[idx])
    if ans > temp:
        ans = temp
print(ans)

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
ans=10**9
for i in range(n):
    bef=i-1
    af=i+1
    aa=0
    bb=0
    if bef<0:
        aa=a[-1]-a[0]
    else:
        aa=k-(a[i]-a[bef])
    if n-1<af:
        bb=a[-1]-a[0]
    else:
        bb=a[i]+abs(k-a[af])
    if aa<ans:
        ans=aa
    if bb<ans:
        ans=bb
print(ans)

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

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

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

Solution (Python):
S = input().split()
K = int(S[0])
N = int(S[1])
A = []
B = []
a = input().split()
for o in range(N):
    A.append(int(a[o]))
A.sort()
for j in range(N-1):
    B.append(A[j+1] - A[j])
B.append(A[0] + K-A[N-1])
B.sort()
ans = 0
ans = K - B[N-1]

print(int(ans))

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

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


kyori_list = [j for j in range(n-1)]
for i in range(n-1):
  kyori_list[i] = n_inf[i+1]-n_inf[i]

kyori_list.append((n_inf[0])+(k-n_inf[n-1]))

kyori_list.sort(reverse=1)
print(sum(kyori_list[1:n+1]))

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

for i in range(n):
  x = abs(a[i] - a[i-1])
  if x > k/2:
    x = abs(x - k) 
  d.append(x)

print(sum(d)-max(d))

Solution (Python):
K,N=list(int(i) for i in input().split())
A=list(int(i) for i in input().split())
ans=10**20
for i in range(N):
    if i>0 and i<N-1:
        ans=min(K-(A[i]-A[i-1]),ans,K-(A[i+1]-A[i]))
    elif i==0:
        ans=min(A[-1]-A[0],ans,K-(A[1]-A[0]))
    else:
        ans=min(A[-1]-A[0],K-(A[i]-A[i-1]),ans)
print(ans)

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

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

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

print(k - dd)

Solution (Python):
import copy
K,N = (int(x) for x in input().split())
a = list(map(int,input().split()))
b = copy.copy(a)
tmp = a[N-1]
for i in range(N-1):
  a[i+1] -= a[i]
a[0] += K-tmp
d1 = sum(a)-max(a)
b = b[::-1]
tmp =b[0] 
for i in range(N-1):
  b[i] -= b[i+1] 
b[N-1] += K-tmp
d2 = sum(b)-max(b)
print(min(d1,d2))

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

for i in range(len(A)-1):
    length = A[i+1] - A[i]
    length_list.append(length)
last = K - sum(length_list)
length_list.append(last)

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

Solution (Python):
import sys
(k,n)=map(int,input().split())
a=input().split()
n-=1
j=int(a[n])-int(a[0])
l=0
for i in range(n):
  l=int(a[(n+i+1)%(n+1)])+k-int(a[i+1])
  if(j>l):
    j=l
print(j)

Solution (Python):
"""
"""


def int_as_array(num): return list(map(int, [y for y in str(num)]))


def array_as_int(arr): return int(''.join(map(str, arr)))


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


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


def array_to_string(arr, sep=' '): return sep.join(map(str, arr))


def matrix_to_string(arr, sep=' '): return '[\n' + '\n'.join(
    [sep.join(map(str, row)) for row in arr]) + '\n]'


def read(): input()


def solve(array, dist):
    total = 0
    biggest = 0
    for i, x in enumerate(array):
        if i == len(array) - 1:
            next_pos = dist + array[0]
        else:
            next_pos = array[i + 1]
        d = next_pos - array[i]
        if d > biggest:
            biggest = d
        total += d
    return total - biggest


K, N = read_array()
array = read_array()
print(solve(array, K))

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

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

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

int(K)
int(N)


B = []
for i in range(0, len(A)-1):
    B.insert(i,A[i+1]-A[i])
B.insert(int(N)-1,int(K)-A[int(N)-1]+A[0])

ans = 0

for j in range(int(N)):
    ans += B[j]

ans -= max(B)

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(15000)
k,n = map(int,input().split())
a = list(map(int,input().split()))
d = [a[i]-a[i-1] for i in range(1,n)]+[a[0]+k-a[-1]]

m = max(d)
s = sum(d)
print(s-m)

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

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

distance.remove(max(distance))
print(sum(distance))

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









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

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


ans = []
le = 0
a = list(map(int, input().split()))




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

mx = numpy.argmax(ans)

for i in range(n):
    if i != mx:
        le = le + ans[i]


print(le)

Solution (Python):
def resolve():
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))
    diff_max = 0
    A_sum = 0

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

        diff_max = max(diff_max, diff)
        A_sum += diff

    print(A_sum - diff_max)
    
if __name__ == '__main__':
    resolve()

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

K = KN[0]
N = KN[1]

kyori = []


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

print(K - max(kyori))

Solution (Python):
import numpy as np

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

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

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

print(int(K-np.max(dif)))

Solution (Python):
import sys
import numpy as np
import itertools
from collections import deque

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline


K, N = [int(i) for i in readline().split()]
A = list(map(int, readline().split()))

A.append(A[0] + K)
tmp = np.diff(np.array(A))
distance = np.append(tmp, tmp[0:N-2])



base = np.sum(distance[0:N-1])
ans = min(base, 1000000)
for i in range(N-1):
    
    base -= distance[i]
    base += distance[N-1+i]
    ans = min(base, ans)
   
print(ans)

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

k, n = map(int, input().split()) 
houses = list(input().split())
houses = [int(h) for h in houses]
houses.sort()
max_dist = 0
total = 0
for i in range(n):
    if i == n - 1:
        d = abs(k - houses[i] + houses[0])
    else:
        d = houses[i + 1] - houses[i]
    max_dist = max(max_dist, d)
    total += d

ans = total - max_dist
print(ans)

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

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

B[N-1] = K - (A[N-1] - A[0])
B_max = max(B)
print(K - B_max)

Solution (Python):
if __name__ == "__main__":
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    import numpy as np
    a = np.array(a)

    sa_kouho = a[0] + (k - a[-1])

    sa = a[1:] - a[0: -1]

    sa_kouho1 = max(sa)

    print(k - max(sa_kouho, sa_kouho1))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
cnt = 1
a = 0
B = []
if (max(A)-min(A)) <= K//2:
    print(max(A)-min(A))
else:
    for i in A:
        if i/cnt == K/N:
            a += 1
        cnt += 1
    
    if a == N:
        print(max(A)-min(A))
    else:
        for j in range(N):
            if j == N-1:
                haba = K-(A[j]-A[0])
            else:
                haba = A[j+1]-A[j]
            B.append(haba)
        print(K-max(B))

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

    num_list.append(k+num_list[0])

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

    print(k-tmp)

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
import numpy as np

Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main(): 
    k,n = map(int,input().split())
    a = list(map(int,input().split()))
    anslist = []
    a.append(k+a[0])
    for i in range(n):
        anslist.append(abs(a[i+1]-a[i]))
    print(k-max(anslist))
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(A[0] + (K - A[N-1]))


print(K - max(B))

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

A = list(map(int, input().split()))
a = A[-1] - A[0]
ans = 10000000
i = 1
for d in A:
  b = K-A[-i]+A[-i-1]
  ans = min(b, ans)
  i += 1
  if i == len(A):
    break

if a <= ans:
  print(a)
else:
  print(ans)

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

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

print(k - d_max)

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

Solution (Python):
import math
a , b = map(int,input().split())
res = list(map(int,input().split()))
res2 = []
if len(res) == 1:
    print(0)
else:
    for i in range(len(res)-1):
        aa = res[i+1] - res[i]
        res2.append(aa)
    if res[0] == 0:
        res2.append(a-res[-1])
    else:
        res2.append(res[0] + (a - res[-1]))
print(sum(res2) - max(res2))

Solution (Python):


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


longest_dist = (k - A[n-1]) + A[0]

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

print(k-longest_dist)

Solution (Python):
k,n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
maxdis = 0
pre = a[-1] - k
for dis in a:
    t = dis - pre
    pre = dis
    if t > maxdis:
        maxdis = t
print(k-maxdis)

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

for i in range(n-1):
    distances_between_houses.append(a_list[i+1]-a_list[i])

distances_between_houses.append(k-a_list[n-1]+a_list[0])

max_distance = max(distances_between_houses)
distances_between_houses.remove(max_distance)

print(sum(distances_between_houses))

Solution (Python):
import numpy as np

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

next_ls = [0] * n
for i in range(n-1):
    next_ls[i] = a_ls[i+1] - a_ls[i]
next_ls[n-1] = k - a_ls[-1] + a_ls[0]
argmax = np.argmax(next_ls)
next_ls.pop(argmax)
print(sum(next_ls))

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






if n ==2:
    f=a[0]-a[1]
    if f > k//2:
        ans =k-f
    else:
        ans=f
else:
    a.sort
    a.append(a[0]+k)

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

    ans = k-f
    
    
    
    

print(ans)

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

o=l[-1][0]
if o==len(a)-2:
    g=a[-2]-a[0]
else:
    g=a[o]+n-a[o+1]
print(g)

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

Solution (Python):
import numpy as np

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

a_list.append(k+a_list[0])

ans_list = np.diff(a_list)

print(sum(ans_list) - max(ans_list))

Solution (Python):
import sys

input = sys.stdin.readline

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

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



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

x = x + [k - sum(x)]


print(sum(x) - max(x))

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

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

answer = [0]

for index, i in enumerate(house):
  if i == house[-1]:
    X = (K - i) + house[0]
    answer.append(X)
  else:
    X = house[index + 1] - i
    answer.append(X)
print(K - max(answer))

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

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

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

Solution (Python):

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

max_dist = 0

for i in range(len(ls) - 1):
    max_dist = max(ls[i + 1] - ls[i], max_dist)

print(K - max(max_dist, K - ls[-1] + ls[0]))

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

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

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

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

a.sort()
b=[a[0]+k-a[n-1]]
ans = a[0]+k-a[n-1]

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

ans-=max(b)

print(ans)

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

ans=min(ans,K-K+abs(A[0]-A[-1]),K-abs(A[0]-A[1]))

ans=min(ans,K-abs(A[-1]-A[-2]),K-abs(K-A[-1]))

print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    k, n = na()
    mid = k // 2
    a = na()

    ma = [] 
    pa = [] 
    for i in range(n - 1):
        x = a[i+1] - a[i]
        if x < 0:
            x = k - abs(x)
        ma.append(x)
    
    for i in range(n - 1, 0, -1):
        x = a[i] - a[i - 1]
        if x < 0:
            x = k - abs(x)
        pa.append(x)

    x = a[-1] - a[0]
    if x < 0:
        x = k - abs(x)
    ma.append(x)
    x = a[0] - a[-1]
    if x < 0:
        x = k - abs(x)
    pa.append(x)
    ma.sort()
    pa.sort()
    
    print(min(sum(ma[:-1]), sum(pa[:-1])))
if __name__ == '__main__':
    main()

Solution (Python):
k,n = map(int,input().split())
a_list = list(map(int,input().split()))
result_list = []
for i in range(n):
  if i == 0:
    a_ = a_list[-1] - k
    result =  k - (a_list[0] -a_)
    result_list.append(result)
  else: 
    result =  k - (a_list[i] -a_list[i-1])
    result_list.append(result)
print(min(result_list))

Solution (Python):
DEBUG = False
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)]+[K-A[-1]+A[0]]

if DEBUG:
    print("K = %d  N = %d"%(K,N))
    print("A = \n"+str(A))
    print("Distance = \n"+str(distance))
print(sum(distance)-distance[distance.index(max(distance))])

Solution (Python):
import sys, re
import math
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7



k, n = MAP()
a = LIST()

b = []

for x in range(0, n - 1):
    b.append(a[x + 1] - a[x])

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

print(k - max(b))

Solution (Python):
K, N = map(int, input().split())
*A, = map(int, input().split())
A = A + [K+A[0]]
chk = [0] * N
for i in range(N):
    chk[i] = A[i+1] - A[i]
chk.sort(reverse=True)
print(sum(chk[1:]))

Solution (Python):
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

K, N = map(int, readline().split())
A = list(map(int, readline().split()))
A_ = [a + K for a in A]

ans = 10**9 + 7
min1, max1 = min(A), max(A)

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

    if i != N - 1:
        if A[i] - A[i + 1] > 0:
            CCW = A[i] - A[i + 1]
        else:
            CCW = A[i] + K - A[i + 1]
    else:
        CCW = A[i] - A[0]

    ans = min(ans, min(CW, CCW))

print(ans)

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


def main(k, n, a):
    distance = []
    for i in range(n):
        dist = abs(a[i] - a[i - 1])
        if dist >= k / 2:
            dist = k - dist
        distance.append(dist)

    distance.pop(distance.index(max(distance)))

    res = sum(distance)

    return res


print(main(K, N, A))

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

ans = 0
max = 0 
for i in range(N-1):
	diff = Aj[i+1] - Aj[i]
	ans += diff
	if max < diff:
		max = diff
diff = K-Aj[N-1]+Aj[0] 
ans += diff
if max < diff:
	max = diff
ans -= max
print(ans)

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

print(k-c)

Solution (Python):
import numpy

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

a.sort()

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

max_diff = max(diff)
print(k - max_diff)

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




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

if Ma < A[0]+ K-A[N-1]:
    Ma = A[0]+ K-A[N-1]


ans = K-Ma

print(ans)

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


dist_list = []

for i in range(len(a_list)-1):
    dist_list.append(a_list[i+1]-a_list[i])

dist_list.append(K+a_list[0]-a_list[-1])

max_dist = max(dist_list)

print(K-max_dist)

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

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

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

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

Solution (Python):
from collections import deque

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


d = [int(_) for _ in input().split()]

ans = k

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

print(ans)

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

Solution (Python):
inputNumber = input()
K = int(inputNumber.split(" ")[0])
N = int(inputNumber.split(" ")[1])

inputNumber = input()
A=inputNumber.split(" ")

A_Length=len(A)

for i in range(A_Length):
    A[i] = int(A[i])

result=0

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





result = K - result
print(result)

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

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

dist.append(A[0] + K - A[N-1])
dist.sort()
dist.pop(-1)


print(sum(dist))

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

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

    if distance > max_distance:
        max_distance = distance

print(K - max_distance)

Solution (Python):



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

m = an[0] + (k - an[-1])
for i in range(1, n):
    d = abs(an[i] - an[i-1])
    m = max(m, d)

print(k-m)

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = k
b = a[0] + (k-a[-1])
for i, v in enumerate(a):
    x = 0
    if i == 0:
        x = max(abs(a[i+1]-v), b)
    elif i == len(a)-1:
        x = max(b, abs(v-a[i-1]))
    else:
        x = max(abs(a[i+1]-v), abs(v-a[i-1]))
    ans = min(ans, k-x)
print(ans)

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

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

import numpy as np
a = np.array(a)

s = np.sort(a)
temae = 0
oku = 0
m = k
for i in range(n):
  if s[(i-1)%n] - s[i] < 0:
    c1 = k+s[(i-1)%n] - s[i]
  else:
    c1 = s[(i-1)%n] - s[i]
  if s[i] - s[(i+1)%n] > 0:
    c2= s[i] - s[(i+1)%n]
  else:
    c2 = k + s[i] - s[(i+1)%n]
  if m > c1:
    m = c1
  if m > c2:
    m = c2
print(m)

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

def main(K, N, lst_A):

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

    sum_diff = sum(diff_dis)
    answer = sum(diff_dis)

    for diff in diff_dis:
        _answer = sum_diff - diff
        answer  = min(answer, _answer)

    return answer

if __name__ == "__main__":

    answer = main(K, N, lst_A)
    print(answer)

Solution (Python):
from functools import reduce
import numpy

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

D = numpy.diff(A) 
D = numpy.append(D, K - sum(D))

print(K - max(D))

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])
d.pop(d.index(max(d)))
print(sum(d))

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

A = list(map(int, input().split()))
D = []

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

kankaku = max(D)
saitan = K - kankaku

print(saitan)

Solution (Python):
import numpy as np

K, N = map(int, input().split())
A = np.array(list(map(int, input().split())))
tmp = K - A[-1]

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

A[0] += tmp

A = np.sort(A)
print(np.sum(A[:-1]))

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

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

X = A[1] - A[0]
i = 1
while i < N-1:
    Y = A[i+1] - A[i]
    if X < Y:
        X = Y
    i += 1
Z = A[0] + K - A[N-1]
if X < Z:
    X = Z

print(K - X)

Solution (Python):
K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
r = 0
l = 0
ans = A[N-1] - A[0]

for i in range(len(A)):
    if A[i] != A[0]:
        r = A[i] - A[i-1]
    if A[i] != A[N-1]:
        l = A[i+1] - A[i]
    if ans > K - max(r,l):
        ans = K - max(r,l)
print(ans)

Solution (Python):
kk,nn = map(int, input().split(' '))
ll = list(map(int, input().split(' ')))
ll.append(kk)

aida = []
for j in range(len(ll)-1):
    
    if j==0: aida.append(ll[-1]-ll[-2]+ll[0])
    else: aida.append(ll[j]-ll[j-1])
print(sum(aida)-max(aida))

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

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

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

distance.sort()
longest = distance.pop()

print(K - longest)

Solution (Python):
import numpy as np

K,N=list(map(int, input().split()))
a = np.array(list(map(int, input().split())))
diff_a=np.diff(a,n=1)
max_index=np.argmax(diff_a)
temp_max=diff_a[max_index]
last_first=K-(a[-1]-a[0])
if temp_max>last_first:
  print(int(K-temp_max))
else:
  print(int(K-last_first))

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

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

A = sorted(a)

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

if maxl <= 0:
    maxl = -1 * maxl


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

    if l <= 0:
        l = -1 * l
    
    if maxl < l:
        maxl = l

ans = K - maxl

print(ans)

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

maxD=0

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

print(K-maxD)

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

tmp = [0] * n
for i in range(n):
    tmp[i] = li[i] - k
li = li + tmp
li.sort()
ans = 10**9
for i in range(len(li) - n+1):
    ans = min(ans, abs(li[i] - li[i + n-1]))
print(ans)

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

l.append(k+l[0]);
maxdiff=0
for i in range(n):
	if(l[i+1]-l[i]>maxdiff):
		maxdiff=l[i+1]-l[i]
print(k-maxdiff)

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

dis =[]

dis.append(an[0] + K - an[len(an)-1])

for i in range(N-1):
    dis.append(an[i+1]-an[i])

work = sorted(dis)
total = 0

for i in range(N-1):
    total += work[i]

print(total)

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

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

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

Solution (Python):
base = input().split()
k = int(base[0])
n = int(base[1])
house = input().split()
lenlist = []
a = int(house[0])
b = k-int(house[-1])
one = a+b
lenlist.append(one)
for i in range(n-1):
    lenlist.append(int(house[i+1])-int(house[i]))
print(k-max(lenlist))

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

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

ans = values[n-1] - values[0]

answer = [0]*n
answer[0] = ans

for i in range(1,n):
	answer[i] = ans - (values[i] - values[i-1]) + (k-values[n-1]+values[0])
	

	
print(min(answer))

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

Solution (Python):
i = list(map(int, input().split()))
K = i[0]
N = i[1]
j = list(map(int, input().split()))

d = K - (j[-1] - j[0])

for n in range(1, N):
    if j[n] - j[n-1] > d:
        d = j[n] - j[n-1]

print(K - d)