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 = tuple(map(int, input().split()))

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

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

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


max_ = [0, K+A[0]-A[-1]]


for i in range(1, N):
    tmp = A[i] - A[i-1]
    if tmp > max_[1]:
        max_[0] = i
        max_[1] = tmp
print(K-max_[1])

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

n_1 = k - a[-1] + a[0]

l = [] 

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

print(k-max(l))

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

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

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

Solution (Python):
def solve():
    K,N = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    ans = max_delta = 0
    for i in range(N):
        if i == N-1:
            delta = K - A[i] + A[0]
        else:
            delta = A[i+1] - A[i]
        ans += delta
        max_delta = max(delta, max_delta)
    else:
        ans -= max_delta
    print(ans)

if __name__ == "__main__":
    solve()

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

import numpy as np

a = np.array(A)
D = np.diff(a)
D = np.append(D, a[0] + K - a[-1])

print(K - max(D))

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

import numpy as np

a = np.array(A)
D = np.diff(a)
D = np.append(D, a[0] + K - a[-1])
ind = np.argmax(D)

if N == 2:
    print(a[ind+1]-a[ind])
elif ind == len(a)-1:
    ans =  -a[0] + a[ind]
    print(ans)
else:
    ans = K - (a[ind+1]-a[ind])
    print(ans)

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

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

Solution (Python):
K, N = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = 0
del_A = []
for i in range(N):
  if(i==(N-1)):
    del_A.append(A[0]+K-A[i])
  else:
    del_A.append(A[i+1]-A[i])
print(sum(del_A)-max(del_A))

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

dl.append(k - l[n-1] + l[0])
print(k - max(dl))

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):
a,b=map(int,input().split())
L=list(map(int,input().split()))
rest=L[0]+a-L[-1]
for i in range(b-1):
  U=L[i+1]-L[i]
  rest=max(rest,U)
print(a-rest)

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

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

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

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

B=[0]*N

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

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

B=sorted(B)

ans = sum(B[0:-1])
print(ans)

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

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

max_kyori = 0
a.append(k + a[0])

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

print(k - max_kyori)

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

max_l = 0
for i in range(n):
  if i != n - 1:
    l = lst[i + 1] - lst[i]
  else:
    l = k + lst[0] - lst[i]
  if l > max_l:
    max_l = l
print(k - max_l)

Solution (Python):

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

A[N+1:] = [A[0] + K]

distance_list = []
for i in range(N):
    distance = abs(A[i] - A[i+1])
    distance_list.append(distance)
distance_list.remove(max(distance_list))
print(sum(distance_list))

Solution (Python):

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

A[N+1:] = [A[0] + K]

distance_list = []
for i in range(N):
    distance = abs(A[i] - A[i+1])
    distance_list.append(distance)
distance_list.remove(max(distance_list))
print(sum(distance_list))

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

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

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

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

for i in range(1, len(A)):
    m = A[i] - A[i - 1]
    mm = max(mm, m)

print(K - mm)

Solution (Python):
K,N = map(int,input().split())
cc = list(map(int,input().split()))
min_length = max(cc)-min(cc)
for i in range(1,N):
  Length = K-cc[i]+cc[i-1]
  min_length = min(min_length,Length)
print(min_length)

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

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

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

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

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

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

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

print(sum(ans))

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

l = 0
for i in range(n):
    l = max(l, A[i+1]-A[i])
ans = k - l
print(ans)

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

array=input().split()
A=[]
for i in array:
    A.append(int(i))

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

dist=sorted(dist)

ans=0
for i in range(N-1):
    ans+=dist[i]

print(ans)

Solution (Python):
import math


def resolve():
    import sys
    input = sys.stdin.readline
    
    kn = [int(x) for x in input().rstrip().split(" ")]
    k = kn[0]
    a = [int(x) for x in input().rstrip().split(" ")]

    max_dist = max([a[i+1] - a[i] for i in range(len(a)-1)])
    max_dist = max_dist if max_dist > a[0] + k - a[len(a)-1] else a[0] + k - a[len(a)-1]

    print(k - max_dist)


if __name__ == "__main__":
    resolve()

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

B=[]
t=0
for a in A:
    B.append(a-t)
    t=a
B[0] +=k-t

print(k-max(B) )

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

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

Solution (Python):
score = list(map(int,input().split()))
kenl = list(map(int,input().split()))

syoki = 0
syoki = kenl[0]+(score[0]-kenl[score[1]-1])
length = [0]*score[1]
length[0] = syoki
for i in range(score[1]-1):
    length[i+1] = kenl[i+1]-kenl[i]

length.sort()
answer = length[score[1]-1]
print(score[0]-answer)

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=[]
p = A[-1]-K
for a in A:
  l.append(a-p)
  p=a
print(sum(l)-max(l))

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

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

Solution (Python):

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

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

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

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

Solution (Python):
k,n=map(int,input().split())
h=list(map(int,input().split()))
l=[]
for i in range(n-1):
  l.append(h[i+1]-h[i])
l.append(h[0]+k-h[n-1])
ll=sorted(l,key=int,reverse=True)
print(k-ll[0])

Solution (Python):








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





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

print(K - max(interbals))

Solution (Python):


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

print(K-max(B))

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

Solution (Python):

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

K,N = inputlist()
A = inputlist()
dis = [0]*(N)
for i in range(N):
    if i == N-1:
        dis[i] = K+A[0] - A[i]
        continue
    dis[i] = A[i+1] - A[i]
dis.sort()
print(sum(dis[:-1]))

Solution (Python):
line1 = input()
line2 = input()

aryLine1 = line1.split()
arystrLine2 = line2.split()

K = int(aryLine1[0]);
N = int(aryLine1[1]);

aryLine2 = [int(s) for s in arystrLine2]


aryLine2.sort()


ans = K

for i in range(0, N):
    temp1 = int(aryLine2[i])
    if i == N - 1:
        temp2 = int(aryLine2[0]) + K
    else:
        temp2 = int(aryLine2[i + 1])

    temp = temp1 - temp2 + K

    if temp < ans:
        ans = temp

print(ans)

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

ans = 10**6 + 1
for i in range(n):
    ans = min(ans, a[-1] - a[i])
    a.append(a[i]+k)
print(ans)

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

ans = 10**6 + 1
for i in range(n):
    ans = min(ans, a[-1] - a[i])
    a.append(a[i]+k)
print(ans)

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

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

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

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

k,n = inpm()
a = inpl()

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

print(k - d)

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

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

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

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

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

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

Solution (Python):




import sys
input=sys.stdin.readline

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

if __name__ == '__main__':
    main()

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

if __name__ == '__main__':
    main()

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

def main(k,n,a):
    dmax=0
    for i in range(n-1):
        dmax=max(dmax,a[i+1]-a[i])
    dmax=max(dmax,k+a[0]-a[n-1])
    return k-dmax

if __name__=='__main__':
    k,n,a=readinput()
    ans=main(k,n,a)
    print(ans)

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

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

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

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

diff_list = []
for i in range(N - 1):
    current = A[i]
    next = A[i + 1]
    diff_list.append(next - current)

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

max_index = diff_list.index(max(diff_list))
del diff_list[max_index]

print(sum(diff_list))

Solution (Python):
K,N=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
ls_sub=[ls[i]-ls[i-1] for i in range(N)]
ls_sub[0]+=K
print(K-max(ls_sub))

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

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

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

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

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

L = [] 

for n in range(1, N):
  L.append(A[n]-A[n-1])

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

ans = K - max(L)

print(ans)

Solution (Python):
from sys import stdin

k,n = [int(x) for x in stdin.readline().strip().split()]
a_lst = [int(x) for x in stdin.readline().strip().split()]
diff_lst = [a_lst[i+1] - a_lst[i] for i in range(len(a_lst)-1)] + [k - a_lst[-1] + a_lst[0]]

print(k - max(diff_lst))

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

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

b = sorted(b)

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

Solution (Python):
import sys
import itertools
k, n = map(int, sys.stdin.buffer.readline().split())
a = list(map(int, sys.stdin.buffer.readline().split()))
a.append(a[0] + k)
b, c = itertools.tee(a)
next(c)
ans = k - max(y - x for x, y in zip(b, c))
print(ans)

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

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

b = sorted(b)
print(sum(b[:-1]))

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

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

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

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

print(K-max(dis))

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

ans = K-B[-1]
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

K, N = mapint()
As = list(mapint())

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

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

Solution (Python):
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    k, n = map(int, readline().rstrip().split())
    A = sorted(list(map(int, readline().rstrip().split())))
    maxA = k + A[0] - A[n-1]
    for i in range(n-1):
        a = abs(A[i+1] - A[i])
        maxA = max(maxA, a)
    print(k - maxA)


if __name__ == '__main__':
    main()

Solution (Python):
def distance_between_houses(d1, d2=0):
    """
    2点の基準値からの距離を引数に2点間の距離を求める。
    引数：d1 : 家１の基準値からの距離
    　　　d2 : 家２の基準値からの距離
    戻り値：2点間の距離
    """
    return d2 - d1

def traveling_salesman_around_lake():
    
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    
    distances = list()
    for i in range(N):
        if i < N-1 :
            distances.append(distance_between_houses(A[i], A[i+1]))
        
        else:
            distances.append(distance_between_houses(A[i], A[0]+K))
    
    max_value_ind = distances.index(max(distances))
    distances.pop(max_value_ind)
    
    return sum(distances)

result = traveling_salesman_around_lake()
print(result)

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


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


print(K-max(dists))

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

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

Solution (Python):














































global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    
    K, N = list(map(int, lines[0].split()))
    values = list(map(int, lines[1].split()))
    
    
    
    
    
    
    

    distances = list()
    distances.append(K+values[0] - values[N-1])
    for n in range(N-1):
        distances.append(values[n+1] - values[n])

    result = K - max(distances)

    return [result]



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['20 3', '5 10 15']
        lines_export = [10]
    if pattern == 2:
        lines_input = ['20 3', '0 5 15']
        lines_export = [10]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

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

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


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

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

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

  print(min(sum(SR)-max(SR), sum(SL)-max(SL)))


if(__name__ == '__main__'):
  main()

Solution (Python):


from typing import *
import collections
import functools
import itertools
import math
import sys

INF = float('inf')


def solve(K: int, N: int, A: "List[int]"):
    return sum(sorted([(i == 0)*K+A[i]-A[i-1] for i in range(N)])[:-1])


def main():

    sys.setrecursionlimit(10 ** 6)

    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)]  
    print(f'{solve(K, N, A)}')


if __name__ == '__main__':
    main()

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

A = A + [K]

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


print(min(sum(lis[:N-1]), K - max(lis)))

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

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

Solution (Python):
from sys import stdin
import math
from functools import reduce

k,n = [int(x) for x in stdin.readline().rstrip().split()]

a = [int(x) for x in stdin.readline().rstrip().split()]

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

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

print(k-l)

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

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

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

ans = sum(diff) - max(diff)
print(ans)

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

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

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

dt=[lis[i+1]-lis[i] for i in range(N-1)]+[lis[0]+(K-lis[-1])]
print(K-max(dt))

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

max_dist = 0

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

max_dist = max(max_dist, K-A[-1]+A[0])

print(K-max_dist)

Solution (Python):

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

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

d_max = max(d)

print(K - d_max)

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

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


l = (k - A[-1]) + A[0]
if max_len < l:
  max_len = l

print(k - max_len)

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

max_diff = 0
dsum = 0
for i in range(len(A)):
    if i < len(A)-1:
        diff = A[i+1] - A[i]
    else:
        diff = K - A[i] + A[0]
    dsum += diff
    if diff > max_diff:
        max_diff = diff

ans = dsum - max_diff
print(ans)

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

Solution (Python):


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

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

print(K - ans)

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

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from functools import reduce, lru_cache
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy

sys.setrecursionlimit(1000000)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline().rstrip())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline().rstrip())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    K, N = LI()
    A = LI()

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

    print(K - res[-1])

if __name__ == '__main__':
    main()

Solution (Python):
kn=input("").split(" ")
k=int(kn[0])
n=int(kn[1])
aa=input("").split(" ")
lista=[]
for i in aa:
    lista+=[int(i)]
for i in range(n):
    lista[i]%=k
lista.sort()

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

Solution (Python):
import numpy as np

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

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

dif = np.diff(a, n=1)
maximum = max(dif.max(), a[0]+(k-a[-1]))

print(k-maximum)

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

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

d = [a1 - a2 for (a1, a2) in zip(A[1:], A[:-1])]
d.append(A[0] + K - A[-1])

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

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

diff.append(A[0]+k-A[-1])
print(sum(diff)-max(diff))

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

for i in range(N - 1):
    x.append(A[i + 1] - A[i])
x.append(A[0] + K - A[N - 1])
x = sorted(x)
del x[N - 1]
print(sum(x))

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

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

b = []

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

print(k-max(b))

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

print(min(A[-1]-A[0],temp))

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

print(k-max(ans,c))

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

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

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

Solution (Python):

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

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

ku = a[0] + (k - a[-1])
maxku = max(ku, maxku)
cnt += ku

print(cnt - maxku)