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())
b=sorted(map(int,input().split()))
c=[0]*n
for i in range(n-1):
    c[i]=b[i+1]-b[i]
c[n-1]=b[0]+k-b[n-1]
d=max(c)
print(k-d)

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

m.sort(reverse=True)
kyori = []
for x in range(n[1]-1):
    kyori.append(m[x]-m[x+1])

kyori.append((m[len(m)-1]+n[0])-m[0])
print(sum(kyori)-max(kyori))

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

e=k-L[n-1]+L[0]
c.append(e)

print(k-max(c))

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

print(k-max(s))

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

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

print(K-max)

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

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

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

max=0
b=0
for i in range(N-1):
    b=a[i+1]-a[i]
    if(b>=max):
        max=b
    
m=K-a[i+1]+a[0]

if(m>max):
    max=m

kyori=K-max

print(kyori)

Solution (Python):
K,N=map(int, input().split())
Alist=list(map(int, input().split()))
diflist = []
def whichside(K,A1,A2):
    diff = A2-A1
    diff = K-diff if K-diff<diff else diff
    return diff
diflist.append(whichside(K,Alist[0],Alist[-1]))
for i in range(1,len(Alist)):
    diff = whichside(K,Alist[i-1],Alist[i])
    diflist.append(diff)
print(K-max(diflist))

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

    for i in range(N):
        if i == N - 1:
            d = A[0] + K - A[-1]
            dlist.append(d)
        else:
            d = abs(A[i] - A[i+1])
            dlist.append(d)
    dlist.sort()
    print(sum(dlist[:N-1]))

if __name__ == "__main__":
    main()

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

d_max = K-A[N-1]+A[0]
sum = d_max

for i in range(N-1):
    d = abs(A[i]-A[i+1])
    d_max = max(d,d_max)
    sum += d

print(sum-d_max)

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

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

print(K-ans)

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

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

print(K - max_dist)

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

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

print(K - max(A_dist))

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

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

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

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

a_list = list(map(int, input().split()))
sa_list = []
for i in range(n-1):
    sa_list.append(a_list[i+1]-a_list[i]) 

sa_list.append(k-a_list[-1]+a_list[0])



print(k - max(sa_list))

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

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

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

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

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

print(K-maxi)

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

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

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

max_d = 0
for i in range(1, N+2):
    d = A[i]-A[i-1]
    dist.append(d)

    if d > max_d:
        max_d = d

print(K-max_d)

Solution (Python):
k,n = map(int,input().split())
a = input().split()
s = 0
t = 0
while t < n:
  x = int(a[t-1])
  y = int(a[t])
  if y-x > s:
    s = y-x
  t += 1
l = int(a[0])+k - int(a[-1])

print(k-max(s,l))

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

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


c=k-li[n-1]+li[0]
dis=[c]
i=0
for i in range(n-1):
  a=li[i+1]-li[i]
  dis+=[a]

max_dis=max(dis)
ans=k-max_dis

print(ans)

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

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

d.append((K-A[N-1])+A[0])
print(K-max(d))

Solution (Python):
K,N=map(int, input().split())
As=[int(i) for i in input().split()]
ans=[]
for num in range(0,len(As)-1):
    ans.append(As[num+1]-As[num])
ans.append((K-max(As))+min(As))
ans.sort()
print(sum(ans[:-1]))

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

Solution (Python):
import sys;input=lambda:sys.stdin.readline().rstrip();aint=lambda:int(input());ints=lambda:list(map(int,input().split()))
import math;floor,ceil=math.floor,math.ceil
from collections import deque
Yes=lambda b:print('Yes')if b else print('No');YES=lambda b:print('YES')if b else print('NO')
is_even=lambda x:True if x%2==0 else False

def main():
    k,n=ints();a=ints()
    a.append(a[0]+k);diff=[a[i+1]-a[i]for i in range(n)]
    print(k-max(diff))

if __name__ == '__main__':
    main()

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

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

d0=K-A[N-1]+A[0]
if max(d)>d0:
  print(K-max(d))
else:
  print(K-d0)

Solution (Python):
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))
A_dist = [0]*N

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


A_dist_max = max(A_dist)
print(K-A_dist_max)

Solution (Python):
N = [int(e) for e in input().split()]
nums = [int(t) for t in input().split()]
ans = 0
if(nums[-1] > N[0]//2):
    Max = (N[0]-nums[-1]) + nums[0]
    t = 0
else:
    Max = nums[-1] - nums[0]
    t = 0
for i in range(N[1]-1):
    if(nums[i+1] - nums[i])>Max:
        Max = nums[i+1] - nums[i]
        t = i
if(t==0):
    for i in range(N[1]-1):
        ans += nums[i+1] - nums[i]
else:
    for k  in range(0,t):
        ans += nums[k+1] - nums[k]
    for j in range(t+1,N[1]-1):
        ans += nums[j+1] - nums[j]
    if(nums[-1] > N[0]//2):
        ans += (N[0]-nums[-1]) + nums[0]
    else:
        ans += nums[-1] - nums[0]
print(ans)

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

x=0
for i in range(len(a)-1):
    dis=abs(a[i+1]-a[i])
    x=max(x,dis)

print(k-x)

Solution (Python):

def main():
  k, n = map(int, input().split())
  dis = tuple(map(int, input().split()))

  diff = [dis[i + 1] - dis[i] for i in range(n - 1)]
  diff.append(k - dis[-1] + dis[0])

  max_dis = 0
  max_idx = 0
  for i, d in enumerate(diff):
    if d > max_dis:
      max_dis = d
      max_idx = i

  diff[max_idx] = 0

  print(sum(diff))

main()

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
m = a[0]+(k-a[n-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())
d=list(map(int,input().split()))
res=k-d[n-1]+d[0]
for i in range(0,n-1):
    if d[i+1]-d[i]>res:
        res=d[i+1]-d[i]
print(k-res)

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

print(k-dis[-1])

Solution (Python):
import numpy as np
k, n = map(int, input().split())
a = np.array([int(i) for i in input().split()])
a = np.concatenate([a, [k + a[0]]])
diff_a = np.diff(a)
diff_a = sorted(diff_a)
print(np.sum(diff_a[:-1]))

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

distance = []

for i in range(0,len(A)-1):
    tem = A[i+1] - A[i]
    distance.append(tem)

tem = A[0] + K - A[-1]
distance.append(tem)

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

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

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

print(sum(x) - max(x))

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

Solution (Python):
'''
メモ

時計回りに、隣接する家までの距離を計算する。
　移動先の家の方が、原点からの距離が大きい場合、
 　差を計算する
 小さい場合、
 1周の距離を用いて、差を計算する
 
上記で計算した距離から最大距離を除いた和を出力する

'''

K, N = map(int, input().split())
distance_from_north = [int(i) for i in input().split()]

distance_between_house = []

for i, distance in enumerate(distance_from_north):
  if i == len(distance_from_north) - 1:
    distance_between_house.append(distance_from_north[0] + (K - distance))
    break
  
  distance_between_house.append(distance_from_north[i + 1] - distance_from_north[i])
  
distance_between_house = sorted(distance_between_house)
print(sum(distance_between_house[:-1]))

Solution (Python):
def solve(a, k):
    a.sort()
    min_dist = k
    for i in range(len(a)):
        start = a[i]
        end = a[(i + len(a) - 1) % len(a)]
        if start > end:
            end += k
        min_dist = min(min_dist, end - start)
    return min_dist


if __name__ == "__main__":
    k, n = map(int, input().split())
    a = [int(x) for x in input().split()]
    print(solve(a, k))

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

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

dist = []

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

print(k-max(dist))

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

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

ad_max = max(ad)
ad_max_ind = ad.index(ad_max)

if ad_max_ind == (n-1):
    ans = a[n-1] - a[0]
if ad_max_ind != (n-1):
    ans = k - a[ad_max_ind+1] + a[ad_max_ind]
    
print(ans)

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





K,N = inputIL()
l = inputIL()
l.append(l[0]+K)

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

m = max(dist)

print(K-m)

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

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

print(K - max(dist))

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

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

print(k-max(l))

Solution (Python):
a,b=map(int,input().split())
c=list(map(int,input().split()))
d=[]
for i in range(b-1):
    d.append(c[i+1]-c[i])
d.append(a+c[0]-c[b-1])
d.sort()
del d[-1]
print(sum(d))

Solution (Python):
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0] * (2 * N)
    for i in range(2 * N):
        if i < N:
            B[i] += A[i]
        else:
            B[i] = A[i-N] + K

    ans = float('inf')

    for i in range(N):
        ans = min(ans, B[i + N - 1] - B[i])

    print(ans)



if __name__ == "__main__":
    main()

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

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

l = list(map(int, input().split()))
m = [0]*n
count = 0

for i in range(n-1):
    m[count] = l[i+1] - l[i]
    count+=1
m[n-1] = k - l[n-1]+ l[0]

print(k - max(m))

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

b_l = [a_l[i]-a_l[i-1] for i in range(n)]
b_l[0] += k
b_l.sort()
print(k-b_l[-1])

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

Solution (Python):
K,N = map(int,input().split())
A = list(map(int, input().split()))
max = K + A[0] - A[N-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):
from sys import stdin
import math

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


A.append(N + A[0])

max_diff = 0

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

print(N - max_diff)

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

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



print(sum(d) - max(d))

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

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

for i in range(len(A)):
    if i+1 == len(A):
        break
    dis.append(A[i+1] - A[i])


        
print(K - max(dis))

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

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

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

diff = [a[i + 1] - a[i] for i in range(len(a)-1)]
diff.append(K - a[len(a)- 1] + a[0])
diff.sort()
diff.pop(-1)
print(sum(diff))

Solution (Python):

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

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

Solution (Python):
n,k = [int(s) for s in input().split()]
arr = [int(s) for s in input().split()]
arr.sort()
maxi = 0
for i in range(1,len(arr)):
    maxi = max(maxi,arr[i]-arr[i-1])
maxi = max(maxi,n-arr[k-1]+arr[0])
print(n-maxi)

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

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

print(min(b))

Solution (Python):
K,N  = map(int,input().split())
Alist = list(map(int,input().split()))
p = 10**8
p = min(max((K-abs(Alist[0]-Alist[N-1])),abs(Alist[0]-Alist[N-1])),max((K-abs(Alist[0]-Alist[1])),abs(Alist[0]-Alist[1])),p)
for i in range(1,N-1):
    p = min(p,max((K-abs(Alist[i]-Alist[i-1])),abs(Alist[i]-Alist[i-1])),max((K-abs(Alist[i]-Alist[i+1])),abs(Alist[i]-Alist[i+1])))
p = min(p,max((K-abs(Alist[N-1]-Alist[N-2])),abs(Alist[N-1]-Alist[N-2])),max((K-abs(Alist[N-1]-Alist[0])),abs(Alist[N-1]-Alist[0])))
print(p)

Solution (Python):
k,n=map(int,input().split())
nums=list(map(int,input().split()))
min=10**7
for i in range(n):
    a=nums[n-1+i]-nums[i]
    if a<min:
        min=a
    nums.append(nums[i]+k)
print(min)

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

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


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

    
if __name__ == '__main__':
    solve()

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

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

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

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

ans = 0

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

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

print(k - ans)

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

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

distance_list = []
for i, j in zip(A_list[0:-1], A_list[1:]):
    distance_list.append(j - i)
    
max_distance = max(distance_list)
distance_last_to_first = K - A_list[-1] + A_list[0]
max_distance = max(max_distance, distance_last_to_first)

ans = K - max_distance
print(ans)

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

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

print(sum(dis)-max(dis))

Solution (Python):
il1 = input().split()
il2 = input().split()

K = int(il1[0])
N = int(il1[1])
A = [int(i) for i in il2]

import copy

fixed_A = copy.copy(A)

for i in A:
    fixed_A.append(i+K)

ans_list =[]

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

ans_list.sort()

print(ans_list[0])

Solution (Python):


import numpy as np

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

A_diff = np.diff(A, n= 1).tolist()
A_diff.append(K+A[0]-A[-1])
print(K-max(A_diff))

Solution (Python):


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

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

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

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

print(K-max)

Solution (Python):
import numpy as np
K, N = map(int, input().split())
A = np.array([int(i) for i in input().split()])
B = A[1:]
B = np.insert(B, N - 1, A[0] + K)
X = B - A
print(K - max(X))

Solution (Python):
import numpy as np
K, N = map(int, input().split())
A = np.array([int(i) for i in input().split()])
B = A[1:]
B = np.insert(B, N - 1, A[0] + K)
X = B - A
print(sum(X) - max(X))

Solution (Python):
K, N = map(int,input().split())
A = list(map(int, input().split()))
a = A[0]
sub = []
for _ in range(N):
    p = A.pop()
    sub.append(K-p)
    K = p
sub[0] += a
sub.remove(max(sub))
print(sum(sub))

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

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

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

print(k-maxN)

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

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

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

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

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

d=[]
for i in range(b-1):
  x=c[i+1]-c[i]
  d.append(x)

x=c[0]+a-c[b-1]
d.append(x)
d.sort()
print(sum(d[:b-1]))

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

Solution (Python):
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)


inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import deque, defaultdict, Counter
import math
import sys
sys.setrecursionlimit(10 ** 7)


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



def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


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


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    k, n = wi()
    A = wi()
    B = A.copy()
    C = A + B
    
    m = 0
    for i in range(n):
        m = max(m, C[i+1] - C[i])
        if i == n-1:
            m = max(m, k - C[i]+C[0])
    print(k-m)


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a = np.array(a)
print(k - np.maximum((a[1:] - a[:-1]).max(), k - a[-1] + a[0]))

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

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

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

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

Solution (Python):
a, b = map(int, input().split())
*A, = map(int, input().split())
c = []
for i in range(1, b):
    c.append(A[i]-A[i-1])
c.append(a-sum(c))
print(a-max(c))

Solution (Python):


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

k,n = na()
a = na()
b = [0]*n

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


print(k-max(b))

Solution (Python):


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

minDist=pow(10,6)

for i in range(N-1):
    right=0
    left=0
    if i == 0:
        right = aList[N-1] - aList[0]
        left  = K - aList[1] + aList[0]
    elif i == N-1:
        right = K + aList[N-2] - aList[N-1]
        left  = aList[N-1] - a[0]
    else:
        right = K + aList[i-1] - aList[i]
        left  = K - aList[i+1] + aList[i]
    minDist =min(minDist,min(right,left))

print(minDist)

Solution (Python):
import sys
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
for test in range(int(input())):
'''
inf = 100000000000000000  

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

Solution (Python):


nim, mike = map(int, input().split())

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

A.append(A[0]+nim)

high=0

for i in range(mike):
  if A[i+1]-A[i]>high:
    high=A[i+1]-A[i]
print(nim-high)

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

diff = []
for i in range(len(A)):
    if i < N-1:
        diff.append(A[i+1] - A[i])

diff = diff + [K-A[-1]+A[0]]
longest_interval = diff.index(max(diff))
diff.pop(longest_interval)
ans = sum(diff)

print(ans)

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

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

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

housekyori = []
for i,a in enumerate(A):
    if i == N-1:
        housekyori.append(K-a+A[0])
    else:
        housekyori.append(A[i+1]-a)
print(sum(housekyori)-max(housekyori))

Solution (Python):
import numpy as np

k, n = map(int, input().split())
l = list(map(int, input().split()))
a = np.array(l)
b = np.delete(np.append(a,k+a[0]), 0)

sa = b-a
sa_dash = np.delete(sa, np.argmax(sa))
print(np.sum(sa_dash))

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

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