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

Solution (Python):

import sys
import itertools
import fractions
import copy
import bisect


from operator import itemgetter
from sys import stdin

from operator import mul
from functools import reduce
from collections import Counter


N=3
M=3
i=0
j=0
k=0
n=3
r=1
a=[0]*5
diff=[]
newlist=[]
K,N=map(int, input().split())  
number_list=list(map(int, input().split(" ")))
newlist=sorted(number_list)
for i in range(N-1):
    dis=newlist[i+1]-newlist[i]
    diff.append(dis)
diff.append(newlist[0]+K-newlist[N-1])
print(K-max(diff))






"1行1つの整数を入力を取得し、整数と取得する"




"12 21 332 とか入力する時に使う"
"1行に複数の整数の入力を取得し、整数として扱う"

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

for i in number_list:
    print(number_list[i-1])

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'''
x=[]
y=[]
for i in range(N):
    x1, y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
print(x)
print(y)
"複数行に2数値を入力する形式 x座標とy座標を入力するイメージ"
'''

'''
mixlist=[]
for i in range(N):
    a,b=input().split()
    mixlist.append((int(a),b))
print(mixlist)
"複数行にintとstrを複合して入力するやつ，今回はリスト一つで処理している"
'''

'''



"正方行列にstr型の値を入力"
'''




'''
入力
1 2
4 5
7 8
出力結果
[[1, 2], [4, 5], [7, 8]]
'''
"列数に関して自由度の高いint型の値を入力するタイプの行列"


'''
table = [[int(i) for i in input().split()] for m in range(m)]

print(type(N))
print(N)
print(type(M))
print(M)

print(type(table))
print(table)
'''











'''
入力
1234
出力
1
[[1, 2, 3, 4]]
'''




'''
word_list= input().split(" ")
print(word_list[0])
"連続文字列の入力"
"qw er ty とかの入力に使う"
"入力すると空白で区切ったところでlistの番号が与えられる"
'''



'''
A, B, C=stdin.readline().rstrip().split()
print(A)
"リストではなく独立したstr型を入れるなら以下のやり方でOK"
'''




"aという変数に入っているものを大文字にして出力"

















'''
def cmb(n,r):
    
    r= min(n-r,r)
    
    if r == 0: return 1
    over = reduce(mul, range(n, n-r, -1))
    
    
    

    under = reduce(mul, range(1, r+1))
    
    
    
    

    return over // under
    
    
    
    
a = cmb(n, r)

'''

'''




for i in a.most_common(n):print(i)





 


'''









'''
bisect.bisect_left(a, x, lo=0, h=len(a))
引数
a: ソート済みリスト
x: 挿入したい値
lo: 探索範囲の下限
hi: 探索範囲の上限
(lo, hiはスライスと同様の指定方法)
bisect_leftはソートされたリストaに対して順序を保ったままxを挿入できる箇所を探索します。leftが示す通り、aにすでにxが存在している場合は、挿入箇所は既存のxよりも左側になります。また、lo, hiを指定することで探索範囲を絞り込むことも可能です。デフォルトはaの全体が探索対象です。
'''

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

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

print(solve())

Solution (Python):



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

mlen=K-A[N-1]+A[0]
for ii in range(N-1):
    len2=A[ii+1]-A[ii]
    mlen=max(mlen,len2)
ans=K-mlen


print(ans)

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

    max_interval = 0
    for i in range(N-1):
        tmp_interval = A_list[i + 1] - A_list[i]
        if max_interval < tmp_interval:
            max_interval = tmp_interval

    first_interval = A_list[0] + (K - A_list[N-1])

    if max_interval < first_interval:
        max_interval = first_interval

    ans = K - max_interval

    print(ans)

if __name__ == "__main__":
    resolve()

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):
k, n = map(int, input().split())
A = list(map(int, input().split()))
degree_max = 0

for i in range(len(A)):

    if i+1 == len(A):
        degree = (k - A[i]) + A[0]

        if degree_max < degree:
            degree_max = degree
    else:
        degree = A[i+1] - A[i]
        if degree_max < degree:
            degree_max = degree
ans = k - degree_max
print(ans)

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[-1] = K-A[-1]+A[0]
B.sort()
print(sum(B[:-1]))

Solution (Python):
kn=input().rstrip().split(" ")
k=int(kn[0])
n=int(kn[1])
numbers=input().rstrip().split(" ")
ans=0
for i in range(n-1):
    delta=int(numbers[i+1])-int(numbers[i])
    if delta>=ans:
        ans=delta
lasdelta=int(numbers[0])+k-int(numbers[n-1])
if lasdelta>=ans:
    ans=lasdelta

print(k-ans)

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

for i in range(n - 1):
    if i == 0:
        j = n - 1
        r = min(r, A[j] - A[i])
    else:
        j = i + 1
        r = min(r, A[i] + k - A[j])
print(r)

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(K-A[N-1]+A[0])

print(K-max(dis))

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

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

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

Solution (Python):

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

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


max_value = max(lag)
max_index = lag.index(max_value)
lag.pop(max_index)
print(sum(lag))

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


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):

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

MaxD = 0
B = sorted(A)
for i in range(N-1):
    D = B[i+1] - B[i]
    if D > MaxD:
        MaxD = D

if K - B[N-1] + B[0] > MaxD:
    MaxD = K-B[N-1]+B[0]

print(int(K-MaxD))

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

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

d.append(k-c[n-1]+c[0])
max = max(d)
sum = sum(d)
print(sum-max)

Solution (Python):


K, N = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
dist = []
ans = []

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

s = sum(dist)

for i in range(N):
    ans.append(s - dist[i])

print(min(ans))

Solution (Python):

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

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

A_list = sorted(A_list)

max_d = 0

for i in range(N-1):
    d = A_list[i + 1] - A_list[i]

    if d > max_d:
        max_d = d

l = K - A_list[N -1] + A_list[0]
if l > max_d:
    max_d = l


print(K - max_d)

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

saidai_kyori = 0
sou_kyori = 0
for i, position in enumerate(house_list):
    kyori = 0
    if i == 0:
        kyori = K + house_list[0] - house_list[-1]
    else:
        kyori = house_list[i] - house_list[i-1]

    if kyori > saidai_kyori:
        saidai_kyori = kyori
    
    sou_kyori += kyori

print(sou_kyori - saidai_kyori)

Solution (Python):
kn=input().split()
k=int(kn[0])
n=int(kn[1])
an=input().split()
num_list=[]
for a in an :
  num_list.append(int(a))
count_list=[]

for i in range(n) :
  value=num_list[i-1]-num_list[i]
  if value<0 :
    value+=k
  
  count_list.append(value)
  
print(min(count_list))

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

A.sort()


B = A[1:]
B.append(A[0])


SUB = [b-a for (a, b) in zip(A,B)]

SUB[-1] += K


print(sum(SUB)-max(SUB))

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

Solution (Python):

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

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

l = []

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

x_last = (K - A[-1]) + A[0]
l.append(x_last)


max_value = max(l)


ans = K - max_value

print(ans)

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

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

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

Solution (Python):
K , N = list(map(int, input().split()))
a = list(map(int, input().split()))
x = 0
t = (a[0]-a[N-1])%K
for i in range(N-1):
 if a[i+1]-a[i]>x:
  x = a[i+1]-a[i]
if x < t:
 x = t
y = K -x
print(y)

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

dA = [A[i+1] - A[i] for i in range(N-1)]
dA.append(K-A[-1]+A[0])
sumdA = sum(dA)
maxdA = max(dA)
print(sumdA - maxdA)

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

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

l.sort()
for i in range(n-1):
  if a > l[i+1]-l[i]:
    continue
  else:
    a = l[i+1]-l[i]
    
if l[0] + k - l[n-1] > a:
  a = l[0] + k - l[n-1]

print(k-a)

Solution (Python):
from itertools import accumulate

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

D=[0]*(N+1)
for i in range(N-1):
    D[i+1]=A[i+1]-A[i]
D[-1]=K-A[-1]+A[0]
D.extend(D[1:])
D=list(accumulate(D))

ans=float('inf')
for l in range(1,N+1):
    r=l+N-1
    ans=min(ans,abs(D[r]-D[l]))
print(ans)

Solution (Python):

k,n = map(int, input().split())
a = list(map(int, input().split()))
b=[0]*(len(a)-1)
c=[0]*(len(a)-1)
mx =0 
index =0
flag = True
for i in range(len(a)-1):
    if i==0 :
        mae = (k-a[-1])+a[0]
        ushiro = a[1]-a[0]
        if mx < mae:
            mx = mae
            index =i
            flag =True
        elif mx < ushiro:
            mx = ushiro
            index = i
            frag =False
    else:
        mae = a[i]-a[i-1]
        ushiro = a[i+1] - a[i]
        if mx < mae:
            mx = mae
            index =i
            flag =True
        elif mx < ushiro:
            mx = ushiro
            index = i
            frag =False

print(k-mx)

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

print(K-tem)

Solution (Python):


k,n = map(int,input().split())
al = input().split()
al2 = [int(i) for i in al]
distance = []
for i in range(len(al)-1):
    distance.append(al2[i+1]-al2[i])
distance.append(((al2[0])+(k-al2[-1])))
distance.remove(max(distance))
print(sum(distance))

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

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

Solution (Python):
K,N=map(int,input().split())
a=input().split()
A=[int(b) for b in a]
answers=[]
for z in range(N-1):
    sa=A[z+1]-A[z]
    answers.append(sa)
answers.append(K-A[-1]+A[0])
kotae=max(answers)
print(K-kotae)

Solution (Python):

import numpy as np

k, n = map(int, input().split())
a = np.array(input().split(), dtype='int')

b = np.zeros((n), dtype='int')

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

ans = k - np.max(b)

print(ans)

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

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

Solution (Python):
def main():
    k,n = map(int,input().split())
    arr = list(map(int,input().split()))
    ans = float('inf')
    prev = 0
    for i in range(n):
        if i == n-1:
            if prev == 0:
                total = arr[-1]-arr[i]
            else:
                total = k-arr[i]+prev
        else:
            if i == 0:
                total = arr[-1]-arr[i]
            else:
                total = min(k-arr[i]+prev,arr[i]+k-arr[i+1])

        prev += arr[i]
            
        ans = min(ans,total)

    print(ans)
                            
main()

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

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

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

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

dist = 0
num = 0
for i in range(n):
    temp = s[i+1] - s[i]
    if temp > dist:
        dist = temp

print(k - dist)

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

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

print(k - max(d))

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

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

x = [0]*n
for i in range(n):
  t = a[i] - a[i-1]
  if i == 0:
    t += k
    x[i] = t
  x[i] = t

print(sum(i for i in sorted(x)[:-1]))

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

distance = [0] * N

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

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

print(K - max(distance))

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

print(K - max_diff)

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

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

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

A.append(k + A[0])

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

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

dist = 0

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

dist = max(dist, K - A[N - 1] + A[0])

print(K - dist)

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

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

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

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

print(k-max(d))

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

min = int(A[N - 1]) - int(A[0])

for i in range(1, len(A)):
    d = int(A[i - 1]) - int(A[i]) + K
    if d < min:
        min = d

print(min)

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

diff = []
for i, a in enumerate(a_lst[:-1]):
    diff.append(a_lst[i+1] - a)
diff.append((a_lst[0] + k) - a_lst[-1])

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

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

Solution (Python):
from itertools import accumulate

K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [a+K for a in A]
A.extend(B)

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

cum = [0]
cum.extend(list(accumulate(B)))

ans = float('inf')

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

print(ans)

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

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

start = 0
max_sa = 0
for i in range(n):
	if i+1 == n:
		sa = a[(i+1)%n] - (a[i%n]-k)
	else:
		sa = a[(i+1)%n] - a[i%n]

	if max_sa < sa:
		max_sa = sa
		start = (i+1)%n

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

print(ans)

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

distance = [0]*n

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

distance.sort(reverse=True)
print(sum(distance[1:]))

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

for i in range(N):
    if N-1>i:
        D.append(A[i+1]-A[i])
    else:
        D.append(K-A[i]+A[0])


D.remove(max(D))

print(sum(D))

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

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

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

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

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

Solution (Python):
import numpy as np

def solve(_k, _A):
  ans = np.inf
  tmp_list = _A + [i+_k for i in _A]
  for i in range(len(_A)):
    dist = tmp_list[len(_A)+i-1] - tmp_list[i]
    if ans > dist:
      ans = dist
  return ans

if __name__ == "__main__":
  _k,_n = map(int, input().split())
  _A = [int(i) for i in input().split()]
  print(int(solve(_k, _A)))

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

a = A[0]
A = [x - a for x in A]

cur = 0
res = float('inf')
l = len(A)
for i in range(len(A)):
    x = A[i]

    if i == l - 1:
        d = abs(K - x)
    else:
        y = A[i + 1]
        d = y - x

    if d > cur:
        res = min(K - d, res)
        cur = d

print(res)

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

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

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

print(ans)

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

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

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

MAX=A.pop(N-1)
pre=MAX
counter=[]

for a in reversed(A):
    counter.append(pre - a)
    pre=a

counter.append(pre + K - MAX)
counter.sort()
print(sum(counter[:N-1]))

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

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

print(m-a)

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

AList.sort()

maxIso = -1

for indx in range(N-1):
    if (AList[indx+1] - AList[indx]) > maxIso:
        maxIso =  AList[indx+1] - AList[indx]

if (AList[0] + (K - AList[N-1])) > maxIso:
    maxIso = AList[0] + (K - AList[N-1])

print(K - maxIso)

Solution (Python):
k,n = (int(x) for x in input().split())
la = list(map(int, input().split()))

l1 = la[:-1]
l2 = la[1:]

ls= [x - y for (x, y) in zip(l2, l1)]
ls.append(la[0]+k-la[n-1])

print(k-max(ls))

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

ans = min(a[-1] - a[0], K - a[-1] + a[-2])
for i in range(1, N - 1):
    ans = min(ans, K - a[i] + a[i - 1])

print(ans)

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

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

print(K - m)

Solution (Python):
import numpy
k,n=map(int,input().split())
h=numpy.array(input().split(),dtype=int)
i=numpy.append(h[1:],k+h[0])
print(k-max(i-h))

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

a = input().split()

point = k - int(a[-1]) + int(a[0])

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

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

dis = list(map(int, input().split(' ')))
dis = [__ - dis[0] for __ in dis]
dis.insert(0, 0)
dis = np.array(dis[1:]) - np.array(dis[:-1])
dis = list(dis)
dis.append(K - sum(dis))

dis = sorted(dis)

print(sum(dis[1:-1]))

Solution (Python):
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = sys.stdin.readline
    input_list = (lambda: input().rstrip().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

    K, N = input_number_list()
    A = input_number_list()
    A.sort()
    ans = A[-1] - A[0]
    for i in range(1, N - 1):
        l = A[i] + (K - A[i + 1])
        r = K - A[i] + A[i - 1]
        ans = min(ans, l, r)
    ans = min(ans, K - A[-1] + A[-2])
    print(ans)


if __name__ == '__main__':
    main()

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

ans = 10**9
for i in range(N):
	right = A[i-1] - A[i]
	right += K if right < 0 else 0
	left = A[i] - A[0] if i == N - 1 else A[i] + K - A[i+1]
	ans = min(ans, min(right, left))

print(ans)

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

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

ma = max(b)
print(sum(b) - ma)

Solution (Python):

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

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

print(sum(sorted(sabun)[:n-1]))

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

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

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

print(k - max(d))

Solution (Python):
if __name__ == '__main__':
    k, n = tuple([int(i) for i in input().split()])
    distances = [int(i) for i in input().split()]

    between = [0] * len(distances)
    between[0] = distances[0] + k - distances[-1]

    mx = 0
    for i in range(1, len(distances)):
        between[i] = distances[i] - distances[i - 1]
        mx = max(mx, between[i])

    total = sum(between)
    res = total
    for i in range(len(between)):
        res = min(res, total - between[i])

    print(res)

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

print(K-maxnum)

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

max1 = 0

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

max1 = max(max1, k - (a[-1] - a[0]))

print(k - max1)

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

Solution (Python):

"""
Created on Sat Mar 28 21:18:11 2020

@author: liang
"""

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

max_distance = 0
for i in range(N-1):
    temp = A[i+1] -A[i]
    if temp > max_distance:
        max_distance = temp

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

result = K - max_distance

print(result)

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

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

max_diff = max(A_diff)
print(int(K - max_diff))

Solution (Python):
import copy

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

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




dis = copy.deepcopy(a)
dis[-1] = k - a[-1] + a[0]
for i in range (len(a)-1):
  dis[i] = a[i+1] - a[i]



max_d = max(dis)
ans = sum(dis) - max_d

print(ans)

Solution (Python):



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

B = []
max_len = 0

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

sum_v = sum(B)
print(sum_v-max_len)

Solution (Python):
def main():
  k, n = map(int, input().split())
  max_range = 0
  first = True
  first_range = 0
  pre_range = 0
  for i in [int(i) for i in input().split()]:
    if first:
      first = False
      first_range = i
      pre_range = i
      continue
    
    if max_range < i - pre_range:
      max_range = i - pre_range
    
    pre_range = i
  
  if max_range < first_range + (k - pre_range):
    max_range = first_range + (k - pre_range)
  
  return k - max_range

print(main())

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

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

print(K-d)

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))

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


main()

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

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

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

for i in range(0, n-1):
    d.append(houses[i+1] - houses[i])
d.append(k - houses[-1] + houses[0])
ans = sorted(d)
del(ans[-1])
print(sum(ans))

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

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

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

D = [None] * N
last = A[-1] - K
for i, a in enumerate(A):
  diff = (a - last, i)
  D[i] = diff
  last = a

i = max(D)[1]
if i == 0:
  print(A[i - 1] - A[i])
else:
  print(K - A[i] + A[i - 1])

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 = [int(x) for x in input().split()]

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

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