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):
def solve():
  raw = input().split()
  K = int(raw[0])
  N = int(raw[1])
  vals = [None] * N
  ans = 99999999
  raw = input().split()
  for i in range(N):
    vals[i] = int(raw[i])
  for i in range(N):
    cc = -1
    if i == 0:
      cc = vals[N-1] - vals[0]
    else:
      cc = vals[i-1] + K - vals[i]
    ans = min(ans, cc)
  print(ans)
solve()

Solution (Python):
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list()
i = 0
while i + 1 < len(b):
    c.append(b[i + 1] - b[i])
    i += 1
c.append(a[0] - b[len(b) - 1] + b[0])
print(sum(c) - max(c))

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

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

target=max(dist)
print(K-target)

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

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

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

for i in range(N-1) :
  dist = A[i+1] - A[i]
  if dist > maxDist :
    maxDist = dist
    
print(K-maxDist)

Solution (Python):
k, n = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.sort()
r_list = list()
for index, a in enumerate(a_list):
	if index == n - 1:
		r_list.append((k - a) + a_list[0])
	else:
		r_list.append(a_list[index+1] - a)
r = k - max(r_list)
print(r)

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

if A[-1]>A[0]:
    longdis = abs(K-A[-1]+A[0])
else:
    longdis = abs(A[-1]-A[0])

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

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]

last = a[0] + k - a[n-1]
if last > ans:
    ans = last

print(k-ans)

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline

K, N, *A = map(int, read().split())
A.append(K + A[0])
d = [j - i for i, j in zip(A, A[1:])]

print(K - max(d))

Solution (Python):
import sys


input = sys.stdin.readline


def main():
  k, n = map(int, input().rstrip().split())
  a = [int(_) for _ in input().rstrip().split()]
  
  dist = [(a[i] - a[i - 1]) % k for i in range(n)]
  
  print(k - max(dist))


if __name__ == "__main__":
  main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline

K, N, *A = map(int, read().split())
A.append(K + A[0])
D = 0
for i, j in zip(A, A[1:]):
    d = j - i
    if d > D:
        D = d

print(K - D)

Solution (Python):

circ_length, n_house = map(int, input().split())
places = sorted(list(map(int, input().split())))

delta_places = []
for i in range(n_house):
  if i == 0:
    d = circ_length - places[-1] + places[0]
  else:
    d = places[i] - places[i-1]
  delta_places.append(d)

result = circ_length - max(delta_places)

print(result)

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

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

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

mx = k-a[-1] +a[0]

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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    K,N=MI()
    A=LI()
    diff=[0]*N
    for i in range(N-1):
        diff[i]=A[i+1]-A[i]
        
    diff[-1]=A[0]+(K-A[-1])
    print(K-max(diff))

main()

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

k,n = map(int,readline().split())
lst1 = list(map(int,readline().split()))

dist = 0

for i in range(n-1):
    dist = max(dist,abs(lst1[i]-lst1[i+1]))

dist = max(dist,k-lst1[-1]+lst1[0])

print(k-dist)

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

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):
    ans = min(ans, K-A[i+1]+A[i])
print(ans)

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


max = 0
for i in range(1,n):
    if lista[i]-lista[i-1]>=max:
        max = lista[i]-lista[i-1]

if k-lista[n-1]+lista[0]>=max:
    max = k-lista[n-1]+lista[0]

print(k-max)

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

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

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

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

max = 0
for i in range(len(a_list)):
    if i == len(a_list)-1:
        length = (k-a_list[i])+a_list[0]
    else:
        length = a_list[i+1] - a_list[i]
    if length > max:
        max = length
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 == n - 1:
        l = k + A[0] - A[i]
        dis[i] = l
    else:
        l = A[i + 1] - A[i]
        dis[i] = l

print(k - max(dis))

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


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

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

r = []

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

ind = r.index(max(r))

if ind == n-1:
    print(a[n-1] - a[0])
else:
    print(k - a[ind+1] + a[ind])

Solution (Python):
k, n = map(int, input().split())
lst = list(map(int, input().split()))
min_num = k
for i in range(len(lst)):
    if i == 0:
        tmp = min(lst[0] + (k - lst[1]), lst[-1] - lst[0])
    elif i == n - 1:
        tmp = min(lst[-1] - lst[0], (k - lst[-1]) + lst[n - 2])
    else:
        tmp = min(lst[i] + (k - lst[i + 1]), (k - lst[i]) + lst[i - 1])
    if tmp < min_num:
        min_num = tmp

print(min_num)

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

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

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

print(K-max(dis))

Solution (Python):
import numpy as np
import sys
readline = sys.stdin.buffer.readline
K, N = map(int, readline().split())
A = list(map(int,input().split()))
N1= A[0]+K-A[N-1]

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

print(K-N1)

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

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

b.append(a[0]+C-a[N-1])

print(sum(b)-max(b))

Solution (Python):
K,N=list(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 = list(map(int, input().split()))
list_len = len(A_list)


min_ans = 10**18


for i in range(list_len):
    mae_i = 0
    if i==list_len-1:
        mae_i = 0
    else:
        mae_i = i + 1

    kyori = 0
    if A_list[mae_i]-A_list[i]<0:
        kyori = K - (A_list[mae_i] - A_list[i] + K)
    else:
        kyori = K - (A_list[mae_i] - A_list[i])
    min_ans = min(min_ans, kyori)


print(min_ans)

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

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

print(k-max_dis)

Solution (Python):
k, n = [int(i) for i in input().split()]
a = [int(i) for i in 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()))
kyori=[]
kyori.append(A[0]+k-A[n-1])
for i in range(1,n):
    kyori.append(abs(A[i-1]-A[i]))
print(sum(kyori)-max(kyori))

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

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

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

prev = arr[0]
maxi = 0
sums = 0
for a in arr[1:]:
    sums += a - prev
    maxi = max(maxi, a - prev)
    prev = a

sums += k - (arr[-1] - arr[0])
maxi = max(maxi,  k - (arr[-1] - arr[0]))
print(sums - maxi)

Solution (Python):
import numpy as np
K, N = map(int, input().strip().split())
A = list(map(int, input().strip().split()))


dA=np.diff(A)
dA=np.append(dA,A[0]+K-A[-1])
skip_dist=max(dA)
ans=K-skip_dist

print(ans)

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

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

print(ans)

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

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

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

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

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

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

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

Solution (Python):

import numpy as np
KN = input().split()
K = int(KN[0])
N = int(KN[1])


A = input().split()
A_np = np.append(np.array(A, dtype=np.int64), int(A[0])+K)
A_np_lag = np.append(A_np[1:], A_np[0])
A_diff = A_np_lag[:N] - A_np[:N]

print(A_diff.sum() - A_diff.max())

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

Solution (Python):
K, N = map(int, input().split())
A = input().split()
A = [int(val) for val in A]
A_org = min(A)
A = [val - A_org for val in A]
A.sort()
dist = []
for i in range(len(A)):
  if i != len(A)-1:
    dist.append(A[i+1]-A[i])
  else:
    dist.append(K-A[i])
print(K - max(dist))

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] = K + A[i - N]

    l = 0
    for i in range(N):
        tmp = B[i+1] - B[i]
        l = max(l, tmp)

    ans = K - l
    print(ans)



if __name__ == "__main__":
    main()

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

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

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

anti_clockwise_dist = A[0] - (A[-1] - K) 

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

dist = max(dist, anti_clockwise_dist)

print(K - dist)

Solution (Python):

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


kyori = [ A[i+1] -A[i] for i,v in enumerate(A[:-1]) ]
kyori[-1] = kyori[-1] + K 
kyori.append(A[0] -A[-1] ) 
x_saisyo_saigo = kyori[-2] +kyori[-1]
kyori.append(x_saisyo_saigo) 


kyori_min = K - max(kyori)
print(kyori_min)

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


B.sort()
B.pop()

ans = sum(B)
print(ans)

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

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

print(d)

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

i=0
dist=-1
while i < n:
  dist=max(dist,a[i+1]-a[i])
  i += 1

print(str(k-dist))

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

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

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

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

distances = [K-sum(distances)] + distances
distances.remove(max(distances))

print(sum(distances))

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

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

memo = []

for i in range(1,n):
    memo.append(abs(a[i]-a[i-1]))
memo.append(k-a[-1]+a[0])
memo.sort()

ans = 0

for i in range(len(memo)-1):
    ans += memo[i]

print(ans)

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

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])
ans = k - l
print(ans)

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

max_dist = 0
for i in range(n):
    next = (i+1) % n
    dist = a[next] - a[i]
    if dist < 0: dist+= k
    max_dist = max(max_dist, dist)
print(k - max_dist)

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

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



B = []
for i in range(N):
    B.append(A[i+1] - A[i])
B = sorted(B,reverse = False)

print(sum(B) - max(B))

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

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

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

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

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

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

print(k - distance)

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

Solution (Python):



from math import *
from copy import *
from string import *				
from random import *				
from bisect import *				
from sys import stdin				
from sys import maxsize
from operator import *				
from itertools import *
from collections import Counter		
from collections import defaultdict 

'''
20 3
5 10 15
'''

def solve(l,K):
	n = len(l)
	l += [l[0] + K]
	l = sorted(l)
	maxx = 0
	for i in range(n):
		maxx = max(maxx,l[i+1]-l[i])
	return K - maxx
	

K,N = list(map(int, stdin.readline().rstrip().split()))
l = list(map(int, stdin.readline().rstrip().split()))
ans = solve(l,K)
print(ans)

Solution (Python):
K, N = map(int, input().split())
A = [int(n) for n in input().split()]
distances = []
for i in range(N):
    if i == 0:
        distances.append(A[0] + (K - A[N - 1]))
    else:
        distances.append(A[i] - A[i - 1])
distances.sort()
distances.pop(N - 1)
print(sum(distances))

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

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

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

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

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

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

rec = list()

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

for i, _ in enumerate(A[:-1]):
    rec.append(A[i+1] - A[i])

sum = 0
for r in rec:
    sum += r

max_r = max(rec)
print(sum - max_r)

Solution (Python):
P, N = list(map(int, input().split()))
arr = list(map(int, input().split()))
mn = 100000000
l, r = 0, len(arr) - 1
for i in range(N):
    if (arr[r] - arr[l]) < mn:
        mn = arr[r] - arr[l]
    arr[l] += P
    l = (l + 1) % N
    r = (r + 1) % N
print(mn)

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

Solution (Python):
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [min(abs(a[i-1]-a[i]), abs(k-(a[i-1]-a[i]))) for i in range(n)]
print(sum(b)-max(b))

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

for i in range(1,n):
    if i!=pos:
        s+=a[i]-a[i-1]
if pos!=0:
    s+=k-a[-1]+a[0]
print(s)

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, *A = map(int, open(0).read().split())
    A.append(K + A[0])
    longest_dist = max(j - i for i, j in zip(A, A[1:]))
    ans = K - longest_dist
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

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

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

Solution (Python):
K,N = 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],i])
dist.sort()
ind = dist[-1][1]
ans = min(A[-1]-A[0],A[ind]+abs(K-A[ind+1]))

print(ans)

Solution (Python):
K,N = map(int,input().split())
A_s = list(map(int,input().split()))
MAX = max(list(map(lambda i:(A_s[(i+1)%N]-A_s[i])%K, range(N))))
print(K-MAX)

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]



def main():
    m=LI()
    k=m[0]
    n=m[1]
    a=LI()
    for i in range(n):
        if i!=0:
            a[i]-=a[0]
    a[0]=0;
    ans=k;
    for i in range(n-1):
        ans=min(ans, k-(a[i+1]-a[i]))
    ans=min(ans, a[n-1])
    print(ans)
main()

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

lists_slic = lists[1:] + lists[0:1]


a = 0
for i in range(len(lists)):
    if lists_slic[i] - lists[i] < 0:
        b = lists_slic[i] - lists[i] + K
        if b > a:
            a = b
    else:
        if (lists_slic[i] - lists[i])>a:
            a = lists_slic[i] - lists[i]

c = K - a

print(c)

Solution (Python):
K, N = map(int, input().split())
A = [int(i) for i in 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):
x,y = map(int,input().split())
li = list(map(int,input().split()))	
minmin = x - (li[0] + x - li[y-1])
for i in range(len(li)-1):
    if minmin > x - (li[i+1] - li[i]):
        minmin =  x - (li[i+1] - li[i])
    else:
      continue
print(minmin)

Solution (Python):
k,n = map(int,raw_input().split())
a = map(int,raw_input().split())
b = [0]*n
for i in range(1,n):
    b[i-1] = a[i]-a[i-1]
b[n-1] = a[0]+k-a[n-1]
b.sort()
b.pop(n-1)
print sum(b)

Solution (Python):
K, N = 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] + K - A[-1])
print(K - max(s))

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

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

dst_rev = sorted(dst)

ans = 0

for i in range(n-1):
  ans += dst_rev[i]

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(K - A[N-1] + A[0])
C = sum(B) - max(B)
print(C)

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):
K,N = map(int,input().split())
data = list(map(int,input().split()))

min_num = data[-1] - data[0]

for _ in range(N-1):
  temp = data[_] + K-data[_+1]
  if min_num > temp:
    min_num = temp
print(min_num)

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

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

print(k - max(segments))

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

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

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

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

    else:
        x = l[i+1]-l[i]

    if x > m:
        m = x

print(k-m)

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

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

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

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

print(k-mx)

Solution (Python):
p, n = map(int,input().split())
l = list(map(int,input().split()))
l.sort()
val = l[-1]-l[0]
for i in range(1,n):
    val = min(val, l[-1] - l[i] + p-l[-1]+l[i-1])
print(val)

Solution (Python):
import sys
K,N=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

L=[]
prev=None
for a in A:
    if prev is None:
        prev=a
    else:
        L.append(a-prev)
        prev=a
else:
    L.append(K-A[-1]+A[0])

print K-max(L)

Solution (Python):
k,n = map(int,raw_input().split())
a = map(int,raw_input().split())
b = []
for i in range(n-1):
    b.append(a[i+1]-a[i])
b.append(a[0]+k-a[n-1])
b.sort()
b.pop(n-1)
print sum(b)

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

per_dist = list(A[i+1]-A[i] for i in range(0,N-1))
last_dist = A[0] + K - A[N - 1]
type(last_dist)
per_dist.append(last_dist)
max_dist = max(per_dist)
print(K - max_dist)

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

if __name__ == '__main__':
    main()

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

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

print(min(ans))

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

min_num = data[-1] - data[0]

for _ in range(N-1):
    min_num = min([min_num,data[_] + K-data[_+1]])
print(min_num)

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

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

D = [j - i for i, j in zip(A, A[1:])]

ans = k - max(D)

print(ans)

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

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

print(ans)

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

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

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

tmp.remove(max(tmp))

print(sum(tmp))

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

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

m = A[N-1] - A[0]

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

    if n < m:
        m = n

print(m)

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

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

Solution (Python):

k, n = map(int,input().split())
a = input().split()
a.append(str(int(a[0]) + k))
distance = []
for i in range(n):
    preh, nexh = int(a[i]),int(a[i+1])
    d = abs(nexh - preh)
    distance.append(d)
b = abs(k - max(distance))
print("{}".format(b))

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):
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n, m = input_list()
  a = input_list()
  b = []
  for i in range(m):
    if i == 0:
      b.append((n - a[-1]) + a[0])
      continue
    b.append(a[i] - a[i-1])
  bm = max(b)
  print(sum(b) - bm)
import math
import fractions
import collections
import itertools
from functools import reduce
main()