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 = [int(i) for i in input().rstrip().split()]
A = [int(i) for i in input().rstrip().split()]
A.sort()
maxD = [A[0] + (K - A[-1]), 0]
for i, a in enumerate(A):
    if i == 0:
        continue
    D = A[i] - A[i-1]
    if D > maxD[0]:
        maxD = [D,i]
print(K - maxD[0])

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

A = input().split()

maxv = abs(int(A[0]) + K - int(A[N-1]))

for i in range(N):
    try:
        if abs(int(A[i]) - int(A[i+1])) > maxv:
            maxv = abs(int(A[i]) - int(A[i+1]))
    except IndexError:
        break

print(K-maxv)

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

Solution (Python):
import sys
import collections
sys.setrecursionlimit(10**6)

class gv:
    K = 0
    N = 0
    A = []
    max_interval = 0

def build():
    gv.K, gv.N = map(int, sys.stdin.readline().split(' '))
    *gv.A, = map(int, sys.stdin.readline().split())
    for i in range(1,gv.N):
        gv.max_interval = max(gv.max_interval, gv.A[i] - gv.A[i-1])

def solve():
    ans = 0
    ans = min(  gv.A[gv.N -1] - gv.A[0], 
                gv.K - gv.max_interval)
    
    print(ans)

if __name__ == '__main__':
    build()
    solve()

Solution (Python):

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

print(sum(Ad2)-max(Ad2))

Solution (Python):
from sys import stdin
import math
import fractions

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

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

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

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

ans=k-m
print(ans)

Solution (Python):



k, n = map(int, input().split())
A = list(map(int, input().split()))
AA = sorted(A)
AA.append(AA[0]+k)
dif_AA = []

for a in range(len(AA)-1) : 
    tmp = AA[a+1] - AA[a]
    dif_AA.append(tmp)
    
dif_max = dif_AA.index(max(dif_AA))
ans = AA[dif_max] - AA[0] + AA[-1] - AA[dif_max+1]

print(ans)

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

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

res = k - m
print(res)

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

print(K-a)

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

import math

m = 0

for i in range(len(lis)):
    a = abs(lis[i] - lis[i-1])
    if K-a <= a:
        a = K-a
    if a >= m:
        m = a

print(K-m)

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

if __name__ == "__main__":
    main()

Solution (Python):

K, N = map(int, input().split())
A_list = list(map(int, input().split()))
q = K - (A_list[N-1] - A_list[0])
for i in range(N-1):
    p = A_list[i+1] - A_list[i]
    if p > q:
        q = p
print(K - q)

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

Solution (Python):
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

k,n=n1()
a=n1()

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

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

Solution (Python):
def resolve():
    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))
resolve()

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

biggestDist = 0
for i in range(len(a)):
    if i == len(a)-1:
        aDist = a[0]-(a[i]-K)
        biggestDist = max(aDist, biggestDist)
    else:
        dist = a[i+1]-a[i]
        biggestDist = max(dist, biggestDist)
print(K-biggestDist)

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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

    res = INF
    for i in range(N):
        if i == N-1:
            res = min(res, A[N-1] - A[0])
        else:
            res = min(res, K - (A[i+1] - A[i]))
    
    print(res)


if __name__ == '__main__':
    main()

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

distance = []
for i in range(len(a)-1):
    d = abs(a[i+1]-a[i])
    distance.append(d)
a = sum(distance)
distance.append(k-a)
print(sum(distance)-max(distance))

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

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

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

Solution (Python):
import sys
i = []
for line in sys.stdin.readlines():
  i.append(line.rstrip().split(" "))

list_i1 = [int(s) for s in i[1]]

a = 0
distance = []
for list_i1[a] in list_i1:
  if a > len(list_i1)-2:
    break
  distance.append(list_i1[a+1] - list_i1[a]) 
  a += 1
c = 0
for b in distance:
  c += b
distance.append(int(i[0][0]) - c)

print(int(i[0][0]) - max(distance))

Solution (Python):

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


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

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

print(K-max_val)

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

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

print(K-max(d))

Solution (Python):

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

ans = 0
max = 0

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

print(ans)

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

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

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

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

Solution (Python):
import numpy as np

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

K = a[0]
N = a[1]

c = np.zeros(N)

for i in range(N-1):
  c[i] = abs(b[i+1] - b[i])
  
c[N-1] = abs(K - b[N-1] + b[0])

r = max(c)

print(int(K-r))

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

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

print(k-d)

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

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

Solution (Python):


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

for i in range(n):
    a.append(a[i]+k)


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


print(min(lis))

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

for i in range(n):
    if i == 0:
        len.append(min(k-(a[1]-a[0]), a[n-1]-a[0]))
    elif i == n-1:
        len.append(min(a[n-1]-a[0], k-(a[n-1]-a[n-2])))
    else:
        len.append(min(k-(a[i]-a[i-1]), k-(a[i+1]-a[i])))

print(min(len))

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

print(K-max(x))

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

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

def get_dis(distance_list):
    longest = 0
    sum = 0
    for num in distance_list:
        sum += num
        if num > longest:
            longest = num
    return (sum-longest)

def cover(k, n, position_list):
    distance_list = []
    for i in range(len(position_list)-1):
        distance = position_list[i+1] - position_list[i]
        distance_list.append(distance)
    
    distance_list.append(position_list[0]+k-position_list[-1])
    ans = get_dis(distance_list)
    return ans

print(cover(k, n, position_list))

Solution (Python):


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

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

print(K - max_distance)

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

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

    max_dist = 0
    A.append(K + A[0])

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

    print(K - max_dist)


if __name__ == '__main__':
    main()

Solution (Python):

k,n = map(int, input().split());
A = list(map(int, input().split()))
B = [0 for i in range(len(A))]
for i in range(len(A)):
    if i == 0:
        B[i] = k-A[len(A)-1]+A[i]
    else:
        B[i] = A[i]-A[i-1]
print(k-max(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(A[0]+K-A[N-1])
print(K-max(B))

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

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

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

maxdist = 0
for i in range(N - 1):
  dist = dist_list[i + 1] - dist_list[i]
  maxdist = max(maxdist, dist)
else:
  dist = (dist_list[0] + K) - dist_list[-1]
  maxdist = max(maxdist, dist)

print(K - maxdist)

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

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

maxdist = 0
for i in range(n - 1):
    dist = A[i+1]-A[i]
    maxdist = max(maxdist, dist)
    
else:
    dist = A[0] + k - A[n-1]
    maxdist = max(maxdist, dist)
    

ans = k - maxdist

print(ans)

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

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

a = K-A[N-1]

sub.append(a+A[0])
ans = K - max(sub)

print(ans)

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

max_dif = max(dif_houses)
print(n-max_dif)

Solution (Python):
a,b= map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
ar.append(a+ar[0])
count = 0
mx = 0
for i in range(len(ar)-1):
    count += ar[i+1] - ar[i]
    if mx < ar[i+1] - ar[i]:
        mx = ar[i+1] - ar[i]
print(count - mx)

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

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

for i in range(n):
    a.append(a[i]+k)

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

print(k-maxi)

Solution (Python):
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
i = 1
max_diff = 0
while i < n:
    if a[i] - a[i - 1] > max_diff:
        max_diff = a[i] - a[i - 1]
    i += 1
if k - a[n - 1] + a[0]> max_diff:
    max_diff = k - a[n - 1] + a[0]
print(k - max_diff)

Solution (Python):
import numpy as np

K,N = map(int, input().split())
An_list = np.array(input().split(), dtype=int)

An_delta_list = np.diff(An_list)
An_delta_list = np.append(An_delta_list, (K - An_list[-1])+An_list[0])
An_delta_max = np.max(An_delta_list)
result = np.sum(An_delta_list) - An_delta_max
print(result)

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

Solution (Python):

K_N = list(map(int, input().split()))
A_line = list(map(int, input().split()))


length = []
for i in range(K_N[1] - 1):
  length.append(A_line[i + 1] - A_line[i])
else:
  length.append(K_N[0] - sum(length))

min_length = K_N[0] - max(length)
print(min_length)

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

e = []
max = 0

for i in range(n-1) :
  e.append(houses[i + 1] - houses[i])
e.append(perimeter - houses[n-1] + houses[0])

for i in range(n):
  if e[i] > max:
    max = e[i]

print(perimeter - max)

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

Solution (Python):
import heapq
def main():
    k,n = map(int,input().split())
    a_n = map(int,input().split())
    l_1 = []
    l_2 = []
    for a_i in a_n:
        heapq.heappush(l_1,a_i)
        heapq.heappush(l_2,a_i)
    a_1 = heapq.heappop(l_2)
    heapq.heappush(l_2,a_1 + k)
    dif_m = 0
    for _ in range(n):
        start = heapq.heappop(l_1)
        goal = heapq.heappop(l_2)
        dif = goal - start
        if dif_m < dif:
            dif_m = dif
    print(k - dif_m)
                


if __name__ == "__main__":
    main()

Solution (Python):
kn=input().split()
k=int(kn[0])
n=int(kn[1])
disfn=[int(i) for i in input().split()]
dismax=disfn[0]+k-disfn[-1]
for i in range(n-1):
	if dismax<disfn[i+1]-disfn[i]:
		dismax=disfn[i+1]-disfn[i]
print(k-dismax)

Solution (Python):
from sys import exit, stdin
K, N = [int(_) for _ in stdin.readline().rstrip().split()]
A = [int(_) for _ in stdin.readline().rstrip().split()]
distances = []
for i in range(N-1):
    distances.append(A[i+1]-A[i])
distances.append(A[0]+K-A[N-1])
print(K-max(distances))

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

max_diff = 0

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

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):
import numpy as np



first_input = list(map(lambda x:int(x), input().split(" ")))
second_input = list(map(lambda x:int(x), input().split(" ")))








K = first_input[0]
N= first_input[1]

A_list = second_input

A_list.append(K+A_list[0])

dist_list = np.array(A_list)[1:]-np.array(A_list)[:-1]

print(sum(dist_list)-max(dist_list))

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

K = a[0]
N = a[1]

A_nplus1 = K + A[0]
A.append(A_nplus1)

max = -1
max_id = -1

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

Solution (Python):
k,n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = []
for i in range(0,n):
  if i < n-1:
    b.append(a[i+1]-a[i])
  else:
    b.append(k + a[0] - a[n-1])

print(k - max(b))

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

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


max = 0
for x, y in zip(a, a[1:]):
    if max < y-x:
        max = y-x

print(k-max)

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

print(
  n - \
    max(
      [a_2 - a_1 \
          for a_1, a_2 in \
          zip(
            a_list[:-1], 
            a_list[1:]
          )
      ] + \
      [n - a_list[-1] + a_list[0]]
    )
)

Solution (Python):
l, n = (int(i) for i in input().split());
a = [int(i) for i in input().split()];
mx = 0;
for i in range(1, n):
    mx = max(mx, a[i] - a[i - 1]);

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

print(l - mx);

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

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

Solution (Python):
arrayNM = list(map(int, input().split()))
array = list(map(int, input().split()))
route = [array[0] + arrayNM[0] - array[arrayNM[1] - 1]]
for i in range(1,arrayNM[1]):
  route.append(array[i] - array[i-1])
  
Max = max(route)
maxIndex = route.index(Max)
distance = 0

for i in range(0,maxIndex):
  distance += route[i]

for i in range(maxIndex + 1, arrayNM[1]):
  distance += route[i]

print(distance)

Solution (Python):
def main():
    K, N = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))

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

    print(sum(dist) - max(dist))


if __name__ == '__main__':
    main()

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

Solution (Python):
def main():
    K, N = map(int, input().split())
    A = list(map(int,input().split()))
    ans = K
    for i in range(0,N,1):
        s = A[i]
        if i == 0:
            g = A[N-1]
            ans = min(ans, g-s)
        else:
            g = A[i-1]
            ans = min(ans, K-(s-g))

    print(ans)

main()

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(1,N):
  mx=max(mx,A[i]-A[i-1])
print(K-mx)

Solution (Python):
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(abs(a[i+1]-a[i]))
print(str(k-max(b)))

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

for i in range(n-1):
    d.append(a[i+1]-a[i])
d.sort()
print(sum(d[0:n-1]))

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]=k-a[i]+a[0]
	else:
		b[i]=a[i+1]-a[i]
max_a=0
for i in range(n):
	if b[i]>max_a:
		max_a=b[i]
print(k-max_a)

Solution (Python):
import numpy as np
k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0] + k)
a = np.array(A)
 
max_dist = np.max(np.diff(A))
print(k - max_dist)

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

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

Solution (Python):
import sys
k, n = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls += [i + k for i in a_ls]
best = 10**18
for i in range(n):
    best = min(best, a_ls[i+n-1] - a_ls[i])
print(best)

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

max_dist = np.max(np.diff(A))
print(k - max_dist)

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(0,n-1):
  b[i] = a[i+1] -a[i]

b[n-1] = k - a[n-1] + a[0]
ans = k - max(b)
print(int(ans))

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

max_dist = np.max(np.diff(A))
print(k - max_dist)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

K, N = na()
A = na()
A.append(A[0]+K)
t = [A[i+1] - A[i] for i in range(N)]
print(sum(t) - max(t))

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

print(k - dist)

Solution (Python):


K, N = (int(x) for x in input().strip().split())
A = list(map(int, input().strip().split()))

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

print(ans)

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

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

list=[]

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

Solution (Python):
def main():
    length, num = map(int, input().split())
    a = list(map(int, input().split()))
    max_dis = 0
    first_h = a[0]
    prev_h = a[0]
    for h in a[1:]:
        dis = h - prev_h
        max_dis = max(max_dis, dis)
        prev_h = h

    dis = (length - prev_h) + first_h
    max_dis = max(max_dis, dis)
    print(length - max_dis)


if __name__ == "__main__":
    main()

Solution (Python):


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

Bn=An[1:]+[K+An[0]]
dis = [x-y for x,y in zip(An,Bn)]
print(K-abs(min(dis)))

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

d = 0
for i in range(len(lst)-1):
  if d < lst[i+1] - lst[i]:
    d = lst[i+1] - lst[i]
if k-lst[-1] + lst[0] > d:
  d = k-lst[-1] + lst[0]
print(k-d)

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

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

dist.sort()

min_dist = 0
for i in range(n-1):
    min_dist += dist[i]

print(min_dist)

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

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

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

print(K - max(A_aida))

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

Solution (Python):
def solve(k,a):
    b = a[1:] + [k+a[0]]
    dif = [y-x for x,y in zip(a,b)]
    return k-max(dif)

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

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

a.append(a[0] + k)
b = [p - q for p, q in zip(a[1:], a)]
ans = sum(b) - max(b)
print(ans)

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

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

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

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

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

print(ans)

Solution (Python):
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.reverse()
span = []
for i in range(len(A)):
  if i + 1 > len(A) - 1:
    span.append(K - A[0] + A[-1])
    continue
  span.append(A[i] - A[i + 1])
print(sum(span) - max(span))

Solution (Python):
distance,num = map(int, input().split())
tmp = input()
a = tmp.split()
a = list(map(int,a))
b = []

for i in range(len(a)-1):
    b.append(a[i+1] - a[i])
b.append(distance - a[i+1] + a[0])

b = sorted(b)
min = 0
for i in range(len(b)-1):
    min += b[i]

print(min)

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(n-1):
    cnt=a[i+1]-a[i]
    tmp=max(cnt,tmp)

print(k-tmp)

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

a.sort()

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

print(k-max_l)

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

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

Solution (Python):
class TravelingSalesmanAroundLake:
    def calculdat_diff(self, lake_dis, house_num, coordinate_list):
        count = 0
        max_dis = (lake_dis - coordinate_list[len(coordinate_list) - 1]) + coordinate_list[0]
        for c in coordinate_list:
            dis = coordinate_list[count + 1] - c
            if dis > max_dis:
                max_dis = dis
            count +=1
            if count + 1 == len(coordinate_list):
                break
        return max_dis
    
    def calculate_minimum(self, lake_distance, max_dis):
        return lake_distance - max_dis

if __name__ == "__main__":
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    tsl = TravelingSalesmanAroundLake()
    max_dis = tsl.calculdat_diff(K, N, A)
    result = tsl.calculate_minimum(K, max_dis)
    print(str(result))

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

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

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

G =[]
for idx,a in enumerate(A):
    if idx == 0:
        G.append(A[idx]+K-A[N-1])
    else:
        G.append(A[idx]-A[idx-1])

G.append(K-A[N-1])

print(K-max(G))

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

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

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

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

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

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

print(K-max_dist)

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

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

print(k-ans)

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