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 resolve():
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))
    t = [0] * len(A)
    max_v = K - A[-1] + A[0]
    curr_sum = max_v
    for i in range(1, len(A)):
        max_v = max(max_v, A[i] - A[i-1])
        curr_sum += A[i] - A[i-1]
    print(curr_sum - max_v)

resolve()

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

Bs = [k - As[-1] + As[0]]

for i in range(1, n):
    Bs.append(As[i] - As[i - 1])

print(k - max(Bs))

Solution (Python):
def input_int_list():
    return [int(thing) for thing in input().split(" ")]

K,N=input_int_list()
A_list=sorted(input_int_list())
A_list=A_list+[A_list[0]+K]
max_dis=0
for i in range(N):
    max_dis=max(max_dis,A_list[i+1]-A_list[i])
print(K-max_dis)

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

ma = 0
for i in range(len(aL)):
    if i == len(aL) - 1:
        ma = max(abs(k - aL[i] + aL[0]), ma)
    else:
        ma = max(abs(aL[i] - aL[i + 1]), ma)

print(k - ma)

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

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

maxx = 0
for i,v in enumerate(A):
    if i == 0:
        right = A[i+1]
        a ,b = K-A[-1]+A[0], abs(v-right)
    elif i == len(A)-1:
        left = A[i-1]
        a, b = abs(v-left) , K-v+A[0]
    else:
        left,right = A[i-1],A[i+1]
        a, b = abs(v-left) , abs(v-right)


    if maxx < a:
        T = i
        turn = 'right'
        maxx = a
    
    if maxx < b:
        T = i
        turn = 'left'
        maxx = b

print(K - maxx)

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

doublelist = alist.copy()

for a in alist:
    doublelist.append(k + a)



res = float('inf')
prev = float('inf')

for i in range(n + 1):
    prev = doublelist[i + n - 1] - doublelist[i]
    
    res = min(res, prev)

print(res)

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

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

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

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

print(K - x)

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

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

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

A.sort()

for i in range(n):
    if abs(A[i] - A[i - 1]) <= k / 2:
        T[i] = abs(A[i] - A[i - 1])
    else:
        T[i] = k - abs(A[i] - A[i - 1])

print(k - max(T))

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

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

print(K-M)

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

max = k -A[n-1] + A[0]
for i in range(n-1):
    if (A[i + 1] - A[i]) > max:
        max = (A[i + 1] - A[i])

print(k-max)

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

A.sort();

Am = [0]*n;
for i in range(n-1) :
    Am[i] = A[i+1] - A[i];
Am[n-1] = k-A[n-1] + A[0];

Am.sort();

print(sum(Am)-Am[n-1]);

Solution (Python):




if(__name__ == '__main__'):
    
    k, n = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))

    a.sort()

    d_max = 0
    for elem in range(0,len(a)):
        d = 0
        if elem == len(a) - 1:
            d = k - a[elem] + a[0]
        else:
            d = a[elem+1] - a[elem]

        if d > d_max:
            d_max = d

    ans = k - d_max
    
    print(ans)

Solution (Python):
def Main(K, N, A):
    dist = [0]*N
    for i in range(1, N):
        dist[i-1] = A[i] - A[i-1]
    dist[N-1] = K - A[N-1] + A[0]
    return sum(dist) - max(dist)

def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    print(Main(K, N, A))

if __name__ == '__main__':
    main()

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

Solution (Python):
k, n = list(map(int, input().split()))
x = list(map(int, input().split()))
y = [] * len(x)

max_dist = 0
for i in range(len(x)):
  now_dist = 0
  if i == 0:
    now_dist = k - (x[len(x)-1] - x[0])
  else:
    now_dist = x[i] - x[i-1]
  if now_dist > max_dist:
    max_dist = now_dist
print(k - max_dist)

Solution (Python):
def main():
    K,N,*A=map(int, open(0).read().split())
    A+=[A[0]+K]
    print(K-max(abs(A[i+1]-A[i]) for i in range(N)))

if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np

def main():
    K, _ = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    
    A.append(K+A[0])
    X = np.diff(np.array(A))
    print(X.sum() - X.max())

if __name__ == "__main__":
    main()

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

ans = K - max((A[(i + 1) % N] - A[i]) % K for i in range(N))

print(ans)

Solution (Python):

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

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

max_diff = 0
for i in range(1,n):
    if i != 0:
        max_diff = max(abs(An[i] - An[i-1]), max_diff)

max_diff = max(max_diff, k-An[n-1] + An[0])
print(k-max_diff)

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

end = a[0] + k - a[n-1]
b = []
b.append(end)

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

b.sort(reverse=True)
max = b[0]
print(k-max)

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

la = len(a)
for i in range(la):
    d = a[(i+1)%la] - a[i]
    if amax < a[(i+1)%la] - a[i]:
        amax = a[(i+1)%la] - a[i]
if amax < k - a[-1] + a[0] :
    amax = k - a[-1] + a[0]

print(k - amax)

Solution (Python):
k, n, *a = map(int, open(0).read().split())

dis_max = 0

for i in range(1, len(a)):
  dis_i = a[i] - a[i-1]
  if dis_i > dis_max:
    dis_max = dis_i

if (k-a[-1]+a[0]) > dis_max:
  dis_max = (k-a[-1]+a[0])

ans = k - dis_max

print(ans)

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

dMax=(k-A[-1]+A[0])

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

ans = k - dMax
print(ans)

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

dis_max=0

for i in range(n-1):
    dis_tmp = a[i+1]-a[i]
    if dis_tmp > dis_max:
        dis_max = dis_tmp

if a[0]+(k-a[n-1]) > dis_max:
    dis_max = a[0]+(k-a[n-1])

print(k-dis_max)

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

if __name__ == '__main__':
    main()

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

a = list(map(int,input().split()))
k_diff = []

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

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

print(k - max(k_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):
def main():
  nagasa, num_house = map(int, input().split())
  house_list = [int(t) for t in input().split()]
  
  max_length = 0
  for i in range(num_house):
    if i != num_house-1:
      dif = house_list[i+1] - house_list[i]
    if i == num_house-1:
      dif = nagasa - house_list[i] + house_list[0]
    if max_length < dif:
      max_length = dif
  print(nagasa - max_length)

main()

Solution (Python):

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


print((K - max(B)))

Solution (Python):
K,N = map(int,input().split())
A = [int(a) for a in 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):
r, n = map(int, input().split())
num = list(map(int, input().split()))
ans = 0
for i in range(n-1):
  max_bet = abs(num[i] - num[i+1])
  ans = max(ans, max_bet)
bet = r-num[n-1]+num[0]
ans = max(ans, bet)
print(r-ans)

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

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

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

Solution (Python):
k, n, *a = map(int, open(0).read().split())

dis_max = 0

for i in range(1, len(a)):
  dis_i = a[i] - a[i-1]
  if dis_i > dis_max:
    dis_max = dis_i

if (k-a[-1]+a[0]) > dis_max:
  dis_max = (k-a[-1]+a[0])

ans = k - dis_max

print(ans)

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

if __name__ == '__main__':
  main()

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

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

ans = float("inf")
for right in range(N):
    left = right - (N - 1)
    if left >= 0:
        ans = min(ans, A[right] - A[left])
    else:
        ans = min(ans, A[right] + (K - A[left]))

print(ans)

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

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

for i in range(N):
    A.append(A[i]+K)
    
ans = 10**9+7

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

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

Solution (Python):

from sys import stdin, stdout


def read():
    x = stdin.readline().rstrip()
    return x


def __main__():

    k, n = map(int, read().split())
    A = [int(i) for i in read().split()]

    A.sort()
    d = []
    a0 = A[0]
    for a in A[1:]:
        d.append(abs(a-a0))
        a0 = a
    d.append(abs(k - A[-1]) + A[0])

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

__main__()

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

cs[0] += K-X[-1]
print(K-max(cs))

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):
hoge = input().split()
K = int(hoge[0])
N = int(hoge[1])
A_list = list(map(int,input().split()))

kankaku = []
for i in range(len(A_list) - 1):
  kankaku.append(A_list[i+1] - A_list[i])
kankaku.append(A_list[0] - A_list[-1] + K)

_kankaku_max = max(kankaku)
print(K - _kankaku_max)

Solution (Python):
list_in = list(map(int, input().split()))
list_house = list(map(int, input().split()))
K = list_in[0]
N = list_in[1]
list_diff = [0] * N

for i in range(1, N):
    list_diff[i] = list_house[i] - list_house[i-1]

list_diff[0] = K + list_house[0] - list_house[N-1]

max_diff = max(list_diff)
start_idx = list_diff.index(max_diff)

sum = 0
for i in range(N-1):
    tmp_idx = ( start_idx + i + 1 ) % N
    sum += list_diff[tmp_idx]

print(sum)

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

Solution (Python):
[K,N] = [int(s) for s in input().rstrip().split(' ')]
L_A = [int(s) for s in input().rstrip().split(' ')]
s = L_A[0] + K - L_A[-1]
for i in range(N-1):
  if s < L_A[i+1] - L_A[i]:
    s = L_A[i+1] - L_A[i]
print(K-s)

Solution (Python):
import sys
import math
from collections import Counter

k, n = [int(s) for s in sys.stdin.readline().strip().split()]
a = [int(s) for s in sys.stdin.readline().strip().split()]

longest = -1
prev = a[0]

for i in range(1, n):
    ai = a[i]
    dist = ai - prev
    longest = max(longest, dist)
    prev = ai

last_cand = k - a[-1] + a[0]
longest = max(longest, last_cand)

print(k - longest)

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

lgt_dist = 0
for i in range(n):
    if i == 0:
        lgt_dist = a[i] + (k - a[i - 1])
    else:
        lgt_dist = max(lgt_dist, abs(a[i] - a[i - 1]))

print(k - lgt_dist)

Solution (Python):
import numpy as np
from sys import stdin

def get_result(data):
    K, N = data[0]
    A = data[1]
    tmp = A[0]
    inter_dist = []
    for i in range(1, len(A)):
        inter_dist.append(A[i]-tmp)
        tmp = A[i]
    inter_dist += [K-A[-1]+A[0]]
    ans = sum(inter_dist) - max(inter_dist)
    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(int, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

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[n-1] + a[0])

sorted_dist = sorted(dist)
print(sum(sorted_dist[:n-1]))

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

Solution (Python):




import sys
input = sys.stdin.readline



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

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

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

    res = K - most
    print(res)



main()

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

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






ans = K
for i in range(1, N - 1):
    right = K - (A[i] - A[i - 1])
    left = K - (A[i + 1] - A[i])
    ans = min(ans, min(right, left))


right = A[N - 1] - A[0]
left = K - A[0] - (K - A[N - 1])
ans = min(ans, min(right, left))


left = A[N - 1] - A[0]
right = K - A[0] - (K - A[N - 1])
ans = min(ans, min(right, left))


print(ans)

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

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

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

print(K-m)

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    K, N = input_nums()
    A = input_nums()
    dist = []
    for i in range(len(A)-1):
        dist.append(A[i+1]-A[i])
    dist.append(K+A[0]-A[-1])
    maxv = max(dist)
    print(sum(dist) - maxv)
    

if __name__ == '__main__':
    main()

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

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

print(k - max(dis))

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

dif_max = 0

for i in range(N):
    if i == 0:
        dif = A[0] + (K - A[-1])
    else:
        dif = abs(A[i] - A[i-1])
    if dif > dif_max:
        dif_max = dif

print(K - dif_max)

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

Solution (Python):
import string
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))

K,N = nums()
A = nums()

ans = K*2

for i in range(N):
    start = A[i]
    goal = A[i-1]
    if goal < start:
        goal = K + goal
    dis = goal - start
    if dis < ans:
        ans = dis


print(ans)

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

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

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

ans = k - da
print(ans)

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

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

a.sort()

b = []

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

b.append(k - sum(b))

ans = sum(b) - max(b)

print(ans)

Solution (Python):
import math

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

max_dist = None

for i in range(N-1):
    dist = A[i+1] - A[i]
    if max_dist is None or max_dist < dist:
        max_dist = dist

dist = A[0] + K - A[N-1]
if max_dist < dist:
    max_dist = dist

print(K - max_dist)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def S(): return input().rstrip()


def LS(): return S().split()


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


mod = 1000000007
inf = 1e10




def solve():
    k, n = LI()
    
    a = LI()
    a = a + list(map(lambda x: x + k, a))
    ans = inf
    for i in range(n):
        ans = min(ans, a[i + n] - a[i] , k - (a[i + 1] - a[i]))
    print(ans)
    return



if __name__ == '__main__':
    solve()

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

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

A.sort()
l_max = A[0] + (K - A[N-1])
for i in range(N-1):
    l = A[i+1] - A[i]
    if l > l_max:
        l_max = l

print(K - l_max)

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

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

dis_max = 0
for i in range(n):
    if i == n - 1:
        dis = abs(k - a[i] + a[0])
    else:
        dis = abs(a[i] - a[i + 1])
    if dis > dis_max:
        dis_max = dis

print(int(k - dis_max))

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

sa = []

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

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

ma = max(sa)
sa.remove(ma)

print(sum(sa))

Solution (Python):

import sys


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

    return



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

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
k,n=map(int,input().split())
a=np.array(list(map(int,input().split())))

a_diff=np.diff(a)
a_diff2=(k - a[n-1])+a[0]
diff_max=np.max(np.append(a_diff,a_diff2))
print(k-diff_max)

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

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

print(K - maxLen)

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

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

Solution (Python):
import numpy as np
k,n = [int(i) for i in input().split()]

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

A.sort()

ans_p = A[0] + k - A[-1]

for i in range(n-1):
    cand = A[i+1] - A[i]
    if cand > ans_p:
        ans_p = cand
        
ans = k - ans_p

print(ans)

Solution (Python):
k,n=map(int,input().split())
arr=[int(x) for x in input().split()]
dist=[arr[x]-arr[x-1] for x in range(1,n)]
dist.append(k-arr[-1] + arr[0])
print(min([k-item for item in dist]))

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

Solution (Python):
d,n=map(int,input().split())
l=list(map(int,input().split()))
al=[]
for i in range(1,len(l)):
    al.append(l[i]-l[i-1])
x=l[0]+d-l[n-1]
al.append(x)

mx=max(al)
ss=sum(al)
print(ss-mx)

Solution (Python):
n,m=map(int,input().split())
a=[int(j) for j in input().split()]
ma=0
sum=0
for i in range(1,m):
    diff=a[i]-a[i-1]
    ma=max(ma,diff)
    sum+=diff

diff=n-sum
ma=max(ma,diff)
print(n-ma)

Solution (Python):



def main():
  k, n = map(int, input().split())
  la = list(map(lambda x:int(x)%k, input().split()))
  la.sort()
  diff = [(la[i] - la[i-1])%k for i in range(len(la))]
  print(k - max(diff))
  

if __name__=='__main__':
  main()

Solution (Python):
K, N = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
Adif = []
for i in range(1, len(A)):
    dif = A[i] - A[i-1]
    Adif.append(dif)
Adif.append(K - A[-1] + A[0])
Adif.remove(max(Adif))
ans = sum(Adif)

print(ans)

Solution (Python):
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

k, n = load_list()
A = load_list()


def dist(a, b):
    if b > a:
        return b - a
    else:
        return (k-a) + b

ds = []
for i in range(n):
    if i == n-1:
        ds.append(dist(A[i], A[0]))
    else: 
        ds.append(dist(A[i], A[i+1]))

print(sum(ds) - max(ds))

Solution (Python):


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

if l_max < K - A[-1] + A[0]:
    l_max = K - A[-1] + A[0]

ans = K - l_max
print(ans)

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

a = input().split()
a = list(map(lambda n:int(n), a))

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

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

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

dis.remove(max(dis))
print(sum(dis))

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

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

A = sorted(A)
B = []

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

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

print(K-max(B))

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
A=a[-1]
iti=k-A
hantei=10**9
for i in range(1,n-1):
  
  sr=k+a[i-1]-a[i]
  
  sl=k-(a[i+1]-a[i])
  if hantei>sr:
    hantei=sr
  if hantei>sl:
    hantei=sl
if hantei>(a[-1]-a[0]):
  hantei=(a[-1]-a[0])
print(hantei)

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

A.insert(0, A[N-1] - K)
diffs = []
for i in range(1, len(A)):
    diff = A[i] - A[i-1]
    diffs.append(diff)

print(K - max(diffs))

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

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

print(k-b)

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

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

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

Solution (Python):
kn = [int(i) for i in input().split()]
K = kn[0]
N = kn[1]
A = [int(i) for i in input().split()]

Ad = [A[i+1] - A[i] if i != N-1 else K-A[-1]+A[0] for i in range(len(A))]
print(sum(Ad)-max(Ad))

Solution (Python):
import numpy as np

K,N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = []
maxdist = 0
for i in range(N-1):
    B.append(A[i+1]-A[i])
    if B[-1]>maxdist:
        maxdist = B[-1]

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

print(K - maxdist)

Solution (Python):
K, N= map(int, input().split())
A = list(map(int, input().split()))
A.append(K+A[0])
M=0
for i in range(N):
    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()))
D = list()
D.append(A[0] + K - A[-1])
for i in range(N - 1):
    D.append(A[i + 1]- A[i])

print(K - max(D))