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()))
a1 = sorted(a)
temp = list()
x = len(a)-1
for i in range(x):
    temp.append(a1[i+1]-a1[i])
temp.append(min(max(a)-min(a),k-max(a)+min(a)))
ans = k-max(temp)
print(ans)

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

a=list(map(int,input().split()))
a.sort()
a.append(k+a[0])

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

print(k-max(b))

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


dists = []
for i,start in enumerate(A):
  
  posR = A[(i - 1) % N] + (K if A[(i - 1) % N] < start else 0)
  
  
  posL = A[(i + 1) % N] - (K if A[(i + 1) % N] > start else 0)
  

  dists.append(min(posR - start, - posL + start))

print(min(dists))

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    K,N=MI()
    A=LI()
    S=sum(A)
    L=[]
    for i in range(N-1):
        temp=A[i+1]-A[i]
        L.append(temp)
        
    L.append(A[0]+K-A[-1])
    M=max(L)
    
    print(K-M)

main()

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

A3 = A + [K+a for a in A]
dist = [A3[i+N-1] - A[i] for i in range(N)]

print(min(dist))

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

A2 = [K+a for a in A]
A3 = A + A2

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

print(min(dist))

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

max_d = 0

for a in range(1, N):
    d = A[a] - A[a-1]
    if d > max_d:
        max_d = d
last_d = K - A[-1] + A[0]
if last_d > max_d:
    max_d = last_d
print(K - max_d)

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

Solution (Python):
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

k, n = LI()
a = LI()








far = []

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

    far.append(d)

longest = 0

for i in range(n):
    if far[i] > longest:
        longest = far[i]
        longest_i = i
del far[longest_i]
print(sum(far))

Solution (Python):



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

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

print(K - max(dist))

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

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

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

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

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

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

D = [0] * (N)

A2 = [A1[i]+K for i in range(len(A1)-1)]
A1 = A1+A2

for i in range(N):
    D[i] = A1[i+N-1] - A1[i]
    
print(min(D))

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

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

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

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

for i in range(len(A) - 1):
    dis = A[i + 1] - A[i]
    if dis > mx:
        mx = dis

print(K - mx)

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    ans = A[N-1] - A[0]
    for i in range(1, N):
        if ans > (A[i-1] + K- A[i]):
            ans = A[i-1] + K - A[i]
    print(ans)

    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):
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())
import sys
sys.setrecursionlimit(10**6)

c,n=sep()
ar=lis()
ar.sort()
m=-10
for i in range(n):
    t=min(abs(ar[i]-ar[((i-1) + n)%n]),c-abs(ar[i]-ar[((i-1) + n)%n]))
    
    if t>m:
        m=t
print(c-m)

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

Solution (Python):

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

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

print(k - max(b))

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

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

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

print(k-m)

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

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

B = []

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

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

b_max       = max(B)
b_max_index = B.index(max(B))


B.remove(b_max)
print(sum(B))

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[-1] + a[0])

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

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

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

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

for i in root:
    ans += i

ans -= max(root)
print(ans)

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[-1]+a[0])

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

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

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

Solution (Python):








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





kyori=[]
for i in range(len(f)-1):
    an=f[i+1]-f[i]
    kyori.append(an)
    
an2=A-f[-1]+f[0]
ans=max(max(kyori),an2)
print(A-ans)

Solution (Python):
import numpy as np
K, N = map(int, input().split())
A = np.array(list(map(int, input().split())))
diff = np.append(A[1:] - A[:-1], K + A[0] - A[-1])
print(np.delete(diff, np.argmax(diff)).sum())

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

bef = a[0]
max_diff = 0
for i in range(1,n):
    diff = abs(bef - a[i])
    max_diff = max(diff, max_diff)
    bef = a[i]

print(k - max(max_diff, k - abs(a[n-1] - a[0])))

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

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

d.sort()
d[n-1] = 0

print(sum(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.sort()
print(sum(d[:-1]))

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

ans = 10 ** 6 + 1
for i in range(n):
  r_idx = ((i+1)%n)
  r_ans = a[i]+(k-a[r_idx]) if a[i] < a[r_idx] else abs(a[r_idx]-a[i])
  l_idx = (i-1+n)%n
  l_ans = (k-a[i])+a[l_idx] if a[i] > a[l_idx] else a[l_idx]-a[i]

  ans = min([ans, r_ans, l_ans])

print(ans)

Solution (Python):
K, N = map(int, input().split())
Alst = list(map(int, input().split()))
zero = Alst[0] + K
M = 0
now = Alst[0]
for i in Alst:
    dis = i - now
    if dis > M:
        M = dis
    now = i

last = zero - now
if last > M:
    M = last

print(K - M)

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

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

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

d1 = (k - A[n-1]) + A[0]
d = max(d, d1)
print(k-d)

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

for i in range(n-1):
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)

print(k-max(longest, k-a_list[n-1]+a_list[0]))

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

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

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

print(k-longest)

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

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

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

print(ans)

Solution (Python):
k, n = map(int,input().split())
a = list(map(int,input().split()))
new_a = sorted(a)
num = len(new_a) - 1
s_a = []
b = (k - new_a[num]) + new_a[0]
s_a.append(b)
cun = 0
for i in new_a[1::]:
    s = i - new_a[cun]
    s_a.append(s)
    cun += 1
max_num = max(s_a)
print(sum(s_a) - max_num)

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

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

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[N-1]=K-A[N-1]+A[0]
print(K-max(B))

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

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

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

print(K-max(sa))

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

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

    li.append(abs(A[0] + (K - A[-1])))
    print(sum(li) - max(li))
except:
    pass

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

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

Solution (Python):
import numpy as np

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

a_array.append(k + a_array[0])

a_diff = np.diff(a_array)
ans = np.sum(a_diff) - np.max(a_diff)
print(ans)

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

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

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

ans = 10000000
for i in range(n-1):
    ans = min(k-(a[i+1]-a[i]), ans)
ans = min(ans,a[n-1]-a[0])
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(A[0] + K - A[N-1])

max_d = max(d)

print(K - max_d)

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

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

a.sort()
a.append(a[0]+k)

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

print(k - ans)

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



A = sorted(A)
L = []
ANS = 0

A_max = max(A)

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

L = sorted(L)


L.pop(-1)

for i in range(len(L)):
    ANS = ANS + L[i]
    
print(ANS)

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

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

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

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

Solution (Python):


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

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

    print(K - max(tmp1, tmp2))


if __name__ == "__main__":
    main()

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

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

for i in range(len(a)-1):
    if i == 0:
        maxDis = a[i]+k-a[i-1]
    elif abs(a[i]-a[i-1]) > maxDis:
        maxDis = abs(a[i]-a[i-1])
    if abs(a[i]-a[i+1]) > maxDis:
        maxDis = abs(a[i]-a[i+1])

print(k-maxDis)

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

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

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

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

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

print(K - max_distance)

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

dlist=[]

for i in range(1,N):
    d=A[i]-A[i-1]
    dlist.append(d)

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

print(K-max(dlist))

Solution (Python):

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

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

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

print(sum(li) - max(li))

Solution (Python):
import copy
a = [int(c) for c in input().split()]
K = a[0]
N = a[1]
A = [int(c) for c in input().split()]
B = list(map(lambda x: x+K,copy.deepcopy(A)))
A = A+B
l = 0
tmp = 0
cnt = 0
for i in range(N):
    tmp = A[i+1]-A[i]
    if tmp > l :
        cnt = i
        l = tmp

print(K-l)

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

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

Solution (Python):
def main():
    K, N = tuple([int(_x) for _x in input().split()])
    A = [int(_x) for _x in input().split()]
    maxdiff = 0
    for i in range(1, len(A)):
        maxdiff = max(maxdiff, A[i] - A[i-1])
    maxdiff = max(maxdiff, K + A[0] - A[-1])
    print(K-maxdiff)


main()

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

check = 0
for i in range(n):
  x = lst[i + 1] - lst[i]
  if (x > check):
    check = x

print(k - check)

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

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

last = a[0] + k - a[n - 1]
sum = 0
l =[]

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

l.append(last)

print(k - max(l))

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

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

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

Solution (Python):
k,n = map(int,input().split()) 
A=list(map(int,input().split()) )
ans=10**10
for i in range(len(A)):
    
    
    
    if A[i-1] - A[i] > 0:    
        dist1  = A[i-1] - A[i]    
    
    
    else:
        dist1 = A[i-1] + (k-A[i])
    
        
    
    
    if A[i] - A[i-1] < 0:    
        dist2  = A[i-1] - A[i]    
    
    
    else:
        
        dist2 = A[i] + (k-A[i-1])
        
    dist = min(dist1,dist2)
    
    ans = min(ans,dist)

print(ans)

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

Solution (Python):
k, n = map(int,input().split())
a_l = list(map(int, input().split()))
d = [ abs(a_l[i+1] - a_l[i]) for i in range(n-1) ]
d += [ k-a_l[-1]+a_l[0] ]
print(sum(sorted(d, reverse=True)[1:]))

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

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

print(K-t)

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

print(min((k-m),abs(a[n-1]-a[0])))

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

Solution (Python):
K,N = map(int,input().split())
a = list(map(int,input().split()))
b = K - a[N - 1] + a[0]
d = 10**20
for i in range(N):
  if i == 0:
    if b < a[i + 1] - a[i]:
      c = K - (a[i + 1] - a[i])
    else:
      c = K - b
  if i == N - 1:
    if a[i] - a [i - 1] < b:
      c = K - b
    else:
      c = K - (a[i] - a[i - 1])
  else:
    if a[i] - a[i - 1] < a[i + 1] - a[i]:
      c = K - (a[i + 1] - a[i])
    if a[i] - a[i - 1] < a[i + 1] - a[i]:
      c = K - (a[i + 1] - a[i])
  if d > c:
    d = c
print(d)

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

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

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

dist = 0
ng = 0
for i in range(n-1):
    sub = A[i+1] - A[i]
    if sub > ng:
        ng = sub
    dist += sub

sub = A[0] + (k - A[-1])
if sub > ng:
    ng = sub
dist += sub

print(dist - ng)

Solution (Python):
import sys

K, N = map(int,input().split())
A = list(map(int,input().split()))
L = [0]*(2*N)
MIN = 1e18

for i in range(0,N):
	L[i] = A[i]
for i in range(N,2*N):
	L[i] = A[i-N]+K
for i in range(N-1,2*N):
	MIN = min(MIN,L[i]-L[i-(N-1)])

print(MIN)

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

Solution (Python):

import sys
input=sys.stdin.readline

def main():
    houses=[]
    k,n=map(int,input().split())
    houses=list(map(int,input().split()))
    distance=[]
    
    for i in range(1,n):
        distance.append(houses[i]-houses[i-1])
    distance.append(k-(houses[-1]-houses[0]))

    max_length=max(distance)    
    print(sum(distance)-max_length)

if __name__=="__main__":
    main()

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


if __name__ == '__main__':
    main()

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

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


length.append(K-sum(length))

max_l = length[0]
for j in range(N):
  if max_l <= length[j]:
    max_l = length[j]
  else:
    pass


length.remove(max_l)
result = sum(length)
print(result)

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

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

c = a[0] + (k - a[n - 1])

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

c = max(c,d)

print(k - c)

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

c = a[0] + (k - a[n-1])

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

print(k-c)

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

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

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

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

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

Solution (Python):

def main():
    k, n = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    d =[min(a)+k-max(a)]
    for i in range(n-1):
        d.append(a[i+1]-a[i])
    print(k-max(d))
    

if __name__ == "__main__":
    main()

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

print(sum(sorted(diff)[:-1]))

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

Ai = list(map(int,input().split()))
kyori = []

for x in range(n-1):
  kyori.append(Ai[x+1]-Ai[x])
  
kyori.append(Ai[0]+k-Ai[n-1])

kyori.remove(max(kyori))

print(sum(kyori))

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

before = 0
max_n = 0
for num in nums:
  dist = num - before
  max_n = max(max_n, dist)
  before = num
max_n = max(max_n, K-nums[-1]+nums[0])
print(K-max_n)

Solution (Python):

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

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = K
for i in range(N):
    start = A[i]
    end = K+A[i-1] if A[i-1] < A[i] else A[i-1]
    path = end-start
    ans = min(path, ans)

print(ans)

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

print(k-m)

Solution (Python):


k,n = map(int,input().split())
a = list(map(int,input().split()))
bet = [a[0]+k-a[n-1]]

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

print(k-max(bet))

Solution (Python):
k,n = map(int, input().split())
a = [int(x.strip()) for x in input().split()]
min_dist = k
for i in range(n):
  if i != 0 and i != n-1:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] + (k - a[i+1])
    if min_dist > min(dist_r,dist_l):
      min_dist = min(dist_r,dist_l)
  elif i == 0:
    dist_r = a[n-1] - a[i]
    dist_l = a[i] + (k - a[i+1])
    min_dist0 = min(dist_r,dist_l)
  else:
    dist_r = a[i-1] + (k - a[i])
    dist_l = a[i] - a[0]
    min_distn = min(dist_r,dist_l)
ans = min(min_dist,min_dist0,min_distn)
print(ans)

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

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

Solution (Python):
k, n = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
max_l = a[0] + k - a[-1]
for i in range(n-1):
  if a[i+1] - a[i] >= max_l:
    max_l = a[i+1] - a[i]
print(k - max_l)

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

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

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

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

Solution (Python):
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

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

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

print(k-max_l)

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

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

ans = []

diss = []
suma = 0

for i in range(len(a)-1):
    dis = a[i+1] - a[i]
    diss.append(dis)


diss.append(k-a[-1]+a[0])
diss.pop(diss.index(max(diss)))
print(sum(diss))

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

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

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

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

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

A.append(k+A[0])

road = [A[i+1]-A[i] for i in range(n)]

print(sum(road)-max(road))

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

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

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


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

print(sum - max)

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

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


print(sum-max)

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

max_dist = K - A[N-1] + A[0]
s = 0
for i in range(0,N-1):
  if max_dist < A[i+1] - A[i]:
    max_dist = A[i+1] - A[i]
    s = i + 1

B = list(map(lambda x: int(copysign(x+K, x)), A[:s]))
A = A[s:] + B

ans = 0
for j in range(N):
  if j == 0:
    continue
  ans += A[j] - A[j-1]
  
print(ans)

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

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