Problem:
 Score : 300 points    Problem Statement There is a circular pond with a perimeter of K meters, and N houses around them.  The i -th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.  When traveling between these houses, you can only go around the pond.  Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.      Constraints  2 \leq K \leq 10^6  2 \leq N \leq 2 \times 10^5  0 \leq A_1 < ... < A_N < K  All values in input are integers.         Input Input is given from Standard Input in the following format:  K  N  A_1  A_2  ...  A_N       Output Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.        Sample Input 1 20 3
5 10 15      Sample Output 1 10  If you start at the 1 -st house and go to the 2 -nd and 3 -rd houses in this order, the total distance traveled will be 10 .       Sample Input 2 20 3
0 5 15      Sample Output 2 10  If you start at the 2 -nd house and go to the 1 -st and 3 -rd houses in this order, the total distance traveled will be 10 .

Solution (Python):
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''



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

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

for i in range(n-1):
    b = a[i+1]-a[i]
    if(maxim<b):
        maxim = b
        
print(k-maxim)

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

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

m = n

max_dist = 0
while n >= 1:
    if max_dist < li[n -1] - li[n -2]:
        max_dist = li[n -1] - li[n -2]
    n -= 1

x = 0
while m >= 2:
    x += li[m -1] - li[m -2]
    m -= 1

print(min([k - max_dist, x]))

Solution (Python):
def find_d(a,k,n):
    d = a[0] + (k - a[n-1])
    point = 0
    
    for i in range(n-1):
        tmp = a[i+1] - a[i]
        if d<tmp:
            d = tmp
            point = i+1
    return d

if __name__ == "__main__":
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    d = find_d(a,k,n)
    
    print(k-d)

Solution (Python):
input_line = input()
tmpa,tmpb = input_line.split()
K = int(tmpa)
N = int(tmpb)
listin = input().split()
int_s = [int(s) for s in listin]
Distance = []

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

print(sum(Distance)-max(Distance))

Solution (Python):
k, n = map(int, input().split())
a = [int(x) for x in input().split()]
a_ = [x - a[0] for x in a] + [k]
d = [y - x for x, y in zip(a_, a_[1:])]
print(k - max(d))

Solution (Python):

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

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

b = K -abs(A[0]-A[N-1])
L.append(b)

ans = K - max(L)

print(ans)

Solution (Python):
k,n=map(int,input().split())
home = input()
home = home.split()
for i in range(len(home)):
    home[i] = int(home[i])
d=[]
for i in range(len(home)):
    if i == len(home)-1:
        d.append(home[0]+k-home[i])
        break
    d.append(home[i+1]-home[i])
Max = max(d)
leng=len(home)
count=0
for i in range(len(d)):
    if d[i] == Max:
        break
d=0
while True:
    
    if count==leng-1:
        break
    j=i-1
    if i==0:
        d+=abs(home[leng-1]-(home[0]+k))
        count+=1
    else:
        d += abs(home[i]-home[i-1])
        count+=1
    i-=1
    if i == -1:
        i=leng-1
print(d)

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


min1 =10**10

for i in range(n):
   min1 = min(min1,(k-li[i]))
   
print(min1)

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

Solution (Python):

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


answer = K

for i in range(1, N):
    temp =  K + A[i-1] - A[i]
    answer = min(temp, answer)

answer = min(answer, A[N-1]-A[0])

print(answer)

Solution (Python):



def solve(*args: str) -> str:
    k, n = map(int, args[0].split())
    A = list(map(int, args[1].split()))

    D = [k-A[-1]+A[0]] + [A[i+1]-A[i] for i in range(n-1)]

    return str(k-max(D))


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
a = input().split(' ')
a = [int(s) for s in a]

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

l = []
for i in range(a[1]):
    if i == 0:
        l.append(b[a[1] - 1] - b[i])
    else:
        l.append((a[0] - b[i]) + b[i - 1])
print(min(l))

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

print(K - max(B))

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

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

Solution (Python):
import numpy as np

val = input().split(' ')
k = int(val[0])
n = int(val[1])

alist = sorted([int(a) for a in input().split(' ')])

a2list = list(map(lambda a: a + k, alist))
alist.extend(a2list[:-1])
adiff = np.diff(alist)
before = sum(adiff[0: n-1])
result = before
for i in range(1, n):
    tmp = before - adiff[i - 1] + adiff[i - 2 + n]
    before = tmp
    if tmp < result:
        result = tmp
print(result)

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

for i in range(n):
    if(i == 0):
        f = l[i-1] - l[0]
        s = (k - l[i+1]) + l[i]
    elif(i == n-1):
        f = l[i] - l[0]
        s = (k - l[i]) + l[i-1]
    else:
        f = l[i] + (k - l[i+1])
        s = (k - l[i]) + l[i-1]
    
    if(m > min(f,s)):
        m = min(f,s)

print(m)

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

a_list_sorted = sorted(a_list)

max_distance = 0
for i in range(1, len(a_list_sorted)):
    tmp_max_distance = a_list_sorted[i] - a_list_sorted[i-1]
    if max_distance < tmp_max_distance:
        max_distance = tmp_max_distance

tmp_max_distance_last = a_list_sorted[0] + (k - a_list_sorted[-1])
if max_distance < tmp_max_distance_last:
    max_distance = tmp_max_distance_last

print(k-max_distance)

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

Solution (Python):
import math

S =  list(map(int,input().split()))
K = S[0]
N = S[1]
P =  list(map(int,input().split()))
R = []
for i in range(len(P)):
    if i >= 1:
        X = P[i]-P[i-1]
        R.append(X)
        if i == N-1:
            X = K + P[0] - P[i]
            R.append(X)
B = max(R)
answer = K-B        
print(answer)

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

Solution (Python):
import sys
input = sys.stdin.readline
from collections import defaultdict, Counter

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

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

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

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

print('{}'.format(k-max_sub))

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


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

d_li.append(K+A_li[0] - A_li[N-1])


allsum = sum(d_li)
ans = allsum
for d in d_li:
	ans = min(ans,allsum-d)

print(ans)

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

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

sa.append(k-max(a)+min(a))
ans = sum(sa)-max(sa)



print(ans)

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

max = 0
result = 0
j = 0
k = 0

for x in range(1, len(A)):
  
  
  
  
  A_next.append(abs(A[x] - A[x-1]))
  if max < A_next[j]:
    max = A_next[j]
    
    k = j
  j += 1


for x in range(len(A_next)):
  if x == k:
    pass
  else:
    result += A_next[x]
print(result)

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

if a[0] == 0:
    li.append(k - a[n - 1])
    li.sort()
    li.pop(-1)
else:
    li.append(k - a[n - 1] + a[0])
    li.sort()
    li.pop(-1)
print(sum(li))

Solution (Python):
import sys

K,N = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))





distance = []

for i,a in enumerate(A):
    if i == len(A) - 1:
        distance.append(K - A[i] + A[0])
    else:
        distance.append(A[i+1] - A[i])


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

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(k-max(b))

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

sub_a = []

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

max_sub = max(sub_a)

print(K - max_sub)

Solution (Python):
ddd = [input() for i in range(2)]
eee = ddd[0].split()
fff = ddd[1].split()
longest_distance = int(eee[0]) + int(fff[0]) - int(fff[-1])
for i in range(len(fff) - 1):
    distance = int(fff[i+1]) - int(fff[i])
    if longest_distance < distance:
        longest_distance = distance
shortest_road = int(eee[0]) - longest_distance
print(shortest_road)

Solution (Python):

"""
Created on Thu Dec 19 22:37:09 2019

@author: lucifer
"""
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
max=k+a[0]-a[n-1]
sum=max
for i in range(n-1):
    if a[i+1]-a[i]>max:
        max=a[i+1]-a[i]
    sum=sum+a[i+1]-a[i]
sum=sum-max
print(sum)

Solution (Python):


if __name__ == "__main__":
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    
    A_dif = []
    for i in range(len(A)-1):
        
        
        A_dif.append(A[i + 1] - A[i])
    A_dif.append((abs(K - A[-1]) + A[0]))
    
    A_dif.sort(reverse=True)
    ret = sum(A_dif[1:])
    print(ret)

Solution (Python):
KN = input().split()
A = input().split()
kn = [int(s) for s in KN]
a = [int(i) for i in A]
b = kn[0] + a[0] - a[-1]
num = 0
while num < len(a) -1:
    a[num] = a[num+1] - a[num]
    num += 1
a[len(a)-1] = b
print(int(KN[0])-max(a))

Solution (Python):
import numpy as np
ina = input().split()
k=int(ina[0])
n=int(ina[1])
inaa = list(map(int, input().split()))
f=int(inaa[0])
inaa.pop(0)
inaa.append(k+f)
inaaa=np.array(inaa)
inaaaa=np.diff(inaaa,n=1)
print(k-np.amax(inaaaa))

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

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

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

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

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

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

l=[]

l.append(kn[0]-a[-1]+a[0])

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

print(kn[0]-max(l))

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


d_ls = []
for i in range(1, n):
  d = abs(a[i] - a[i-1])
  d_ls.append(d)


d = k - sum(d_ls)
d_ls.append(d)


d_ls.remove(max(d_ls))

print(sum(d_ls))

Solution (Python):
import sys

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

x = sys.stdin.readline()
x = x.split(' ')
a=[int(str) for str in x]

prev = 0
amax = 0
sa = 0
for i in range(0,n):
  if(i == 0):
    prev = a[i]
  else:
    among = a[i] - prev
    if( amax < among):
      amax = among
    prev = a[i]
    sa += among

last = a[0] + k - a[n-1]
if( amax < last):
  amax = last
sa += last

print(sa-amax)

sys.stdout.flush()

Solution (Python):
s=0;f=0;p=0;n=0
K,N=map(int,input().split())
n=N
A=list(map(int,input().split()))
if K!=A[-1]:
  if A[0]==0:
    A.append(K)
  else:
    A.append(K+A[0])

for i in range(N):
  if abs(A[i+1]-A[i])>s:
    s=abs(A[i+1]-A[i])
    f=i+1
if f==(N):
  f=0

if f==0:
  n=n-1
for i in range(f,n):
  p=p+abs(A[i+1]-A[i])
  
for i in range(f-1):
  p=p+abs(A[i+1]-A[i])
print(p)

Solution (Python):
from sys import stdin
K, N = [int(x) for x in stdin.readline().rstrip().split()]
l = [int(x) for x in stdin.readline().rstrip().split()]

l.sort()

l2 = []

for i in range(N-1):
    l2.append(l[i+1] - l[i])

l2.append(l[0] + K - l[N-1])

ans = K - max(l2)

print(ans)

Solution (Python):
S = input().split(" ")
K = int(S[0])
N = int(S[1])
ARR = [int(s) for s in input().split(" ")]


def calculate(k,n,arr):
    result = []
    for i in range(n-1):
        s = arr[i+1] - arr[i]
        m = k - s
        result.append(m)

    result.append(arr[n-1] - arr[0])

    print(min(result))


calculate(K,N,ARR)

Solution (Python):
length,house = map(int,input().split())	
plot= list(map(int,input().split()))
plot.append(plot[0]+length)
ans=[]
for i in range(1,house):
  ans.append(min(length-abs((plot[i-1]-plot[i])),length-abs((plot[i+1]-plot[i]))))
ans.append(min(length-abs(plot[-1]-plot[-2]),length-abs(plot[1]-plot[0])))  
print(min(ans))

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


d_ls = []
for i in range(1, n):
  d = abs(a[i] - a[i-1])
  d_ls.append(d)


d = k - sum(d_ls)
d_ls.append(d)


d_ls.remove(max(d_ls))

print(sum(d_ls))

Solution (Python):
K,N=input().split()
K=int(K)
N=int(N)
A=list(map(int,input().split()))
i=0
C=[]
for i in range(N-1):
    if (2<=K<=10**6)and(2<=N<=2*10**5)and(0<=A[i]<A[i+1]<K)and(i<N-1):
        B=A[i+1]-A[i]
        C.append(B)
        i+=1
    if (i==N-1)and(A[N-1]<K):
        B=K-A[N-1]+A[0]
        C.append(B)
d=max(C)
e=K-d
print(e)

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

s = 0
save = 0
start = 0
for i, a in enumerate(an):
  term = a - start
  if term > save:
    save = term
    s = i
  start = a

term = (k - an[-1]) + an[0]
if term > save:
  save = term
  s = 0

print(k-save)

Solution (Python):
import numpy as np

k, n = map(int,input().split())
a = np.array(list(map(int, input().split())))
diff = a[1:] - a[:-1]
diff = np.append(diff, k - a[-1] + a[0])
if np.argmax(diff) == len(a) - 1:
    print(a[-1] - a[0])
else:
    print(k - a[np.argmax(diff) + 1] + a[np.argmax(diff)])

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

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


kankaku = [0] * N

for i, v in enumerate(A):
	if i == 0:
		
		kankaku[i] = A[i] + (K - A[N - 1])
	else:
		kankaku[i] = v - A[i - 1]

kankaku.remove(max(kankaku))
print(sum(kankaku))

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

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

ans = l[-1] - l[0]

for i in range(N-1):
  tempo_ans = l[i]+K -l[i+1]
  
  if ans > tempo_ans:
    ans = tempo_ans
print(ans)

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

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

d.append(k-a[-1]+a[0])
d=sorted(d,reverse=True)
print(sum(d[1:]))

Solution (Python):
k,n=map(int,input().split())
a=[int(x) for x in input().split()]
half=k//2
min_a,max_a=min(a),max(a)
ans=0
diff=[0]*n
for i in range(1,n):
	diff[i-1]=a[i]-a[i-1]
diff[n-1]=k-a[n-1]+a[0]


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

Solution (Python):
k,n=map(int,input('').split())
a=input('')
a=a.split(' ')
for b in range(n):
    a[b]=int(a[b])
a.sort()

m=0
for b in range(n-1):
    if m<=a[b+1]-a[b]:
        m=a[b+1]-a[b]

if m<=k-a[n-1]+a[0]:
    m=k-a[n-1]+a[0]

print(k-m)

Solution (Python):
lake_circumference, house_count = map(int, input().split())
locations = list(map(int, input().split()))
min_distance = 10e6
for i in range(house_count):
    if i == 0:
        distance = locations[house_count-1] - locations[0]
    else:
        distance = lake_circumference + locations[i-1] - locations[i]
    if distance < min_distance:
        min_distance = distance
print(min_distance)

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

Solution (Python):
import numpy as np

K,N = map(int, input().split())
An = list(map(int, input().split()))
arr = np.array(An)
arr = np.diff(arr)
ans = list(arr)
ans.append(K-An[-1]+An[0])

ans.remove(max(ans))
print(sum(ans))

Solution (Python):
import heapq

k, n = map(int, input().split())
a = [int(x) for x in input().split()]
qa = []
heapq.heapify(qa)

for x in range(len(a) - 1):
    heapq.heappush(qa, abs(a[x] - a[x + 1]))
heapq.heappush(qa, k - a[-1] + a[0])

dis = 0
for x in range(len(qa) - 1):
    dis += heapq.heappop(qa)

print(dis)

Solution (Python):

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

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


ans = 0
max = 0


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


dl = K - A[N - 1]
if dl + A[0] < max:
    ans = ans - max + dl + A[0]


print(ans)

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

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

tokei = []

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

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

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

Solution (Python):
a,n=map(int,input().split())
d=[0]*n
b=[int(ss) for ss in input().split()]

for ii in range(n-1):
  d[ii+1]=b[ii+1]-b[ii]


d[0]=a-b[n-1]+b[0]
d=sorted(d)

print(a-max(d))

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

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


l = []

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



M = max(l)



print(K - M)

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

dif =[]

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

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

dif.sort()
ans = 0
for i in range(n-1):
    ans += dif[i]

print(ans)

Solution (Python):
K, N = [int(x) for x in input().split()]
list = [0]*N
list = [int(x) for x in input().split()]
list.append(K+list[0])

a = 0
for i in range(N):
    if a < (list[i+1] - list[i]):
        a = list[i+1] - list[i]
        b = i

print(K-a)

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

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

Solution (Python):
from copy import deepcopy

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

a = list(map(int, input().split()))
a = sorted(a)
max_ = max(a)
min_ = min(a)
a_ = deepcopy(a)
ans = 100000000000
for index in range(len(a)):
    
    
    temp = max_ - min_
    max_ = min_ + k
    if index != len(a)-1:
        min_ = a[index+1]
    a[index] = a[index] + k
    
    if ans > temp:
        ans = temp

max_ = max(a_)
min_ = min(a_)

for index in reversed(range(len(a))):
    
    
    
    temp = max_-min_
    min_ = max_ -k
    if index != 0:
        max_ = a_[index-1]
    
    
    if ans > temp:
        ans = temp

print(ans)

Solution (Python):
import numpy as np

K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
x=np.array(A[1:])
y=np.array(A[:-1])
B=x-y
C=B.tolist()
C.append(K+A[0]-A[N-1])
print(K-int(max(C)))

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

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

result.sort()
print(sum(result[:-1]))

Solution (Python):
K, N = map(int, input().split())
A = 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())
AA = list(map(int,input().split()))
 
KK = [False]*K
for a in AA:
  KK[a] = True
 
dist = 0
dist_max = 0
for i in range(2*K):
  if KK[int(i%K)] == 1:
    dist = 1
  else:
    dist += 1
  if dist > dist_max:
    dist_max = dist
print(K-dist_max)

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

Solution (Python):
import numpy as np

N,M=(int(x) for x in input().split())
A=list(map(int, input().split()))
A=np.array(A)
B=A[1:]-A[:-1]
b=max(B)
c=A[-1]-A[0]
if c > N/2:
    d=int(N-c)
else:
    d=c
e=max(b,d)
f=N-e

print(f)

Solution (Python):
import heapq

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

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


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


heapq.heapify(dis)

heapq.heappop(dis)

print(-sum(dis))

Solution (Python):
a = input().split()
b = input().split()

K=int(a[0])
N=int(a[1])



b = [int(s) for s in b]

c=0
d=[]
for i in b:
  
  d.append(i-c)
  c=i
d[0]=d[0]+K-b[N-1]

print(K-max(d))

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

max = K - A[-1] + A[0]
B.append(max)
ind = 0
for i in range(1, N):
	_max = A[i] - A[i-1]
	B.append(_max)
	if _max < 0:
		_max = K + _max
	if _max > max:
		max = _max
		ind = i
dis1 = 0

for i in range(1, N):
	if i + ind >= N:
		dis1 += B[(i + ind) - N]
	else:
		dis1 += B[(i + ind)]
        
print(dis1)

Solution (Python):
import sys
import numpy as np
import itertools

from collections import deque 
from functools import reduce



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

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

a_np = np.array(a)
a_np = a_np.flatten()
a_np = np.hstack((a_np, k+a_np[0]))
a_sa = abs(a_np[:-1] - a_np[1:])
print(a_sa.sum() - a_sa.max())

Solution (Python):
from math import ceil,sqrt
def main():
    
    
    
    
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    b=[0]*n
    for i in range(n):
        if(i==0):
            b[i]=(a[n-1]-a[i])
        else:
            b[i]=(a[n-1]-a[i])+(k-a[n-1])+a[i-1]
    a1=[(k-i) for i in a]
    a1.reverse()
    b1=[0]*n
    for i in range(n):
        if(i==0):
            b1[i]=(a1[n-1]-a1[i])
        else:
            b1[i]=(a1[n-1]-a1[i])+(k-a1[n-1])+a1[i-1]
    b1.reverse()
    ans=1e9+7
    
    for i in range(n):
        ans=min(ans,min(b[i],b1[i]))
    print(int(ans))
    
    
    
    
    
    
    
    
    
    




    
if __name__ =="__main__":
    main()

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


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

L = 0

for i in range(n-1):
  M = A[i+1] - A[i]
  if M > L:
    L = M
      
M = (k-A[n-1]) + A[0]
if M > L:
  L = M
  
print(k-L)

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

y=[sum(b)]
for i in range(len(b)):
    y.append(k-b[i])
print(min(y))

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

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

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

print(k-max)

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

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

print(ans)

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

ans = A[N - 1] - A[0]
now = ans
for i in range(1, N):
  prior = A[i] - A[i - 1]
  if i == 1:
    
    next_ = A[0] + K - A[N - 1]
  else:
    next_ = A[i-1 ] - A[i -2] 
  
  now = now - prior + next_
  
  if ans > now:
    ans = now
print(ans)

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

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

id =tmp.index(max(tmp))
if(id<N-1):
    print(li[id]+K-li[id+1])
else:
    print(li[id]-li[0])

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

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

distances.append(k-a[len(a)-1]+a[0])

m=max(distances)

print(k-m)

Solution (Python):
K,N = map(int,input().split())
li = list(map(int,input().split()))
kyori = 0
for i in range(1,len(li)):
    if li[i] - li[i-1] > kyori:
        kyori = li[i] - li[i-1]
syokiti = li[0]+K-li[N-1]         
if syokiti < kyori:
    print(K - kyori)
else:
    print(K-syokiti)

Solution (Python):

K,N = map(int, input().split())
houses = list(map(int, input().split()))
M = N - 1
lis = []
a = 0
while M > 0:
    lis.append(houses[a+1] - houses[a])
    M -= 1
    a += 1
lis.append(K - houses[N-1] + houses[0])
hou = sorted(lis, reverse = True)
del hou[0]
print(sum(hou))

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

X = list()
for i in range(n-1):
    x = A[i+1]-A[i]
    X.append(x)

y = k-A[n-1]+ A[0]

X.append(y)

m = max(X)

print(k-m)

Solution (Python):
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
mn = k+1
for i in range(n):
    nxt= (i+1)%n;
    prv= (i-1+n)%n;
    if a[i]<a[nxt]:
        diff1= k- (a[nxt]-a[i])
    else:
        diff1= a[i]-a[nxt]
    if a[i]>a[prv]:
        diff2=k-(a[i]-a[prv])
    else:
        diff2=a[prv]-a[i]
    mn=min(diff1,mn)
    mn=min(diff2,mn)
print(mn)

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

a = A[1] - A[0]

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

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

e = max(c,d)

print(K-e)

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

"""
5 - 0 
10 - 5
15 - 10
20 - 10  
"""
L = []
for i in range(N):
    if i == 0:
        continue
    else:
        L.append(A[i] - A[i - 1])
        
L.append(A[0] + K - A[N-1])

L = sorted(L, reverse=True)
del L[0]
print(sum(L))

Solution (Python):
input_first = input()
input_second = input()

split_input_first = input_first.split()
split_input_second = input_second.split()

k = int(split_input_first[0])
n = int(split_input_first[1])

distance_list = []
for i in range(n):
    if i == n - 1:
        distance_list.append(k + int(split_input_second[0]) - int(split_input_second[n-1]))
    else:
        distance_list.append(int(split_input_second[i+1]) - int(split_input_second[i]))
distance_list = sorted(distance_list)

result = 0
for i in range(n):
    if i == n - 1:
        break
    else:
        result += distance_list[i]
print(result)

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

Solution (Python):
import numpy as np

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

diff_A = []
for i in range(N):
    diff_A.append(A[i+1] - A[i])
diff_max = max(diff_A)
print(K - diff_max)

Solution (Python):


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

table =[-1] * len(a)

for i in range(len(a)):
    if i < len(a) - 1:
        table[i] = a[i+1] - a[i]
    else:
        table[i] = a[0] + k - a[len(a)-1]
table.remove(max(table))

print(sum(table))