peterverebics/CodeNet_Python_118 huggingface dataset:
n, k = list(map(int, input().split()))
a_list = list(map(int, input().split()))

print(
  n - \
    max(
      [a_2 - a_1 \
          for a_1, a_2 in \
          zip(
            a_list[:-1], 
            a_list[1:]
          )
      ] + \
      [n - a_list[-1] + a_list[0]]
    )
)
k,n = map(int,input().split())
a = [int(i) for i in input().split()]
fest = 0
last = 0
for i in a:
    fest = max(i - last,fest)
    last = i
fest = max(fest,k-a[-1]+a[0])
ans = k - fest
print(ans)
K, N = map(int,input().split())
A = list(map(int,input().split()))

mi = 10000000
for i in range(N):
  if i == 0:
    d = A[-1] - A[0]
  else:
    d = K - (A[i] - A[i - 1])
  mi = min(d, mi)
print(mi)
K,N=map(int,input().split())
*A,=map(int,input().split())
A.append(A[0]+K)
ans=float('inf')
for i in range(N):
  a,b=A[i],A[i+1]
  ans=min(ans, K-(b-a))
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for _ in range(N)]
for i in range(N-1):
    B[i] = A[i+1]-A[i]
B[N-1] = A[0]-A[N-1]+K
print(K-max(B))
from itertools import *
K, N = map(int, input().split())
A, B = tee(map(int, input().split()), 2)

first = next(B)
M = max(b-a for a, b in zip_longest(A, B, fillvalue=first+K))

print(K-M)
import copy
from functools import reduce
k,n=map(int,input().split())
a=list(map(int,input().split()))
c=copy.copy(a)
c.insert(0,0)
a.insert(n,(k+a[0]))
b=list(map(lambda a,b:a-b,a,c))
d=reduce(lambda a,b:b if b>a else a,b)
print(k-d)
K,N = map(int,input().split())
A = list(map(int,input().split()))
ans = 0
mi = 0
for i in range(N-1):
  a = A[i+1]-A[i]
  ans += a
  mi = max(mi, a)
  
ans+=(K-A[-1]+A[0])
mi = max(mi,K-A[-1]+A[0])
ans -= mi
print(ans)
import sys
read = sys.stdin.buffer.read
 
K, N, *A = map(int, read().split())
A += [A[0] + K]
gap = max(y-x for x,y in zip(A, A[1:]))
print(K - gap)
K,N=map(int,input().split())

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

B=[]

s=0

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

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

print(k - ans)
import sys 
MOD = 10 ** 9 + 7
INF = 10**11
from heapq import heapify,heappush,heappop

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

if __name__=='__main__':
    main()
k, n = map(int, input().split())
arr = sorted(list(map(int, input().split())))

maxDistance = 0

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

maxDistance = max(maxDistance, arr[0]+(k-arr[-1]))

print(k-maxDistance)
K,N=map(int,input().split())
A=list(map(int,input().split()))

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

print(K-dmax)
k, n = map(int, input().split())
lst = list(map(int, input().split()))
lst.append(lst[0] + k)
for i in range(n):
  lst[i] = lst[i + 1] - lst[i]
lst.pop()
lst.sort()
lst.pop()
print(sum(lst))
k,n = map(int, input().split())
A = list(map(int, input().split()))
ans = 10**6
for i in range(n-1):
  if ans > k - (A[i+1] - A[i]):
    ans = k - (A[i+1] - A[i])
if ans > A[-1] - A[0]:
  ans = A[-1] - A[0]
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))
maxim = 0
for i in range(N-1):
    d = A[i+1] - A[i]
    maxim = max(d, maxim)
maxim = max(K-A[N-1]+A[0], maxim)
print(K - maxim)
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(a[0] + k)

mx = 0
for i in range(n):
  mx = max(mx, a[i + 1] - a[i])
print(k - mx)
k,n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = []
for i in range(0,n):
  if i < n-1:
    b.append(a[i+1]-a[i])
  else:
    b.append(k + a[0] - a[n-1])

print(k - max(b))
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

sub_a = []

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

max_sub = max(sub_a)

print(K - max_sub)
K,N = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
start = 0
sa  = []
for a in A:
  sa.append(a - start)
  start = a
sa[0] += K - A[-1]

print(K - max(sa))
K,N = map(int, input().split())
A = list(map(int, input().split()))

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

mi=max(li)
print(K-mi)
k, n = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
a.append(k+a[0])

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

print(k - max(box))
import numpy as np
 
tmp =list(map(int, input().split()))
num=list(map(int, input().split()))

num.append(num[0]+tmp[0])

print(np.diff(num).sum()-np.diff(num).max())
K,N = map(int,input().split())
A = list(map(int,input().split()))

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

for i in range(1,N):									#range(始まり、項数)
	dis = max(dis,A[i]-A[i-1])

dis = K - dis
print(dis)
k,n,*l=map(int,open(0).read().split())
print(k-max((l[i]-l[i-1])%k for i in range(n)))
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K, N = lr()
A = np.array(lr())
A = np.append(A, A[0] + K)
diff = np.diff(A)
answer = K - diff.max()
print(answer)
K,N = map(int,input().split())
array = list(map(int,input().split()))
ans = 10**8
for i in range(N):
  if i == 0:
    ans1 = array[-1] - array[i]
  else:
    ans1 = array[i-1] + ( K - array[i] )
  if i == N-1:
    ans2 = array[-1] - array[0]
  else:
    ans2 = array[i] + ( K - array[i+1] )
  ans = min(ans,ans1,ans2)
print(ans)
k,n=map(int,input().split())
arr=list(map(int,input().split()))


min_=99999999999

for i in range(n):
	if i==0:
		x=arr[-1]-arr[0]
	else:
		x=k-(arr[i]-arr[i-1])
	if x<min_:
		min_=x


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

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

a.append(a[0]+k)
l=0
for i in range(0,n):
  l=max(l,a[i+1]-a[i])
print(k-l)
def resolve():
    K, N = list(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])
    dist.append(abs(A[0] + K - A[-1]))

    print(sum(dist)-max(dist))
    return


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

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

l.remove(max(l))
print(sum(l))
K, N = map(int, input().split())
A = list(map(int, input().split()))

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

print(K - max(D))
K, N = map(int, input().split())
A = [int(e) for e in input().split()]

ans = 0
S = [(A[i + 1] - A[i]) for i in range(N - 1)]
S.append((K - A[N - 1]) + A[0])
S.sort
m = sorted(S)
for n in range(N - 1):
    ans += m[n]
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))
dA = [0] * N
for i in range(N-1):
    dA[i] = A[i+1] - A[i]
dA[N-1] = A[0] + K - A[N-1]
print(K - max(dA))
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [a[i]-a[i-1] for i in range(1,n)]
b.append(k-a[-1]+a[0])
print(k-max(b))
k, n = map(int, input().split())
a = list(map(int, input().split()))
a2 = a + [aa + k for aa in a]
max_inter = 0
for i in range(n*2 - 1):
    max_inter = max(max_inter, a2[i+1] - a2[i])
print(k - max_inter)
k,n=map(int,input().split())
a=sorted(list(map(int,input().split())))
dist=2*10e+12
for i in range(n):
  p1=i
  p2=i+1
  if i+1!=n:
    dist=min(k-(a[p2]-a[p1]), dist)
  else:
    p2=0
    dist=min(a[p1]-a[p2],dist)
    print(dist)
K,N = map(int,input().split())
A=list(map(int,input().split()))

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

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

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

next_ls = [0] * n
for i in range(n-1):
    next_ls[i] = a_ls[i+1] - a_ls[i]
next_ls[n-1] = k - a_ls[-1] + a_ls[0]
argmax = np.argmax(next_ls)
next_ls.pop(argmax)
print(sum(next_ls))
k, n = 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 bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

K, N = MI()
A = LI()
A_dif = []

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

print(K - max(A_dif))
K,N = map(int,input().split())
A = []
A = list(map(int,input().split()))
last = 0
maxd = 0
x = 0
for i in range(N):
  now = A[i]
  d = now - last
  if(maxd < d):
    maxd = d
    x = i
  last = now
if(maxd < (A[0] + K - A[N-1])):
  print(A[N-1] - A[0])
else:
  print(K - A[x] + A[x-1])
k, n = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

diffs = list()
max_diff = 0
for i in range(n):
    diff = 0
    if i == n-1:
        diff = k + a[0] - a[i]
    else:
        diff = a[i+1] - a[i]

    if max_diff < diff:
        max_diff = diff
    diffs.append(diff)

print(sum(diffs) - max_diff)
#!/usr/bin/env python3

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

d = [0 for _ in range(n)]
for i in range(n):
    if i != n-1:
        d[i] = a[i+1]-a[i]
    else:
        d[i] = k+a[0]-a[i]
print(k-max(d))
K, N = map(int, input().split())
A = list(map(int, input().split()))

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

sum_num = 0
max_num = 0
tmp = K - A[-1] + A[0]
max_num = tmp
sum_num += tmp
diff.append(tmp)
for i in range(N - 1):
    tmp = A[i + 1] - A[i]
    diff.append(tmp)
    if tmp >= max_num:
        max_num =tmp
    sum_num += tmp

ans = sum_num - max_num
print(ans)
import numpy as np
k,n=map(int,input().split())
A = np.array(list(map(int,input().split())))
B = np.append(A[1:],A[0]+k)-A

print(np.sum(B)-np.max(B))
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(k + a[0])
print(k - max(a[i + 1] - a[i] for i in range(n)))
k,n = map(int, input().split())
a = list(map(int, input().split()))
d = 0
e = abs(k-a[-1])
    
for i in range(n-1):
    d = max(d, abs(a[i]-a[i+1]))
d = max(d, abs(a[0]+k-a[-1]))

print(k-d)
k,n = map(int, input().split())
a = list(map(int, input().split()))
m = 0
l = 0
for i in range(n-1):
    p = a[i+1]-a[i]
    if(m < p):
        m = p
    l += p
q = k-a[n-1]+a[0]
if(m < q):
    m = q
l += q
print(l-m)
K, N = map(int, input().split())
A = list(map(int, input().split()))
 
D = [0]*N
 
for i in range(N-1):
  D[i] = A[i+1] - A[i]
D[N-1] = A[0] - A[N-1] + K
      
D.sort()
print(K - D[N-1])
l = list(map(int, input().split()))
l2 = list(map(int, input().split()))
m = abs(l[0]-l2[-1]+l2[0])
for i in range(1, len(l2)):
    m = max(m,abs(l2[i]-l2[i-1]))
print(l[0]-m)
k,n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.append(a[0]+k)
b = []
for i in range(len(a)-1):
    b.append(a[i+1] - a[i])
print(sum(b) - max(b))
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))
k, n = map(int, input().split())
As = list(map(int, input().split()))

maxdis = 0
for i in range(n):
  if i == 0:
    continue
  if As[i] - As[i-1] > maxdis:
    maxdis = As[i] - As[i-1]
if As[0] + k - As[n-1] > maxdis:
  maxdis = As[0] + k - As[n-1]
print(k - maxdis)
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())
A = list(map(int,input().split()))
A.sort()
l = [0]*N
l[0] = A[0] +K - A[N-1]
for i in range(1,N):
    l[i]= A[i] -A[i-1]

    
print(K-max(l))
k, n = map(int, input().split())
a_array = [int(x) for x in input().split()]
diff = []
tmp = 0
for i in range(n - 1):
    diff.append(abs(a_array[i + 1] - a_array[i]))
diff.append(k - a_array[-1] + a_array[0])
print(sum(diff) - max(diff))
K,N = map(int,input().split())
A = list(map(int,input().split()))
def solver(K,N,A):
    distance = [A[0] + (K - A[N-1])]
    for i in range(1,N):
        distance.append(abs(A[i]-A[i-1]))
    max_value = max(distance)
    return K - max_value
print(solver(K,N,A))
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

for i in range(1, n):
    if a[i] - a[i -1] > r:
        r = a[i] - a[i -1]

print(k - r)
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)
k,n = map(int,input().split())
n_inf = list(map(int,input().split()))
#n_inf.append(k)
#n_inf.insert(0,0)
kyori_list = [j for j in range(n-1)]
for i in range(n-1):
  kyori_list[i] = n_inf[i+1]-n_inf[i]
#print(kyori_list)
kyori_list.append((n_inf[0])+(k-n_inf[n-1]))
#print(kyori_list)
kyori_list.sort(reverse=1)
print(sum(kyori_list[1:n+1]))
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.append(A[0]+K)

D=[]
for i in range(len(A)-1):
    d=A[i+1]-A[i]
    D.append(d)

print(K-max(D))
kn = list(map(int, input().split()))
a = list(map(int, input().split()))

a.append(kn[0]+a[0])
dist = []
for i in range(1, kn[1]+1):
    dist.append(a[i]-a[i-1])

dist[dist.index(max(dist))] = 0

print(sum(dist))
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)
import math

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

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

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

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

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

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

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

print('{}'.format(k-max_sub))
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)
# n = int(input())
k,n = map(int,input().split())
A = list(map(int,input().split()))
# An = [list(map(int,input().split())) for i in range(n)]
d = []
for i in range(n-1):
    d.append(A[i+1]-A[i])
d.append(k-A[n-1]+A[0])
print(k-max(d))
k, n = map(int, input().split())
l = list(map(int, input().split()))
d = [0] * n
for i in range(n):
    if i == n - 1:
        d[i] = k - l[-1] + l[0]
    else:
        d[i] = l[i + 1] - l[i]
print(k - max(d))
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)
# 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)
k, n = map(int, input().split())
a = list(map(int, input().split()))
a2 = []
for i in range(n-1):
    d = int(a[i+1]-a[i])
    a2.append(d)
a2.append(k-a[n-1]+a[0])
ans = k - max(a2)
print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(n):
    distance.append(min(k - abs(a[i] - a[i - 1]), abs(a[i] - a[i - 1])))


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

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

print(K - m)
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

if __name__=='__main__':
    main()
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

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

total = 0
srtd = sorted(s)
for i in range(len(srtd) - 1):
  total += srtd[i]

print(total)
k, n = map(int, input().split())
mid = k / 2
a = [int(i) for i in input().split()]
max_diff = a[0]  + (k - a[-1])
for i in range(n - 1):
  diff = a[i + 1] - a[i]
  if diff > max_diff:
    max_diff = diff
print(k - max_diff)
KN = list(map(int, input().split()))
K, N = KN[0], KN[1]
A = list(map(int, input().split()))

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

print(K-max(L))
N, K = map(int, raw_input().split())

As = map(int, raw_input().split())

a = N
for i in range(len(As)):
  start = As[i]
  stop = As[i-1]
  a_t = stop-start
  if a_t <= 0: a_t += N
  if a_t < a: a = a_t

print a
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))
k, n = map(int,input().split())
p = list(map(int, input().split()))
l = []

for i in range(n-1):
  a = p[i+1]-p[i]
  x = min(a, k-a)
  l.append(x)
l.append(k-p[-1]+p[0])
  
print(k-max(l))
import numpy as np
K, N = map(int, input().split())
x_list = list(map(int, input().split()))

sub_x_list = x_list.copy()[1:]
sub_x_list.append(K + x_list[0])

for i in range(N):
    sub_x_list[i] -= x_list[i]
    
max_i = max(sub_x_list)
print(K - max_i)
k, n = list(map(int, input().split()))
positions = list(map(int, input().split()))

diffs = [-1] * (n)
for i in range(n-1):
    diffs[i] = positions[i+1] - positions[i]
diffs[n-1] = positions[0] - positions[-1] + k

print(k - max(diffs))
import sys
input = sys.stdin.buffer.readline

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

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

print(K - max(B))
K, N = map(int, input().split())
A = list(map(int, input().split()))

max_d = 0
for i in range(N-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)
m = input().split()
K = int(m[0])
N = int(m[1])

A = input().split()
a = [0] * N

for i in range(N):
  a[i] = int(A[i])

#import pdb; pdb.set_trace()
Sum = 0
n = N -1
for x in range(N):
  if x == n:
    Sum += (K - a[n]) + a[0]
  else:
    Sum += a[x+1] - a[x]

ans = [0] * N
for k in range(N):
  if k == n:
    ans[k] = (K - a[n]) + a[0]
  else:
    ans[k] = a[k+1] - a[k]

l = max(ans)

min_road = Sum - l
print(min_road)
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 = map(int,input().split())
A = list(map(int,input().split()))
tmin = 2*K
for i in range(1,N):
    tmin = min(tmin,K-(A[i]-A[i-1]))
tmin = min(tmin,A[N-1]-A[0])
print(tmin)
def resolve():
    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]))
    total = sum(dist)
    print(total-max(dist))
resolve()
k, n = map(int, input().split())
lis = []
for i in map(int, input().split()):
  lis.append(i)
  
max = k + lis[0] - lis[n-1]
for i in range(n-1):
  j = lis[i+1] - lis[i]
  if j > max:
    max = j

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

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

a_list = a_list + list(map(lambda x: x + k, a_list))

mi_len = k
for i in range(len(a_list)-(n-1)):
    mov_len = a_list[i+(n-1)] - a_list[i]
    if mi_len > mov_len:
        mi_len = mov_len

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

b = max(b, abs(a[0]+k-a[n-1]))
print(k-b)
import numpy as np
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=np.array(a)
a1=abs(a[1:]-a[:-1])
a2=list(a1)
a2.append(abs(a[0]+k-a[-1]))
print(sum(a2)-max(a2))
import sys

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


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


if __name__ == '__main__':
    solve()
# -*- coding: utf-8 -*-

def solve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = max((A[i]-A[i-1])%K for i in range(N))
    res = K-B
    return str(res)

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

diff=0
for i in range(1,n):
  if(a[i]-a[i-1]>diff):
    diff=a[i]-a[i-1]
    
last = k-a[-1]+a[0]
diff= max(diff,last)
print(k-diff)
k,n=map(int,input().split())
a=list(map(int,input().split()))
s=0
b=[0]*(n-1)
for i in range(n-1):
  b[i]=a[i+1]-a[i]
  
b.append(k+a[0]-a[n-1])

c=sorted(b,reverse=True)
for i in range(1,n):
  s+=c[i]
print(s)
k, n = map(int, input().split())
a = list(map(int, input().split()))
a += [a[0] + k]

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

print(k - diff)
import sys

x = sys.stdin.readline()
x = x.split(' ')
(k,n)=[int(str) for str in x]

x = sys.stdin.readline()
x = x.split(' ')
a=[int(str) for str in x]

prev = 0
amax = 0
sa = 0
for i in range(0,n):
  if(i == 0):
    prev = a[i]
  else:
    among = a[i] - prev
    if( amax < among):
      amax = among
    prev = a[i]
    sa += among

last = a[0] + k - a[n-1]
if( amax < last):
  amax = last
sa += last

print(sa-amax)

sys.stdout.flush()