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

max_kyori = 0
a.append(k + a[0])

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

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

max1 = 0
for i in range(1,n):
    max1 = max((a[i]-a[i-1]),max1)
max1 = max(max1,(a[0]+(k-a[n-1])))
print(k - max1)
p, n = map(int,input().split())
l = list(map(int,input().split()))
l.sort()
val = l[-1]-l[0]
for i in range(1,n):
    val = min(val, l[-1] - l[i] + p-l[-1]+l[i-1])
print(val)
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))

    max_dist = 0
    A.append(K + A[0])

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

    print(K - max_dist)


if __name__ == '__main__':
    main()
k,n = map(int,input().split())
a = list(map(int,input().split()))
dis = [0]*n
for i in range(n):
    if i == 0:
        dis[i] = a[-1] - a[0]
    else:
        dis[i] = k - a[i] + a[i-1]
print(min(dis))
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

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

d=[0]*n
d[0]=k-(a[n-1]-a[0])
for i in range(1,n):
    d[i]=a[i]-a[i-1]
print(k-max(d))
inpl = lambda: list(map(int,input().split()))
K, N = inpl()
A = inpl()
D = [A[i+1]-A[i] for i in range(N-1)]
D.append(K+A[0]-A[-1])
print(K - max(D))
k,n = map(int,input().split())
a_list = list(map(int,input().split()))

dif_list = []

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

print(k-max(dif_list))
E = [input() for i in range(2)]
E0 = E[0].split()
K = int(E0[0])
N = int(E0[1])
E12 = E[1].split()
E1 = [int(s) for s in E12]
E1.append(K + E1[0])
d = 0
M = 0
gap = []
for k in range(len(E1)-1):
    gap.append(int(E1[k+1]) - int(E1[k]))
    d += gap[k]
    M = max(M, gap[k])


d -= M

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

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

print(K - max(diffA))
k,n=map(int,input().split())
a=list(map(int,input().split()))+[k]
d=abs(a[0]+(k-a[-2]))
for i in range(n):
  d=max(d,abs(a[i]-a[i+1]))
print(k-d)
K,N = map(int,input().split())
A = list(map(int,input().split()))
min_distance = 10000000
distance = 0

for i in range(N):
  if i == 0:
    distance = A[N-1] - A[0]
  elif i == N-1:
    distance = (K-A[i])+A[i-1]
  else:
    distance = (K-A[i+1])+A[i]
  if distance < min_distance:
    min_distance = distance
print(min_distance)
if __name__ == "__main__":
    k, n = map(int, input().split())
    a = list(map(int, input().split()))

    for i in range(n):
        a.append(a[i] + k)

    ans = 2 * k

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

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

# d[i] = i番目とi+1番目の間の距離（d[n]はnと0番目の家の距離）
d = [0 for i in range(n)]
for i in range(n-1) :
  d[i] = a[i+1] - a[i]
d[n-1] = k - a[n-1] + a[0]

print(sum(d)-max(d))
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.append(A[0]+K)
res = []
for i in range(N):
  res.append(abs(A[i]-A[i+1]))
print(K - max(res))
a=input().split(" ")
k=int(a[0])
n=int(a[1])
b=input().split(" ")
num=[]
for i in range(n):
    num.append(int(b[i]))

num=sorted(num)
dis_list=[]
for i in range(n):
    if i==0:
        dis=num[0]+k-num[n-1]
    else:
        dis=num[i]-num[i-1]
    dis_list.append(dis)
dis_list=sorted(dis_list)
print(k-dis_list[n-1])
k,n=input().split();
a=input().split();
k=int(k);
n=int(n);
by=0;
for i in range(n):
	a[i]=int(a[i]);
for i in range(n-1):
	if a[i+1]-a[i]>by:
		by=a[i+1]-a[i];
if (k-a[n-1])+a[0]>by :
	by=(k-a[n-1])+a[0];
print(k-by);
K,N=map(int,input().split())
A=list(map(int,input().split()))
ans=K
for i in range(1,N-1):
  ans=min(ans,K-abs(A[i]-A[i-1]),K-abs(A[i-1]-A[i]),K-abs(A[i]-A[i+1]),K-abs(A[i+1]-A[i]))

ans=min(ans,K-K+abs(A[0]-A[-1]),K-abs(A[0]-A[1]))

ans=min(ans,K-abs(A[-1]-A[-2]),K-abs(K-A[-1]))

print(ans)
circle_meter, N = [int(i) for i in input().split()]
house = [int(i) for i in input().split()]

max_distance = 0

for i in range(N):
	if i == N-1:
		if max_distance < (house[0] + circle_meter - house[i]):
			max_distance = house[0] + circle_meter - house[i]
		else:
			break
	elif max_distance < (house[i+1] -house[i]):
		max_distance = (house[i+1]-house[i])
	
result = circle_meter - max_distance
print(result)
K,N = map(int,input().split())
cc = list(map(int,input().split()))
min_length = max(cc)-min(cc)
for i in range(1,N):
  Length = K-cc[i]+cc[i-1]
  min_length = min(min_length,Length)
print(min_length)
import copy
K,N = (int(x) for x in input().split())
a = list(map(int,input().split()))
b = copy.copy(a)
tmp = a[N-1]
for i in range(N-1):
  a[i+1] -= a[i]
a[0] += K-tmp
d1 = sum(a)-max(a)
b = b[::-1]
tmp =b[0] 
for i in range(N-1):
  b[i] -= b[i+1] 
b[N-1] += K-tmp
d2 = sum(b)-max(b)
print(min(d1,d2))
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, k-max(a[i],a[i-1])+min(a[i],a[i-1]))
print(ans)
k, n = map(int, input().split())
h_list = list(map(int, input().split()))
distance = []
tmp = h_list[0]
for i in range(len(h_list)):
    a = h_list[i] - tmp
    distance.append(a)
    tmp = h_list[i]
x = (k - h_list[len(h_list)-1] )+ h_list[0]
distance.append(x)
sorted_d_list = sorted(distance)
sorted_d_list.pop()
print(sum(sorted_d_list))
k , n = list(map(int,input().split()))
A = list(map(int,input().split()))

X = list()
for i in range(n-1):
    x = A[i+1]-A[i]
    X.append(x)

y = k-A[n-1]+ A[0]

X.append(y)

m = max(X)

print(k-m)
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 = (int(x) for x in input().split(' '))
A = [int(x) for x in input().split(' ')]
#各家の間の距離も求めてGに追加していく
G = []
for i in range(len(A)-1):
  G += [A[i+1]-A[i]]
#先頭と最後の1軒距離を求める
G += [(K - (A[N-1] - A[0]))]
#一番幅の広い距離を避けて一周回る
print(sum(G)-max(G))
p,n=map(int,input().split())
l=list(map(int,input().split()))
s=0
l1=[]
for i in range(1,n):
    l1.append(l[i]-l[i-1])
    s+=l1[-1]
l1.append(p-s)
l1.remove(max(l1))
print(sum(l1))
k,n=map(int,input().split())
A=list(map(int,input().split()))
ans=A[0]+k-A[n-1]
for i in range(n-1):
    ans=max(ans,A[i+1]-A[i])
print(k-ans)
k,n=map(int,input().split())
arr=list(map(int,input().split()))
ans=arr[n-1]-arr[0]
for i in range(n-1):
    ans=min(ans,k-arr[i+1]+arr[i])
print(ans)
k, n = list(map(int, input().split(' ')))
a_list = list(map(int, input().split(' ')))

max_gap = max([abs(a_list[i-1] - a_list[i]) for i in range(1, n)])
if max_gap < (a_list[0] + k - a_list[-1]):
    max_gap = a_list[0] + k - a_list[-1]
print(k - max_gap)
k,n=list(map(int,input().split()))
A=list(map(int,input().split()))
A.append(k+A[0])
B=[0]*n
for i in range(n):
  B[i]=A[i+1]-A[i]
print(k-max(B))
def main():
    tmp = input().split(" ")
    k = int(tmp[0])

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

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


if __name__ == '__main__':
    main()
a,b=map(int,input().split())
S=list()
L=list(map(int,input().split()))
for i in range(1,b):
  S.append(L[i]-L[i-1])
S.append(a+L[0]-L[-1])
print(a-max(S))
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]))
k, n = [int(x) for x in input().split()]

a_list = [int(x) for x in input().split()]
a_list.append(k + a_list[0])

d_list = [a_list[i + 1] - a_list[i] for i in range(n)]
print(k - max(d_list))
import numpy as np

K, N = [int(element) for element in input().split(" ")]
AList = np.array([int(element) for element in input().split(" ")])
distance = np.diff(AList)
start_end_distance = abs((K - AList[-1]) + AList[0])
distance = np.append(distance, start_end_distance)

print(K - np.max(distance))
K, N = list(map(int, input().split()))
an = list(map(int,input().split()))

min_list = []
for i in range(N):
    if i == 0:
        left = an[i] + K - an[-1]
        right = an[i+1] - an[i]
    elif i == N-1:
        left = an[i] - an[i-1]
        right = an[0] + K - an[N-1]
    else:
        left = an[i] - an[i-1]
        right = an[i+1] - an[i]
    min_list.append(min(K-left, K-right))
print(min(min_list))
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

K, N = LI()
A = LI()

ans = mod
diff = [0 for i in range(N)]
for i in range(N - 1):
    diff[i] = A[i + 1] - A[i]
diff[-1] = (K - A[-1]) + A[0]
# print(diff)
ans = K - max(diff)
print(ans)
import sys


def main():
    read = sys.stdin.buffer.read
    k, n, *A = map(int, read().split())
    A += [k + A[0]]
    far = max(x - y for x, y in zip(A[1:], A))
    print(k - far)


if __name__ == "__main__":
    main()
import numpy as np
K,N=map(int, input().split())
A=list(map(int, input().split()))
K,N=int(K),int(N)
b=[]
for i in range(0,N-1):
    b.append(A[i+1]-A[i])
b.append(A[0]+K-A[N-1])
c=np.sum(b)
print(c-max(b))
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
dist=[0]*(n)

for i in range(n-1):
    dist[i]=a[i+1]-a[i]
dist[n-1]=k-a[n-1]+a[0]
ret=10**7
for i in range(n):
    ret=min(ret,k-dist[i])

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

#find distance
d_ls = []
for i in range(1, n):
  d = abs(a[i] - a[i-1])
  d_ls.append(d)

# final distance
d = k - sum(d_ls)
d_ls.append(d)

#delete maximum distance
d_ls.remove(max(d_ls))

print(sum(d_ls))
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.append(K)

temp = 0
for i in range(N):
  if temp < A[i+1] - A[i]:
    temp = A[i+1] - A[i]
A.pop(-1)

ans = K - max(temp, A[0] - A[-1] + K)

print(ans)
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]))
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)
def main():
  K, N = map(int, input().split())
  A = [int(a) for a in input().split()]
  maxK=0
  for i in range(1,N):
    maxK = max(maxK, A[i]-A[i-1])
  maxK = max(maxK, K+A[0]-A[N-1])
  print(K-maxK)

if __name__=='__main__':
  main()
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))
# -*- coding: utf-8 -*-
K,N = map(int, input().split())
houses = list(map(int, input().split()))
M = N - 1
lis = []
a = 0
while M > 0:
    lis.append(houses[a+1] - houses[a])
    M -= 1
    a += 1
lis.append(K - houses[N-1] + houses[0])
hou = sorted(lis, reverse = True)
del hou[0]
print(sum(hou))
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

arr = list(map(int, input().split()))
# arr.append(arr[0])

maxv = -float('inf')

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

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

print (k - maxv)
import numpy as np


##get int
first_input = list(map(lambda x:int(x), input().split(" ")))
second_input = list(map(lambda x:int(x), input().split(" ")))

##get float
#first_input = list(map(lambda x:float(x), input().split(" ")))
#second_input = list(map(lambda x:float(x), input().split(" ")))

##get string
#first_input = input().split(" ")

K = first_input[0]
N= first_input[1]

A_list = second_input

A_list.append(K+A_list[0])

dist_list = np.array(A_list)[1:]-np.array(A_list)[:-1]

print(sum(dist_list)-max(dist_list))
k,n=map(int,input().split())
a=list(map(int,input().split()))

kyori=[0]*n

for i in range(n-1):
    kyori[i]=a[i+1]-a[i]

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

print(k-max(kyori))
n = list(map(int,input().split()))
m = list(map(int,input().split()))

m.sort(reverse=True)
kyori = []
for x in range(n[1]-1):
    kyori.append(m[x]-m[x+1])

kyori.append((m[len(m)-1]+n[0])-m[0])
print(sum(kyori)-max(kyori))
K,N=map(int,input().split())
A=list(map(int,input().split()))
mx=0
for i in range(1,N):
    mx=max((A[i]-A[i-1]),mx)
mm=A[0]+(K-A[N-1])
mx=max(mx,mm)
print(K-mx)
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))
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

tmp = 0

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

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

print(k-tmp)
import numpy as np
K, N = (int(i) for i in input().split())  
A = [int(i) for i in input().split()] 

A.append(A[0] + K)
lis = sorted(list(np.diff(A)))
print(sum(lis[:-1]))
K,N = map(int,input().split())
As = list(map(int,input().split()))

two_loops = As + [A+K for A in As]

max_dist = 0
max_dist_index = 0
for i in range(len(two_loops)-1):
    new_dist = two_loops[i+1]-two_loops[i]
    if new_dist > max_dist:
        max_dist = new_dist
        max_dist_index = i+1
print(two_loops[max_dist_index+N-1]-two_loops[max_dist_index])
K, N = map(int, input().split())
A = list(map(int, input().split()))

distance = []

A.sort()

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

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

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

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

print(k - d_max)
pond, house = map(int, input().split())
li = list(map(int, input().split()))

li.append(pond+li[0])
maxl=0

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

deff_list = []

for i in range(len(a)-1):
    deff_list.append(a[i+1]-a[i])

deff_list.append(min(a)+(k-max(a)))

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

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

#0と最後
max_ = [0, K+A[0]-A[-1]]

#iとi+1
for i in range(1, N):
    tmp = A[i] - A[i-1]
    if tmp > max_[1]:
        max_[0] = i
        max_[1] = tmp
print(K-max_[1])
import sys
read = sys.stdin.buffer.read
k, n, *A = map(int, read().split())
far = k + A[0] - A[-1]
y = A[0]
for x in A:
    if far < x - y:
        far = x - y
    y = x
print(k-far)
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
res = 0
for i in range(N-1):
    res = max(res, A[i+1] - A[i])
else:
    res = max(res, K - A[-1] + A[0])
print(K - res)
k,n = map(int,input().split())

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

x_list = A_list[:-1]

y_list = A_list[1:]

diff_list = list(map(lambda x,y: y - x, x_list, y_list))
diff_list.append(int(K-A_list[-1] + A_list[0]))

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

dis = [0] * N

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

print(K-max(dis))
k,n=map(int,input().split())
l=list(map(int,input().split()))
l=l+[l[0]+k]
ans=k
for i in range(n):
  ans=min(ans,k-(l[i+1]-l[i]))
print(ans)
l = input().split()
L = [int(c) for c in l]
K = L[0]
N = L[1]
A = [int(b) for b in input().split()]

A_delta = [A[0] + K - A[N-1]]
for n in range(N-1):
  A_delta.append(A[n+1] - A[n])
 
print(K - max(A_delta))
k,n=(int(x) for x in input().split())

a=list(map(int,input().split()))
d0=0
dmax=0
for i in range(n):
    d=a[i]-d0
    d0=a[i]
    if d>=dmax:
        dmax=d
d1=k-a[n-1]+a[0]
if d1>=dmax:
    print(str(k-d1))
else:
    print(str(k-dmax))
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0])

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

ans = min(K-Big, max(A)-min(A))
print(ans)
import numpy as np

K,N = map(int, input().split())
An = list(map(int, input().split()))
arr = np.array(An)
arr = np.diff(arr)
ans = list(arr)
ans.append(K-An[-1]+An[0])
# print(ans)
ans.remove(max(ans))
print(sum(ans))
import sys
input=lambda: sys.stdin.readline().rstrip()
k,n=map(int,input().split())
A=[int(i) for i in input().split()]
ans=k-(k-A[-1]+A[0])
for i in range(n-1):
  ans=min(ans,k-A[i+1]+A[i])
print(ans)
def n1():return [int(x) for x in input().split()]
k,n=n1()
a=n1()
 
ans=0
for i in range(n-1):
    ans=max(a[i+1]-a[i],ans)
print(k-(max(ans,k-a[n-1]+a[0])))
k, n = map(int, input().split())
a = list(map(int, input().split()))
num = 0
for i in range(n-1):
    x = a[i+1]-a[i]
    num = max(x, num)
f = (k - a[n-1]) + a[0]
if num >= f:
    print(k - num)
else:
    print(k - f)
k, n = (int(x) for x in input().split())
seq = sorted((int(x) for x in input().split()))
ans = float('inf')
for i in range(n):
    if seq[i] > seq[i-1]:
        d =  k - (seq[i] - seq[i-1])
    else:
        d = seq[i-1] - seq[i]
    ans = min(ans, d)
print(ans)
# print(min(seq[-1] - seq[0], k-seq[-1] + seq[-2]))
a, b = map(int, input().split())
*A, = map(int, input().split())
c = []
for i in range(1, b):
    c.append(A[i]-A[i-1])
c.append(a-sum(c))
print(a-max(c))
K, N = map(int, input().split())
A = list(map(int, input().split()))
distances_between_houses = []
for i in range(N):
    if i == N - 1:
        distances_between_houses.append(A[0] + (K - A[-1]))
    else:
        distances_between_houses.append(A[i + 1] - A[i])
distances_between_houses.sort()
distances_between_houses.pop()
print(sum(distances_between_houses))
from os import readlink


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

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

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

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

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


minb = max(b)
print(K-minb)
n,k = map(int,input().split())
a = list(map(int,input().split()))

lis = []
for i in range(k-1):
  lis.append(a[i+1]-a[i])
lis.append(a[0]-a[-1]+n)
print(sum(lis)-max(lis))
k, n = map(int, input().split())
a = list(map(int, input().split()))
a += [k]
x = [a[0]]
for i in range(n):
    _a = a[i+1] - a[i]
    x += [_a]
x[0] = x[0]+x[-1]
x = x[:-1]
x.remove(max(x))
print(sum(x))
K,N=map(int,input().split())
A=list(map(int,input().split()))
R=[]
for i in range(N):
    if i == N-1:
        R.append(abs(A[i]-(K+A[0])))
    else:
        R.append(abs(A[i]-A[i+1]))
R.sort(key=int)
print(K-R.pop(-1))
K, N = map(int, input().split())
l = list(map(int, input().split()))

l.append(K+l[0])
l.insert(0, 0)

lsub = list()
for i in range (1, N+2):
    lsub.append(l[i] - l[i-1])

print(K - max(lsub))
K, N = map(int, input().split())
Alist = list(map(int, input().split()))

# print(K, N)
# print(Alist)

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

gapmax = 0

for i, gap in enumerate(gaps):
    # if gapmax < gap:
    gapmax = max(gapmax, gap)
print(K - gapmax)
K, N = map(int, input().split())
A = list(map(int, input().split()))

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

l = l + [i + K for i in l]

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

print(ans)
K, N = map(int, input().split()) 
A = list(map(int, input().split()))
B = []
B.append(A[0] - A[N-1] + K )

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

print (K - max(B))
c, n = map(int, input().split(' '))

l=list(map(int, input().split(' ')))

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

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

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

print(min(K-M,A[N-1]-A[0]))
k,n = map(int, input().split())
a = list(map(int, input().split()))
a.append(k+a[0])
l = 0
for i in range(n):
  l = max(a[i+1] - a[i], l) 
print(k - l)
K,N=map(int, input().split())
A=list(map(int, input().split()))
A.append(A[0]+K)
l = 0
for i in range(N):
    l = max(l, A[i+1]-A[i])
print(K-l)
k,n=[int(i) for i in input().strip().split(" ")]
arr=[int(i) for i in input().strip().split(" ")]
maxDist=arr[0]+min(k-arr[-1],arr[-1]) 
# maxDist=max(arr)-min(arr)
for i in range(n-1):
    maxDist=maxDist if arr[i+1]-arr[i] <=maxDist else arr[i+1]-arr[i]
print(k-maxDist)
k,n = map(int,input().split())
A = [int(i) for i in input().split()]

A.append(A[0] + k)

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

print(k - l)
i = list(map(int, input().split()))
j = list(map(int, input().split()))
a=j[i[1]-1]-j[0]
if a*2>i[0]:
    a=i[0]-a
for k in range(i[1]-1):
    if j[k+1]-j[k]>a:
        a=j[k+1]-j[k]
print(i[0]-a)
K, N = map(int, input().split())
A = list(map(int, input().split()))
dis = []
dis.append(A[0]-A[N-1]+K)
for i in range(N-1):
    dis.append(A[i+1]-A[i])
print(K-max(dis))
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 10 ** 6
for i in range(N):
	if i > 0:
		x = K - A[i] + A[i - 1]
	else:
		x = A[i - 1] - A[i]
	ans = min(ans, x)
print(ans)
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    k,n=map(int,input().split())
    A=[int(_) for _ in input().split()]
    max_len=(k-A[-1])+A[0]
    for i in range(n-1):
        max_len=max(max_len,A[i+1]-A[i])
    print(k-max_len)

if __name__=='__main__':
    main()