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

Solution (Python):
k,n=map(int,input().split())
arr=list(map(int,input().split()))
ans=10**18 
for i in range(n): 
 dist1=(arr[i-1]-arr[i])%k

 dist2=(arr[i]-arr[(i+1)%n])%k
 ans=min(ans,dist1,dist2)
print(ans)

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

distanceList = []

distance = 0
for i in range(N[1]):
    M.append((M[i] + N[0]))

for i in range(len(M) - 1):
    distanceList.append(M[i + 1] - M[i])

print(N[0] - max(distanceList))

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

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

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

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

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

lis = []

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

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

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())
p = list(map(int, input().split()))
l = []

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

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

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

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

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

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

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

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

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    r = -1
    for i in (range(len(A))):
        if i == 0:
            tmp = A[i] + (K - A[len(A)-1])
        else:
            tmp = A[i] - A[i-1]
        r = max(tmp, r)
    print(K-r)



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

Solution (Python):
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])
s=max(B)
print(K-s)

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

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


resolve()

Solution (Python):
N,X=map(int,input().split())
S=list(map(int,input().split()))

uramichi=N-S[X-1]+S[0]
saidai=0
L=[]

goukei=0

for i in range(0,X-1):
    if saidai<S[i+1]-S[i]:
        saidai=S[i+1]-S[i]
    goukei+=S[i+1]-S[i]

if saidai>uramichi:
    goukei=goukei-saidai+uramichi
    
print(goukei)

Solution (Python):
K, N = map(int, input().split())
loc = list(map(int, input().split()))
distances = []
total = []

for i in range(N-1):
    distances.append(loc[i+1] - loc[i])

distances.append(loc[0] + K - loc[N-1])
max_dis = max(distances)

print(K - max_dis)

Solution (Python):
K, N = map(int, input().split())
loc = list(map(int, input().split()))
distances = []
total = []

for i in range(N-1):
    distances.append(loc[i+1] - loc[i])

distances.append(loc[0] + K - loc[N-1])

for i in range(N):
    total.append(K - distances[i])

print(min(total))

Solution (Python):

k, n = map(int, input().split())
house = [int(i) for i in input().split()]
house.append(k+house[0])
dist = []
cand = []


for i in range(n):
  dist.append(house[i+1]-house[i])
  
for i in range(n):
  cand.append(k-dist[i])


print(min(cand))

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

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

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

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

print(k-res)

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

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

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

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

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

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

print(K - max(tmp))

Solution (Python):




import sys
from fractions import gcd
import math
from math import ceil,floor
import collections











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



A.append(A[0]+K)

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

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

print(k-l)

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

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

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

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

Solution (Python):
a, b = map(int, input().split())
A = list(map(int, input().split()))
d = []
for i in range(len(A)):
  if i == len(A)-1:
    r = a-A[i]+A[0]
    d.append(r)
  else:
    d.append(A[i+1]-A[i])
print(a-max(d))

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

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

b.sort()
print(k-b[n-1])

Solution (Python):
a, b = map(int, input().split())
A = list(map(int, input().split()))
d = []
for i in range(len(A)):
  if i == len(A)-1:
    r = a-A[i]+A[0]
    d.append(r)
  else:
    d.append(A[i+1]-A[i])
print(a-max(d))

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


if __name__ == '__main__':
    main()

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

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

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

sum_memo = sum(memo)
sum_memo -= max(memo)
print(sum_memo)

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

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

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
tot_sum = 0
curr_max = 0
i = 0
while i < N - 1:
    temp = A[i + 1] - A[i]
    tot_sum += temp
    curr_max = max(curr_max, temp)
    i += 1

curr_max = max(curr_max, (K - A[i]) + A[0])
tot_sum += (K - A[i]) + A[0]

print(tot_sum - curr_max)

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

maximum=0

for i in range(len(a)-1):
    if maximum<a[i+1]-a[i]:
        maximum=a[i+1]-a[i]
if maximum<=min(a[N-1]-a[0],K-a[N-1]+a[0]):
    maximum=min(a[N-1]-a[0],K-a[N-1]+a[0])

print(K-maximum)

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

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

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

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

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

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





def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))





from itertools import combinations

def main():
	k,n=MI()
	A=LI()
	ans=A[-1]-A[0]

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

	print(ans)





if __name__ == "__main__":
	main()

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

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

print(k - max(space))

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

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

print(k - max(dis))

Solution (Python):

import sys
import numpy as np


def solve(K: int, N: int, A: "List[int]"):

    left = sorted(_left(K, A))
    right = sorted(_right(K, A))

    ans = max(sum(left[: len(left) - 1]), sum(right[: len(right) - 1]))

    print(ans)
    return


def _left(K, A):
    rev_a = list(A[::-1])
    end = A[0] + K - A[len(A) - 1]
    left = np.ediff1d(np.array(rev_a), to_end=[end]).tolist()
    left = [abs(l) for l in left]
    return left


def _right(K, A):
    end = A[0] + K - A[len(A) - 1]
    right = np.ediff1d(np.array(A), to_end=[end]).tolist()
    return right



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)


if __name__ == "__main__":
    main()

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

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

d=k-abs((a[0]-a[n-1]))
max_da=max(d,max_da)

print(k-max_da)

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

m = A[0] + K - A[-1]
res = A[0] + K - A[-1]

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

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

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

Solution (Python):










K, N = map(int, input().split())
points = list(map(int, input().split()))
l = 0

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

ans = K - l
print(ans)

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

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

root = k
for i in range(n):
    if(i != n-1):
        road = k - (a[i+1] - a[i])
    else:
        road = a[n-1] - a[0]
    if(root > road):
        root = road

print(root)

Solution (Python):
lake_length, num_of_house = map(int, input().split())
positions = list(map(int, input().split()))

max_interval = lake_length - positions[num_of_house - 1] + positions[0]

for i in range(1, num_of_house):
    max_interval = max(max_interval, positions[i] - positions[i - 1])

print(str(lake_length - max_interval))

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

k, n, *a = map(int, read().split())
a = sorted(a)
d = [a[0]+k-a[-1]]
for i in range(n-1):
    d.append(a[i+1]-a[i])
print(sum(d)-max(d))

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


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

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

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

ans = 0
max_dif = 0
for i in list(range(1, n)):
	x = abs(a[i] - a[i-1])
	max_dif = max(max_dif, x)
	ans += x

ans += k-a[-1]+a[0]
max_dif = max(max_dif, k-a[-1]+a[0])
ans -= max_dif
print(ans)

Solution (Python):
def solve(K, N, As):
    longest = 0
    for i in range(len(As)):
        x = 0
        if i == len(As) - 1:
            x = As[0] + (K - As[i])
        else:
            x = As[i + 1] - As[i]
        longest = max(longest, x)
    print(K - longest)


if __name__ == '__main__':
    K, N = map(int, input().split())
    As = [int(a) for a in input().split()]
    solve(K, N, As)

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

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

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


dif=[List[0]]
for i in range(1,N):
    dif.append(List[i]-List[i-1])

dif[0]+=K-List[N-1]
ma=max(dif)
print(sum(dif)-ma)

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

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

del B[B.index(max(B))]

print(sum(B))

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

A.sort()
D = []
D.append(K - max(A) + min(A))

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

print(K - max(D))

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

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

print(k - d)

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

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

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

ans = max(ans,abs(k + a[0] - a[-1]))
print(k - ans)

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

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

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


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

print(K-max(A2))

Solution (Python):
K, N = input().split()
k = int(K)
n = int(N)
A = list(map(int, input().split()))
B = []
for j in range(n):
    if j != n-1:
        B.append(A[j+1] - A[j])
    else:
        B.append(k - A[n-1] + A[0])
print(k - max(B))

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

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

print(K-max(diffA))

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

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

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

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

Solution (Python):






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





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

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

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

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

j = 0
i = 0
while i<n:
    if i == n-1:
        x = k + a[0] - a[i]
    else:
        x = a[i+1] - a[i]
    if x>j:
        j=x
    i+=1

print(k-j)

Solution (Python):
K, N = map(int,input().split())
A = [0]*N
tmp = input().split()
for a in range(N):
    A[a] = int(tmp[a])

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

print(K-max(diff))

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
l = [k - a[-1] + a[0] if x == n - 1 else a[x+1] - a[x] for x in range(n)]

print(sum(l) - max(l))

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

distance = [a[0] + (k - a[-1])]

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

distance = sorted(distance)

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

Solution (Python):
import numpy as np
def resolve():
    K,N = map(int,input().split())
    A = np.array(list(map(int,input().split())))
    Adiff = np.diff(A)
    max_gap = max(max(Adiff), A[0] + K - A[N-1])
    print(K-max_gap)
resolve()

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

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

print(s)

Solution (Python):
import numpy as np

k,n = map(int,input().split())
a = list(map(int,input().split()))
temp =a[0]
a.append(temp)
base = np.empty(n)

for i in range(n):
  if i == n-1:
    base[i] = (k-a[i]) + a[0]
    break
    
  base[i] = a[i+1] - a[i]
  
print(k - int(np.max(base)))

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

ans = []

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

print(k - max(ans))

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

tmp = []
for i in range(1, n):
    tmp.append(clist[i] - clist[i-1])
    
tmp.append(k- clist[n-1] + clist[0])

print(k -max(tmp))

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

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

print(K-num)

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

diff_A += [(N, K - A[i] + A[0])]
sorted_diff_A = sorted(diff_A, key=lambda x: x[1], reverse=True)
print(K - sorted_diff_A[0][1])

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

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

distance.append(k-sum(distance))


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

Solution (Python):
import sys




from collections import deque

from fractions  import gcd

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

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

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

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

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

KUKAN = [0] * N

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

print(str(sum(KUKAN) - max(KUKAN)))

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

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

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

    if max < dis:
        max = dis


print(K - max)

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

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, 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):
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

Solution (Python):
k,n=map(int,input().split())
houses=list(map(int,input().split()))
houses.sort()
maxx=-float('INF')
for i in range(n-1):
    if houses[i+1]-houses[i]>maxx:
        maxx=houses[i+1]-houses[i]

maxx=max([maxx,k-(houses[-1]-houses[0])])
print(k-maxx)

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

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

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

print(K - max(S))

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

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




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

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

print(k - max(s))

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

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

print(K - max(S))

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

 
A = list(map(int, input().split()))
l = 0
A.append(A[0]+K)
for i,aa in enumerate(A):
    if(i<len(A)-1):
        l = max(l, A[i+1]-aa)
print(K-l)

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

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


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


print(min(far_list))

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

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


Bn = sorted(An)


C = K-(Bn[N-1]-Bn[0])


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

print(K-C)

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

max = 0
for i in range(len(a)-1):
  p = abs(a[i]-a[i+1])
  max = p if max < p else max
print(N-max)

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

Solution (Python):

















def main():

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

    if N == 2:
        d = abs(A[0]-A[1])
        print(min(d, K-d))
    else:
        max_d = K - A[N-1] + A[0]
        for i in range(N - 1):
            max_d = max(max_d, abs(A[i] - A[i+1]))
        print(K - max_d)


if __name__ == '__main__':
    main()

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
lists = []
for i in range(n-1):
    lists.append(abs(a[i] - a[i+1]))
lists.append(k - a[-1] + a[0])
jyogai = lists.index(max(lists))
cnt = 0
for i in range(len(lists)):
    if i != jyogai:
        cnt += lists[i]
print(cnt)

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

Solution (Python):
K, N = map(int, input().split())
As = list(map(int, input().split()))
ds = []
for i in range(N):
  if i == 0:
    d = K+As[i]-As[i-1]
  else:
    d = As[i]-As[i-1]
  ds.append(d)

r = K-max(ds)
print(r)

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

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

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

print(K-max(B))

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

d_max = A[0] + K - A[-1]
for a1, a2 in zip(A, A[1:]):
    d_max = max(d_max, a2 - a1)

print(K - d_max)