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



input1 = [int(x) for x in input().split()]
lake = input1[0]
homes = input1[1]
input2 = [int(x) for x in input().split()]

result = 1000000
last = homes - 1
for x in range(homes):
    forward = 0
    backward = 0
    start = input2[x]

    
    if x == 0:
        forward = input2[last] - start
    else:
        forward = (lake - start) + input2[x - 1]
    
    
    if x == last:
        backward = (start) - input2[0]
    else:
        backward = (start) + lake - (input2[x + 1])

    
    result = min(result, forward, backward)

print(result)

Solution (Python):

k, n = map(int, input().split())
homes = list(map(int, input().split()))
d_list = []
for i in range(n-1):
    dist = homes[i+1] - homes[i]
    d_list.append(dist)
dist_0 = homes[0] + (k - homes[n-1])
d_list.append(dist_0)
print(k - max(d_list))

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

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



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

print(sum(len_array)-max(len_array))

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

Solution (Python):
import numpy as np

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

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

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

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

A.append(A[0] + k)

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

print(k - l)

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

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

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

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

Solution (Python):

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

Solution (Python):

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

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



'''
ans_a = 0
for i in range(len(a)-1):
    ans_a +=(a[i+1]-a[i])
for i in range(n):
    if a[i]>k/2:
        a[i] = k-a[i]

a.sort(reverse=True)
kai = a[0]+a[1]

print(min(kai,ans_a))
'''

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

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

Solution (Python):
k,n = list(map(int, input().split()))
a = list(map(int, input().split()))
"""
ans = max(a) - min(a)
for i, num in enumerate(a):
    if num>(k/2):
        a[i] = num - k

print(min(ans,max(a) - min(a)))

min
abs(ai-bi)
abs(ai-(bi-k))
"""
def saitan(num1,num2,k):
    return min(abs(num1 - num2), abs(num1-(num2-k)))

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

print(k-bb)

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

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

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

print(K-l)

Solution (Python):

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

if __name__ == "__main__":
    resolve()

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

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

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

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

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

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

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

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

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [None] * len(a)
ans = 0
for i in range(len(a)):
    if i != len(a)-1:
        b[i] = a[i+1]-a[i]
    else:
        b[i] = k - a[i] + a[0]
ans = sum(b)-max(b)
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(2,n):
    ans=min(ans,k-(a[i]-a[i-1]))
print(ans)

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

diff=-pow(10,9)
for i in range(1,k):
	diff=max(diff,l[i]-l[i-1])

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

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

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)]
l = 0
for i in range(n):
    l = max(l, a[i+1] - a[i])
print(k - l)

Solution (Python):
def c_traveling_salesman_around_lake():
    K, N = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]  
    A.append(A[0] + K)  

    return K - max(A[i + 1] - A[i] for i in range(N))

print(c_traveling_salesman_around_lake())

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

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

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

Solution (Python):
a,b = [int(i) for i in input().split()]
l  = [int(i) for i in input().split()]
l.append(l[0] + a)

l2 = [l[i+1] - l[i] for i in range(b)]

print(a - max(l2))

Solution (Python):
import sys
import random

def test(K, N, A):
    ans = []
    maxp = N-1
    minp = 0
    for i in range(len(A)):
        ans.append(A[maxp] - A[minp])
        A[i] += K
        maxp = (maxp + 1) % (N)
        minp = (minp + 1) % (N)
    print(min(ans))

if __name__ == "__main__":
    if "random" in sys.argv:
        K = int(random.random() * 20000)
        N = int(random.random() * 65536)
        A = sorted([int(random.random() * K) for _ in range(N)])
    else:
        K, N = [int(x) for x in input().split()]
        A = [int(x) for x in input().split()]
    test(K,N,A)
    pass

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

dct = sorted(A)
ans = [] * (N + 1)
new = K + dct[0] - dct[N-1]

ans.append(new)

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

print(sum(ans) - max(ans))

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K, N = lr()
A = np.array(lr())
A = np.append(A, A[0] + K)
diff = np.diff(A)
answer = K - diff.max()
print(answer)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int,input().split()))
A.append(A[0] + K)
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 = list(map(int,input().split()))
a.append(a[0])
ans = 10**7+1

for i in range(n):
    up = (a[i]-a[i-1]) % k
    down = (a[i+1] - a[i]) % k
    ans = min(ans, k-up, k-down)

print(ans)

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

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

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

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

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

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

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

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



diff = []

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



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

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


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



idx = num_list.index(max(num_list))

num_list.pop(idx)
print(sum(num_list))

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

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

dists = []

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

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

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

print(K - dif)

Solution (Python):
import sys













readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))

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

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

    print(k - max(d))


if __name__ == '__main__':
    main()

Solution (Python):



def calc(k, kiten, a):
    koho1 = abs(kiten -a)
    koho2 = kiten + k - a
    koho3 = a + k - kiten

    return min(koho1, koho2, koho3)


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

    max_kkn = a_list[0] + k - a_list[-1]
    for a, b in zip(a_list, a_list[1:]):
        max_kkn = max(max_kkn, b - a)

    print(k - max_kkn)


if __name__ == '__main__':
    main()

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

k,n=map(int,input().split())
a=[int(x) for x in input().split()]
a.sort()
mini=a[n-1]-a[0]
for i in range(n):
    if 0<i<n-1:
        mini=min(mini,min(abs(a[i]-a[i+1]+k)%k,(a[(i-1)%n]-a[i]+k)%k))
    else:
        if i==0:
            mini=min(mini,a[i]-a[i+1]+k)
        else:
            mini=min(mini,a[i-1]-a[i]+k)
print(mini)

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

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

dist = [0] * (N + 1)

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

dist[N] = K - a[N - 1] + a[0]

print(K - max(dist))

Solution (Python):


def solve():
    K, N = list(map(int, input().split()))
    As = list(map(int, input().split()))

    As.sort()
    if len(As) == 1:
        print(0)
        return

    Ls = []  
    for i in range(1, len(As)):
        Ls.append(As[i]-As[i-1])
    Ls.append(As[0] + K-As[-1])

    Ls.sort()
    print(K-Ls[-1])


if __name__ == "__main__":
    solve()

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

c=n-k
l.append(c)
l=sorted(l)

print(n-l[-1])

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[0]-a[n-1])
print(k - max(d))

Solution (Python):
a,b = [int(i) for i in input().split()]
l  = [int(i) for i in input().split()]
l.append(l[0] + a)

l2 = [l[i+1] - l[i] for i in range(b)]

print(a - max(l2))

Solution (Python):
k,n=map(int,input().split())
x=list(map(int,input().split()))
xx=[]
for i in x:
    xx.append(i)
    xx.append(i-k)
y=sorted(xx)
z=[]
for i in range(n+1):
    z.append(y[i+n-1]-y[i])
print(min(z))

Solution (Python):
sel='C'

if sel=='A':
    S=input()
    if S[2]==S[3] and S[4]==S[5]:
        print('Yes')
    else:
        print('No')

if sel=='B':
    X=int(input())
    ans=0
    ans+=(X//500)*1000
    ans+=((X%500)//5)*5
    print(ans)

if sel=='C':
    K,N=map(int,input().split())
    A=[int(i) for i in input().split()]
    d=[A[0]+K-A[-1]]
    for i in range(N-1):
        d.append(A[i+1]-A[i])
    d.sort()
    print(K-d[-1])

Solution (Python):
k,_,*a=map(int,open(0).read().split());a.append(k+a[0]);b=[y-x for x,y in zip(a,a[1:])];print(sum(b)-max(b))

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
n = K + min(A)
A.append(n)
A = sorted(A)
l = len(A)
B = []
for i in range(l-1):
  b = abs(A[i+1] - A[i])
  B.append(b)
x = sum(B) - max(B)
print(x)

Solution (Python):

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

b.pop(ii)

print(sum(b))

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

Solution (Python):
K,N = map(int,input().split())
A = list(map(int,input().split()))
for n in range(N):
  A.append(-(K-A[n]))
A.sort()
res = K
for n in range(N):
  res = min(A[n+N-1] - A[n], res)
print(res)

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

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

ans = min(K-Big, max(A)-min(A))
print(ans)

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

ans = K-max_d
print(ans)

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

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

ans = min(K-Big, max(A)-min(A))
print(ans)

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

Solution (Python):
def II():return int(input())
def MI():return map(int,input().split())
def LI():return list(MI())
def ok():print("Yes")
def ng():print("No");exit()

import numpy
k,n=MI()
a=LI()
a+=(a[0]+k),
a=numpy.diff(a)
print(k-max(a))

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

Solution (Python):



def calc(k, kiten, a):
    koho1 = abs(kiten -a)
    koho2 = kiten + k - a
    koho3 = a + k - kiten

    return min(koho1, koho2, koho3)


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

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

    print(k - max_kkn)


if __name__ == '__main__':
    main()

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

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

print(K-max)

Solution (Python):

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

total = 0

dist = []


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

ans = total - max(dist)
print(ans)

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

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

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

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

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

d.append(K - A[N-1] + A[0])
mx = max(mx, K - A[N-1] + A[0])




ans = K - mx
print(ans)

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

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

Solution (Python):


def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    z=0
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            if k-(a[z+1]-i)<=k-(i-a[z-1]):
                res=k-(a[z+1]-i)
            else:
                res=k-(i-a[z-1])
        if resf>=res:
            resf=res
        z+=1
    print(resf)

if __name__ == '__main__':
    main()

Solution (Python):

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

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

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

print(K-max(dists))

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

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

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

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

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

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

Solution (Python):

import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
maxi = -1
for i in range(0, len(a)):
    if i == 0:
        tmp = max(abs((a[i] + k) - a[i - 1]), abs(a[i] - a[i + 1]))
    elif 0 < i < len(a) - 1:
        tmp = max(abs(a[i] - a[i - 1]), abs(a[i] - a[i + 1]))
    else:
        tmp = max(abs(a[i] - a[i-1]), abs(a[i] - (a[0] + k)))
    if maxi == -1:
        maxi = tmp
    else:
        maxi = max(maxi, tmp)
print(k - maxi)

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

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

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

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

print(ans)

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

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

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

Solution (Python):

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


A = A + [K + A[0]]


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

print(K - max(lst))

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

Solution (Python):
KN = input("").split()
A = input("").split()

K = int(KN[0])
N = int(KN[1])
A_0 = int(A[0])
A_N = int(A[N-1])
L = A_0

for j in range(N-1):
    A_i = int(A[j+1])
    A_m = int(A[j])
    if A_i - A_m > L:
        L = A_i - A_m

if L > K - A_N + A_0:
    print(K-L)
else:
    print(A_N - A_0)

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

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

box = []
for i in range(N):
    d = abs(A[i-1]-A[i])
    if d>K/2:
        d = K - d
    box.append(d)
print(K-max(box))

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

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

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

B.sort()

Big = B[N-1]

print(K-Big)

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

Solution (Python):
def main():
    K, N = map(int, input().split(' '))
    A = list(map(int, input().split()))
    A.sort()
    A.append(A[0] + K)
    max_diff = A[0] + K - A[-1]
    for a, b in zip(A, A[1:]):
        if (b - a) > max_diff:
            max_diff = b - a
    print(K - max_diff)


if __name__ == '__main__':
    main()

Solution (Python):
k, n = map(int, input().split())
aa = list(map(int, input().split()))
 
mdiff = 0
for i in range(n):
    mdiff = max(mdiff, abs(k + aa[i] - aa[i - 1]) % k)
if n == 2:
    print(mdiff)
else:
    print(k - mdiff)

Solution (Python):


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

diff = []
for i in range(len(distances)):
    if i == len(distances) - 1 :
        diff.append(k + distances[0] - distances[i])
    else:
        diff.append(distances[i + 1] - distances[i])
max_d = max(diff)
print(sum(diff) - max_d)

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

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

    print(sum(distances) - max(distances))


if __name__ == '__main__':
    main()

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

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

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

As.append(K + As[0])
print(K - max([y - x for x,y in zip(As, As[1:])]))

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

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


dist_list = [K - house_list[-1] + house_list[0]]


for n in range(1, N):
    dist_list.append(house_list[n] - house_list[n-1])
print(K - max(dist_list))

Solution (Python):
K,N = map(int, input().split())
AN = max = i = j = 0
c = []

A = [int(N) for N in input().split()]
while i+1 != N:
    c.append(A[i+1] - A[i])
    if c[i] > max:
        max = c[i]
    i = i+1

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

if A[0] - 0 + K - A[N-1] > max:
    max = A[0] - 0 + K - A[N-1]

while j != N:
    AN = AN + c[j]
    j = j + 1

AN = AN - max

print(AN)

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

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

Solution (Python):
f=lambda:map(int,input().split());k,n=f();l=list(f());print(min((l[i-1]-l[i])%k for i in range(n)))

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

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

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

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

I = input().split()

A = [ int(I[0]) ]
L = [ A[0] ]
for i in range(1,N):
  A.append( int(I[i]) )
  L.append( A[i] - A[i-1] )

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

L.sort()
print(K - L[N-1])

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

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

Solution (Python):
import sys
from copy import deepcopy
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def cal(a,b,k):
    return min(abs(a-b), k-abs(a-b))

def cal2(l):
    n = len(l)
    ans = 0
    for i in range(n-1):
        ans += l[i+1] - l[i]
    return ans
def main():
    k,n = map(int, input().split())
    As = list(map(int, input().split()))
    le = list()
    for i in range(n):
        if i == n-1:
            le.append(cal(As[i], As[0] , k) )
        else:
            le.append(As[i+1]- As[i])
    print(k-max(le))
if __name__ == '__main__':
    main()

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(tmp,ans)
print(K - ans)

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

a.append(k + a[0])

ans = [abs(a[(i + 1) % (n + 1)] - a[i]) for i in range(n)]
print(k - max(ans))

Solution (Python):
import numpy as np

K, N = [int(element) for element in input().split(" ")]
AList = np.array([int(element) for element in input().split(" ")])
distance = np.diff(AList)
start_end_distance = abs((K - AList[-1]) + AList[0])
distance = np.append(distance, start_end_distance)

print(K - np.max(distance))