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-----
20 3
5 10 15

-----Sample Output-----
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.

Difficulty: introductory

Starter code:

Solutions:
k, n = map(int, input().split())
points = list(map(int, input().split()))

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

max = dist[0]
for j in range(1, len(dist), 1):
    if max < dist[j]:
        max = dist[j]
dist.remove(max)

ans = 0
for k in dist:
    ans += k
print(ans)

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

import numpy as np
K, N = map(int, input().split())
A = np.array(list(map(int, input().split())))
diff = np.append(A[1:] - A[:-1], K + A[0] - A[-1])
print(np.delete(diff, np.argmax(diff)).sum())

K, N = map(int, input().split())
A_l = sorted(map(int, input().split()))
d_l = [K+A_l[0]-A_l[N-1]]
for i in range(1, N):
    d_l.append(A_l[i]-A_l[i-1])
print(K-max(d_l))

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

# import math
# import statistics
# import itertools
# a=int(input())
# b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(int(i))
A,B= map(int,input().split())
f = list(map(int,input().split()))
# g = [input().split for _ in range(a)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen
kyori=[]
for i in range(len(f)-1):
    an=f[i+1]-f[i]
    kyori.append(an)
    
an2=A-f[-1]+f[0]
ans=max(max(kyori),an2)
print(A-ans)

def i_input(): return int(input())


def i_map(): return map(int, input().split())


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


def i_row(N): return [int(input()) for _ in range(N)]


def i_row_list(N): return [list(map(int, input().split())) for _ in range(N)]


k, n = i_map()
aa = i_list()
dif=[]
for i in range(n):
    if i==n-1:
        dif.append(k-aa[-1]+aa[0])
    else:
        dif.append(aa[i+1]-aa[i])
print(k-max(dif))

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

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


import numpy as np

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

dist = [a[i+1]-a[i] if i < n-1 else k-a[i]+a[0] for i in range(n)]
max_index = np.argmax(dist)

cost = 0
curr_index = max_index
for _ in range(n-1):
    if curr_index == 0:
        cost += a[curr_index] + k - a[curr_index-1]
    else:
        cost += a[curr_index] - a[curr_index-1]
    curr_index -= 1
print(cost)

import numpy as np
n,k = [ int(i) for i in input().split() ]
a = [ int(i) for i in input().split() ]
a = np.sort(a)
a = np.append(a, n+a[0] )
a1 = np.append( 0, a[:-1] )
b = a-a1
b = b[1:]
print(( np.sum(b) - np.max(b) ))




K, N = list(map(int, input().split()))
A_list = list(map(int, input().split()))
A_list.append(K + A_list[0])

dif_list = [0] * N

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

print(K - max(dif_list))

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

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

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

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

max_d = 0

for a in range(1, N):
    d = A[a] - A[a-1]
    if d > max_d:
        max_d = d
last_d = K - A[-1] + A[0]
if last_d > max_d:
    max_d = last_d
print(K - max_d)

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

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

k,n = map(int,input().split())
L = list(map(int,input().split()))
L.append(k+L[0])
m = 0

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

print(k-m)

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

a = 0
if A[0] == 0:
    a = K - A[N - 1]
else:
    a = min(A[N - 1] - A[0], K - A[N - 1] + A[0])


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

import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    K , N = MI()
    L = LI()
    res = L[-1] - L[0]
    max_num = L[-1]
    for i in range(1, N):
        temp = L[i]
        past = L[i-1]
        past += K - temp
        res = min(res, past)
        
    print(res)
def __starting_point():
    main()
__starting_point()

K, N = map(int, input().split())
Alst = list(map(int, input().split()))
zero = Alst[0] + K
M = 0
now = Alst[0]
for i in Alst:
    dis = i - now
    if dis > M:
        M = dis
    now = i

last = zero - now
if last > M:
    M = last

print(K - M)

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

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

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

print((sum(route) - max(route)))


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

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

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


k, n = map(int, input().split())
a_list = list(map(int, input().split()))
longest = 0

for i in range(n-1):
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)

print(k-max(longest, k-a_list[n-1]+a_list[0]))

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

# coding=utf-8

def __starting_point():
    K, N = map(int, input().split())
    li = list(map(int, input().split()))
    li.append(K+li[0])

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

    print(K-l)
__starting_point()

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

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

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



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

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

C = A[0]+K-A[N-1]
B.append(C)
B.sort()
print((K-B[N-1]))


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

longest = 0
for i in range(n):
    # if i==0:
    #     continue
    if i==n-1:
        longest = max(longest, k-a_list[i]+a_list[0])
    else:
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)

print(k-longest)

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

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

a.append(a[0] + k)

m = 0

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

print(k - m)

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

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

max_d = max(d)

print(K - max_d)

def main():
    k, n = map(int, input().split())
    a = [int(v) for v in input().split()]
    distances = []
    for i in range(1, len(a)):
        distances.append((a[i]-a[i-1]))
    distances.append((k+a[0]) - a[-1])
    maximum = max(distances)
    return k - maximum

def __starting_point():
    print(main())
__starting_point()

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

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


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

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

As += [As[0] + K]

maxDiff = 0
for i in range(N):
    d = As[i+1]-As[i]
    if d > maxDiff:
        maxDiff = d

print((K-maxDiff))


k, n = map(int, input().split())
a = list(map(int, input().split()))
root = []
ans = 0

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

y = a[0] + k - a[n-1]
root.append(y)

for i in root:
    ans += i

ans -= max(root)
print(ans)    

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

da =[-1] * n
da[n-1] = k - a[n-1] + a[0]
for i in range(n-1):
    da[i] = a[i+1] - a[i]
    
print(sum(da)-max(da))

K,N = map(int,input().split())
A = [int(i) for i in input().split()]
L = []
ans = 0
for i in range(N):
    if(i == N-1):
        L.append(K-A[N-1]+A[0])
        break
    L.append(A[i+1]-A[i])
for i in range(N):
    ans += L[i]
ans -= max(L)
print(ans)

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

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

    li.append(abs(A[0] + (K - A[-1])))
    print(sum(li) - max(li))
except:
    pass

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

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

b_max       = max(B)
b_max_index = B.index(max(B))


B.remove(b_max)
print((sum(B)))


import math
import decimal
def i_input(): return int(input())


def i_map(): return map(int, input().split())


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


def i_row(N): return [int(input()) for _ in range(N)]


def i_row_list(N): return [list(map(int, input().split())) for _ in range(N)]

k,n= i_map()
aa=i_list()
dis=[]
for i in range(n):
    if i == 0:
        dis.append(k-aa[-1]+aa[0])
    else:
        dis.append(aa[i]-aa[i-1])
print(k-max(dis))

import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


def solve():
    k, n = inl()
    A = inl()
    dmax = A[0] + (k - A[-1])
    for i in range(1, n):
        dmax = max(dmax, A[i] - A[i - 1])
    return k - dmax


print(solve())


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

a.append(k+a[0])

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

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

#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc160/tasks/abc160_c

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

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

print((K - max(dist)))


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

a.sort()
a.append(a[0]+k)

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

print(k - ans)

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

dif = []

for i,a in enumerate(A[:len(A)-1]):
    dif.append(A[i+1] - a)

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

print((K - max(dif)))


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

a=list(map(int,input().split()))
a.sort()
a.append(k+a[0])

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

print((k-max(b)))


k,n = list(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]+(k-a[-1]))

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


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

D = [0] * (N)

A2 = [A1[i]+K for i in range(len(A1)-1)]
A1 = A1+A2

for i in range(N):
    D[i] = A1[i+N-1] - A1[i]
    #print(A1[i],A1[i+N-1])
print(min(D))

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

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

print(k-max(c))

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


class Solution:
    def __init__(self, k, n, an):
        self.k = k
        self.n = n
        self.an = an

    @staticmethod
    def __append_first():
        an.append(k + an[0])
        new_an = an
        return new_an

    def answer(self):
        new_an = self.__append_first()
        max_length = 0
        for i in range(n):
            max_length = max(max_length, new_an[i + 1] - new_an[i])
        print(k - max_length)


conditions = Solution(k, n, an)
conditions.answer()

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

print(K-max(sa))

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


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

A2 = [K+a for a in A]
A3 = A + A2

dist = [0] * N
for i in range(N):
    j = i + N - 1
    dist[i] = A3[j] - A[i]

print((min(dist)))



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

# import numpy as np
# import math

# from scipy.special import perm : perm(n, r, exact=True)
# from scipy.special import comb : comb(n, r, exact=True)

# import itertools
# for v in itertools.combinations(L, n):M.append(list(v))

# from numba import njit
# @njit('f8(i8,i8,i8,i8,f8[:,:,:])', cache=True)


""" Definitions  """

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

MOD = 10**9+7

# ============================================================
k, n = list(map(int, input().split()))
A = list(map(int, input().split()))

m = 2*10**5
M = [0]*n
dist = min(A[n-1]-A[0], k-A[n-1]+A[0])
for i in range(n-1):
    dist = max(dist, A[i+1]-A[i])
print((k-dist))


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

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

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

K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 10**18

for i in range(N):
    if i == 0:
        clock_distance = A[N-1] - A[i]
        reverse_distance = K - A[i+1] + A[i]
    elif i == N-1:
        clock_distance = K - A[i] + A[i-1]
        reverse_distance = A[i] - A[0]
    else:
        clock_distance = K - A[i] + A[i+1]
        reverse_distance = K - A[i+1] + A[i]
    ans = min(clock_distance, reverse_distance, ans)

print(ans)





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

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

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

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
longest = 0
for i in range(n):
    if i==n-1:
        longest = max(longest, k-a_list[i]+a_list[0])
    else:
        distance = a_list[i+1] - a_list[i]
        longest = max(longest, distance)
print(k-longest)

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

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

print(k-max_length)

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

T = []

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

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

print(sum(T)-max(T))

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

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

print(ans)

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


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

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

print(k - max(b))

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

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

d.sort()
d[n-1] = 0

print(sum(d))

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

A3 = A + [K+a for a in A]
dist = [A3[i+N-1] - A[i] for i in range(N)]

print((min(dist)))



k, n = list(map(int,input().split()))
a = list(map(int,input().split()))
new_a = sorted(a)
num = len(new_a) - 1
s_a = []
b = (k - new_a[num]) + new_a[0]
s_a.append(b)
cun = 0
for i in new_a[1::]:
    s = i - new_a[cun]
    s_a.append(s)
    cun += 1
max_num = max(s_a)
print((sum(s_a) - max_num))



def __starting_point():

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

    #距離の差分が一番多い区間を求める
    sbn = -1
    for i in range(n-1):
        sbn = max(sbn,A[i+1]-A[i])

    #最初と最後も比較する
    tmp1 = A[0] - 0
    tmp2 = k - A[n-1]
    sbn = max(sbn,tmp1+tmp2)

    print((k-sbn))


__starting_point()

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

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

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

d_max = 0
a[0] = a[n] - k

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

print((k - d_max))


k, n = map(int, input().split())
an = [int(num) for num in input().split()]

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

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

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])
ans = K - max(B)
print(ans)

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

sub = [0] * (N - 1)

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

print(sum(sub) - max(sub))

import numpy as np

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

a_array.append(k + a_array[0])

a_diff = np.diff(a_array)
ans = np.sum(a_diff) - np.max(a_diff)
print(ans)

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

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

K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
rst = -1
for i in range(N):
    l = A_ls[(i + 1) % N] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % N]
    if rst == -1:
        rst = l
    else:
        rst = max(rst, l)
print(K - rst)

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


k, n = list(map(int, input().split()))
cnt = [0] * (n + 1)
cmb = 0
a = list(map(int, input().split()))

ans = 10 ** 6 + 1
for i in range(n):
  r_idx = ((i+1)%n)
  r_ans = a[i]+(k-a[r_idx]) if a[i] < a[r_idx] else abs(a[r_idx]-a[i])
  l_idx = (i-1+n)%n
  l_ans = (k-a[i])+a[l_idx] if a[i] > a[l_idx] else a[l_idx]-a[i]

  ans = min([ans, r_ans, l_ans])

print(ans)


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

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

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

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

import numpy as np

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

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

dif = np.array(dif)
max_dif_idx = np.argmax(dif)
print(dif.sum() - dif[max_dif_idx])

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a1 = sorted(a)
temp = list()
x = len(a)-1
for i in range(x):
    temp.append(a1[i+1]-a1[i])
temp.append(min(max(a)-min(a),k-max(a)+min(a)))
ans = k-max(temp)
print(ans)


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

bef = a[0]
max_diff = 0
for i in range(1,n):
    diff = abs(bef - a[i])
    max_diff = max(diff, max_diff)
    bef = a[i]

print(k - max(max_diff, k - abs(a[n-1] - a[0])))

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

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

for i in range(len(A) - 1):
    dis = A[i + 1] - A[i]
    if dis > mx:
        mx = dis

print((K - mx))


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



A = sorted(A)
L = []
ANS = 0

A_max = max(A)

for i in range(N-1):
    L.append(abs(A[i+1]-A[i]))
        
L.append(K + A[0] - A_max)

L = sorted(L)


L.pop(-1)

for i in range(len(L)):
    ANS = ANS + L[i]
    
print(ANS)


k,n = map(int, input().split())
A = list(map(int,input().split()))
d = 0

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

d1 = (k - A[n-1]) + A[0]
d = max(d, d1)
print(k-d)

K, N = map(int, input().split())
A = [int(a) for a in input().split()]
A.append(A[0] + K)
b = []
max = 0
sum = 0
for i in range(N):
    t = A[i+1] - A[i]
    sum += t
    if max < t:
        max = t
sum -= max
print(sum)

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

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

print(sum(nums) - max(nums))
return