peterverebics/CodeNet_Python_118 huggingface dataset:
def main():
    import sys
    input=sys.stdin.readline
    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,A[0]+k-A[-1])
    print(m)
if __name__ == '__main__':
    main()
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(K)
    diff = []
    for i in range(N):
        diff.append(A[i+1]-A[i])
    
    print(sum(diff)-max(diff))
    

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

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

for n in range(0, N):
  maxD = max(maxD, A[n+1] - A[n])
  
print(maxD)
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.append(A[0]+K)
l=0
for i in range(N-1):
    l=max(l,A[i+1]-A[i])
 
print(K-l)
k,n=map(int,input().split())
a=list(map(int,input().split()))+[k]
d=abs(a[0]+(k-a[-1]))
for i in range(n):
  d=max(d,abs(a[i]-a[i+1]))
print(k-d)
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().split())))
maxi = -1
for i in range(0, len(a)-1):
    tmp = max(abs(a[i] - a[i-1]), abs(a[i] - a[i+1]))
    if maxi == -1:
        maxi = tmp
    else:
        maxi = max(maxi, tmp)
print(k - maxi)
n,k=map(int,input().split())
max=0
l=list(map(int,input().split()))

for i in range(k-1):
  s=l[i+1]-l[i]
  if s>max:
    max=s

s=n-l[-1]+l[0]
if s>max:
  max=s
print(max)
perimeter,n = map(int, input().split())
v = list(map(int, input().split()))
f , l= (v[n-1]-v[0]),  (perimeter - (v[n-1]-v[n-2]))
print(min(f, l))
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

def get_distances(A):
    distances = []
    for i in range(1, len(A)):
        prev = A[i-1]
        curr = A[i]
        distances.append(abs(prev-curr))
    return distances
  
K, N = int_spaced_input()
A = list(int_spaced_input())

distances = get_distances(A)
# add last house near the north
distances.append(K - A[-1])

maximum_segment = max(distances)
minimum_travel = sum(distances) - max(distances)

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

hanbun = k /2
a.sort
print(a)
i = 0
while True:
    if a[i] != 0:
        end = a[i]
        break
    else:
        i += 1
start = a[n-1]


result = start - end
if result < 0:
    result = result * -1
print(result)
# coding: utf-8
import itertools

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

for i in range(n-1):
    l = a[i+1] - a[i]
    if l > max:
        max = l
l = a[0] - a[n-1] + 20
if l > max:
    max = l
print(k - max)
K,N = [int(i) for i in input().split()]
A = sorted([int(i) for i in input().split()])
print(K - max(y-x for x,y in zip(A,A[1:])))
import sys
K,N=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

m=float(K)/2

AA=[]
for a in A:
    if a<=m:
        AA.append(a)
    else:
        AA.append(a-K)
AA.sort()
print AA[-1]-AA[0]
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=0
s=[0]*n
#間隔を求める
for i in range(n-1):
    s[i]=a[i+1]-a[i]
#間隔を足す
for i in range(n):
  if s[i]<k/2:
    b=b+s[i]
  else:
    con=k-s[i]
    print(con)
    exit()
  
print(b)
k, n = map(int, input().split())
li = sorted(list(map(int, input().split())))
res = 0
if k - li[-1] + li[0] > li[1] - li[0]:
    res += li[1] - li[0]
    end = n - 1
    start = 1
else:
    res += k - li[-1] + li[0]
    end = n - 2
    start = 0

for i in range(start, end):
    res += li[i+1] - li[i]
print(res)
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(dis[n-1])
K, N = map(int, input().split())
A = list(map(int, input().split()))
diff_list = []
sum_val = 0
for i in range(N):
    diff_list.append(A[i]-sum_val)
    sum_val += A[i]
diff_list.append(K-A[-1])
print(sum(diff_list)-max(diff_list))
K,N = map(int,input().split())
dis = list(map(int,input().split()))
print(sum(dis)-max(dis))
K,N=map(int,input().split())
A= list(map(int,input().split()))
maxA =20-(A[N-1]-A[0])
sumA =20-(A[N-1] -A[0])
for i in range(N-1):
    sumA += A[i+1]-A[i]
    if A[i+1]-A[i] >maxA:
        maxA=A[i+1]-A[i]
print(sumA-maxA)
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.append(K+A[0])
ans = 0
for i in range(N):
    ans = max(ans, A[i+1]-A[i])
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))
k,n=map(int,input().split())
lista=list(map(int,input().split())) 
ans=[]
for i in range(1,n):
  ans.append(lista[i]-lista[i-1])
print(k-max(ans))
K, N  = map(int,input().split())
As = list(map(int, input().split()))

distanceList = []

start = 0
for index, dist in enumerate(As):
  distanceList.append(abs(start - dist))
  start = dist
distanceList.append(abs(As[-1] - K))


total_dist_list = []
for index, dist in enumerate(distanceList):
  total_dist = 0
  for i in range(N):
    total_dist += distanceList[index - i]
  total_dist_list.append(total_dist)

print(min(total_dist_list))
K, N = [int(i) for i in input().split()] 
AL =[int(i) for i in input().split()] 

RL = []

for i in range(N):
  if AL[i] == 0 or AL[i]<(K/2):
    RL.append(AL[i])
  else:
    RL.append(AL[i] - K)


L = []
l = 0

for i in range(N):
  for j in range(N):
    l = l + abs(RL[j] - RL[i])
  L.append(l)
  l = 0

print(min(L))
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
a.reverse()
for i in range(n-1):
    a[i]-=a[i+1]-a[n-1]
ans = k-max(a)
print(ans)
k, n = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
re = 0
for i in A[::-1]:
    re = i - re
print(re)
k,n=map(int,input().split())
l=list(map(int,input().split()))
ans=1000000
for i in range(n-1):
  d = 0
  d+=min(abs(l[i]-l[i-1]),abs(k-l[i-1]))
  d+=min(abs(l[i]-l[i-2]),abs(k-l[i-2]))
  if ans > d:
    ans=d
print(ans)
K,N=map(int,input().split())
A=list(map(int,input().split()))
d=0
for i in range(N-1):
  if d == 0 and A[i+1]-A[0] >= K/2:
    d=d+A[i]-A[0]+K-A[i+1]
if d == 0:
  d=d+A[N-1]-A[N-2]
print(d)
K, N = map(int,input().split())
N = N-1
A = list(map(int, input().split()))
A_d = []
[A[i+1]-A[i] for i in range(0, len(A)-1)]
A_d.append(K-A[-1]+A[0])
A_d2 = (A_d+A_d[1:])*2
res = K
for i in range(len(A_d2)-1):
    if res > sum(A_d2[i:i+N]):
        res = sum(A_d2[i:i+N])
print(res)
def start(k, n, a):
    max_distance=0
    for i in range(n-1):
        if(a[i+1]-a[0]>max_distance):
            max_distance = a[i+1]-a[i]
    if(k-a[-1]+a[0]>max_distance):
        max_distance = k-a[-1]+a[0]
    print(k-max_distance)

k,n = map(int, input().split())
a = list(map(int, input().split()))
start(k, n, a)
K, N = map(int, input().split())
A = list(map(int, input().split()))

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

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

for _ in range(len(a)-1):
    if diff < abs(a[_]-a[_+1]):
        diff = abs(a[_]-(a[_+1]))

print(k-diff)
kn = list(map(int, input().split()))
a = list(map(int, input().split()))

k = kn[0]
n = kn[1]

list = []
for i in range(n):
    if i == 0:
        list.append(a[i])
    else:
        list.append(a[i] - a[i-1])

    if i == n-1:
        list.append(k - a[i])

ans = 0
is_zeroPoint = False
for ele in list:
    ans += ele
    if ele == 0:
        is_zeroPoint = True


max = max(list)
if is_zeroPoint == True:
    ans -= max
else:
    secondMax = sorted(list)[-2]
    ans -= max+secondMax

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

if (0 not in A) and (K not in A):
    print(max(A) - min(A))
else:
    a = max(A) - min(A)
    k = []
    for n in range(N):
        if n + 1 == N:
            pass
        else:
            k.append(A[n + 1] - A[n])
    b = K - max(k)
    if a > b:
        print(b)
    else:
        print(a)
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])    
print(sum(d)-2*min(d))
k,n=map(int,input().split())
a=list(map(int,input().split()))
l=len(a)
if a[-1]-a[0]<=(k-a[-1])+a[0]:
    print(a[-1]-a[0])
else:
    for i in range(n-1):
        if a[-1]-a[i]>(k-a[-1])+a[i]:
            start=i
        else:
            print(((k-a[-1])+a[i])+(a[-1]-a[i+1]))
            break
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

k, n, *lst = map(int, read().split())
lst.sort()
dis = 0
for i in range(n):
    if i == 0:
        dis = max(dis, k+lst[0]-lst[-1])
    else:
        dis = max(dis, lst[i] - lst[i - 1])

dis = min(dis, k - dis)
print(dis)
#C

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

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

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

d.append(k - a[n-1])

print(sum(d) - max(d))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
around = a[0]
number = a[1]
dis = []
i = 0
for i in range(number):
  if i == number-1:
    dis.append(around-b[i])
  else:
    dis.append(b[i+1]-b[i])
  i=i+1
dis_m = max(dis)
dis_sum=sum(dis)-dis_m
print(dis_sum)
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

a = sorted(a)
if a[0] == 0:
    if n > 3:
        print(min(a[1] + (k - a[2]), a[n - 1]))
    else:
        print(abs(a[n - 1] - a[1]))
else:
    print(abs(a[n - 1] - a[0]))
import numpy as np

k, n = map(int, input().split())
a_list = [int(i) for i in input().split()]
a_list += [a_list[0]+k]

a_list = np.array(a_list)

dif = a_list[1:] - a_list[:-1]
dif_max = np.max(dif)

print(dif_max)
k,n=map(int,input().split())
b=list(map(int, input().split()))
b.sort()
a=[b[0]+k-b[n-1]]
for i in range(n-1):
    a.append(b[i+1]-b[i])
    print(b[i])
print(k-max(a))
k, n = map(int, input().split())
a = list(map(int, input().split()))
b = max(a)
print(k - b)
k, n = map(int, input().split())
a = list(map(int, input().split()))
b = []
b.append(a[n-1] - a[0])
for i in range(n-1):
    b.append(k - a[i + 1] - a[i])
print(max(b))
K, N = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))
A.extend([K])
diff = []
for i in range(N):
    diff.append(A[i+1] - A[i])
sum_d = sum(sorted(diff)[:N-1])
print(sum_d)
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
min_=0
max_=0
for i in range(n):
    l=a[i]-k
    c=0
    if abs(l)<a[i]: #左採用
        c=1
    if c==0:
        if max_<a[i]:
            max_=a[i]
    else:
        if l<min_:
            min_=l
print(min(max_-min_,a[-1]-a[0]))
K,N=map(int, input().split())
li = list(map(int,input().split()))

s=min(K/2,li[N-1]-li[0])
print(int(s))
k, n = map(int, input().split())
a = list(map(int,input().split()))
dis = 0
if min(a) == 0:
    a.remove(0)

shortest = min(a)
longest = max (a)

if longest - shortest >= k/2:
    dis = k-(longest-shortest)
else:
    dis = longest-shortest

print(dis)
k,n=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()
print(k-(a[-1]-a[-2]))
k, n = map(int, input().split())
a = list(map(int, input().split()))
mid = k // 2
sa = [k] * n
for i in range(1, n):
    sa[i - 1] = min(k - a[i], a[i] - a[i - 1])
print(sa)
sa.sort()
sum = sum(sa) - sa[-1]
print(sum)
K, N = map(int,input().split())
A = list(map(int,input().split()))

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

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

print(dist)
def rMap():
	return map(int,input().split())
def rList():
	return list(map(int,input().split()))
k,n = rMap()
a = sorted(rList())
z = 1000000
for i in range(n):
	if a[i]>k//2 and i!=0:
		z = a[i-1]+k-a[i]
print(min(z, a[-1]-a[0]))
k, n = map(int, input().split())
a = list(map(int, input().split()))

if a[n-1] + a[0] >= k:
    print(a[n-1] - a[0])
else:
    max_n = 0
    for i in range(n-1):
        diff = abs(a[i]-a[i+1])
        max_n = max(diff, max_n)
    print(k-max_n)
k, n = (int(x) for x in input().split())
seq = sorted((int(x) for x in input().split()))
print(min(seq[-1] - seq[0], k-seq[-1] + seq[-2]))
k,n = map(int, input().split())
a = list(map(int, input().split()))

a.sort()
x = a[-1] - a[0]
y = k - a[-1] + a[-2]

print(min(x,y))
k,n = map(int, input().split())
A = [int(x) for x in input().split()]
c=[]
for i in range(n-1):
  c.append(A[i+1]-A[i])
c.append(k-A[n-1])
print(k-max(c))
k,n=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
if l[0]==0:
    l=l[1:]
t=l[0]
p=0
for val in l:
    p+=(val-t)
    t=val
print(p)
#l=l[::-1]
#t=l[0]
#r=0
#for val in l:
#    r+=(t-val)
#    t=val
#print(min(r,p))
K, N = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
val_index = 0
while val_index < len(A):
    if A[val_index] == 0:
        del A[val_index]
        val_index -= 1
    val_index += 1
print(int(sum(A)/len(A)))
k,n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

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

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

print(k - d)
k,n=map(int,input().split( ))
a=sorted(list(map(int,input().split( ))),reverse=True)
for i in range(n):
 a.append(int(a[i])+k)
b=sorted(a)
w=10000000
for i in range(2*n-2):
 if a[i+2]-a[i]<w:
  w=a[i+2]-a[i]
print(-w)
k,n=map(int,input().split())
l=list(map(int,input().split()))
l.append(l[0]+k)
a=10**10
for i in range(n):
    a=min(a,l[i+1]-l[i])
print(k-a)
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ma = 0
for i in range(n - 1):
    tmp = a[i + 1] - a[i]
    if ma < tmp:
        ma = tmp
print(max(ma, a[0] + (k - a[-1])))
k,n=map(int,input().split())
a=list(map(int,input().split()))

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

print(k-length)
X, N = map(int,input().split())
A = list(map(int, input().split()))
distance = []
for i in range(N):
    if i < N-1:
        distance.append(A[N-1]+A[0])
        distance.append(X-A[i+1]+A[i])
    else:
        distance.append(X-A[N-1]+A[0])
        distance.append(A[N-1]-A[0])
print(min(distance))
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    a.append(k)
    before = a[0]
    new = []
    for i in a[1:]:
        new.append(i - before)
        before = i
    print(sum(new) - max(new))


if __name__ == "__main__":
    main()
def solve(K, N, As):
    longest = 0
    for i in range(len(As)):
        if i == len(As) - 1:
            longest = max(longest, As[0] + (K - As[i]))
        elif i == 0:
            longest = max(longest, As[i])
        else:
            longest = max(longest, As[i] - As[i - 1])
    print(K - longest)


if __name__ == '__main__':
    K, N = map(int, input().split())
    As = [int(a) for a in input().split()]
    solve(K, N, As)
K,N=map(int,input().split())
A=list(map(int,input().split()))
for i in range(N):
    if i==0:
        l = abs(A[-1]-A[0])
    else:
        l = max(l,abs(A[i-1]-A[i]))
print(K-l)
# 入力の取得
k, n = map(int, input().split())
a = list(map(int, input().split()))

min_distance = k + 1
for index, x in enumerate(a):
    start = a[index]
    goal = a[(index + k) % n]

    if start < goal:
        work_distance = goal - start
    else:
        work_distance = k - start + goal

    if work_distance < min_distance:
        min_distance = work_distance

print(min_distance)
k, n= map(int,input().split())
a = list(map(int,input().split()))
for i in range(1,n):
  a[i] -= a[i-1]
a[0] += k - a[-1]
a += [k-sum(a)]
print(k-max(a))
k, n = map(int, input().split())
a = list(map(int, input().split()))
s0 = min(a[0], k - a[0])
s1 = min(a[1], k - a[1])
s2 = min(a[2], k - a[2])
ans = s0+s1+s2
print(ans)
k,n=map(int,input().split())
a = list(map(int,input().split()))
a.append(k-a[-1]+a[0])
print(sum(a)-max(a))
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K, N = list(map(int,_in[0].split(' ')))  # type:list(int)
    A_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt1 = max(A_arr) - min(A_arr)
    A_arr_dist = [_-K if K-_<_ else _ for _ in A_arr]
    cnt2 = max(A_arr_dist) - min(A_arr_dist)
    cnt = min(cnt1,cnt2)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)
K,N = (int(x) for x in input().split())
al = [int(x) for x in input().split()]
d = [al[x+1]-al[x] for x in range(0, N-1)]
d.append(al[-1]-al[0])
print(sum(d)-max(d))
def main():
  K,N = map(int, input().split())
  A_list = list(map(int, input().split()))

  space_max = 0
  space_sum = 0

  if A_list[-1] - A_list[0] <= K/2:
    print(A_list[-1] - A_list[0])
    exit()
  else:
    pass

  for i in range(N-1):
    space =  A_list[i+1] - A_list[i]
    space_sum += space
    if space > space_max:
      space_max = space
    else:
      pass
  
  if space_max >= K/2:
    print(K-space_max)
  else:
    print(space_sum)
a,b = [int(i) for i in input().split()]
l  = [int(i) for i in input().split()]

l2 = [i for i in l if i <= (a//2)]
l3 = [a - i for i in l if i > (a//2)]
print(min(l2[-1] + l3[0],l2[-1] + (a - l3[0] - l2[-1]),l[-1] - l[0]))
K, N = map(int, input().split())

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

rec = list()

rec.append(K - A[-1])

for i, _ in enumerate(A[:-1]):
    rec.append(A[i+1] - A[i])

sum = 0
for r in rec:
    sum += r

max_r = max(rec)
print(sum - max_r)
# coding:utf-8
k, n = map(int, input().split())
a = list(map(int, input().split()))
distance = []
for i in range(n):
  if i == n-1:
    distance.append(k - a[i])
  else:
  	distance.append(a[i+1] - a[i])
distance.sort()
print(sum(distance[:-1]))
K,N = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
P=[A[0]]
for i in range(len(A)-1):
    a = abs(A[i+1] -A[i])
    P.append(a)
P.append(K - A[-1] + A[0])
P.sort()
del P[-1]
sum(P)
print(P)
k, n = map(int, input().split())
a = list(map(int, input().split()))

if 0 in a :
  a.sort()
  print((k - max(a)) + a[1])
else :
  print(max(a) - min(a))
k,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
 
sol=[]
for i in range(n-1):
  sol.append(a[i+1]-a[i])
sol.append(a[0]-a[n-1]+k)
 
sorted(sol)
#unwanted segment of max length removed while printing
print(sum(sol[0:n-1]))
K,N = map(int, input().split())
A = list(map(int, input().split()))

dist = []
for i in range(N):
  if i == N-1:
    if A[0] == 0:
      dist.append(K-A[i])
    else:
      dist.append(A[i]-A[0])
  else:
    dist.append(A[i+1]-A[i])
max_dist = max(dist)
print(sum(dist)-max_dist)
k,n=map(int,input().split())
a=list(map(int,input().split()))
x=max(a)-min(a)
y=min(a)+(k-max(a))
if(x<=y):
  print(x)
else:
  print(y)
k, n = map(int, input().split())
clist = list(map(int, input().split()))

max1 = 0
ind = 0
for i in range(n-1):
    tmp = clist[i+1] - clist[i]
    if tmp > max1:
        max1 = tmp
        ind = i

print(clist[i]+(k-clist[i+1]))
k,n=input().split()
k=int(k)
n=int(n)
l=list(map(int, input().split()))
ans=[l[0]]
num=l[0]
for i in range(1,len(l)):
    ans.append(min(abs(l[i]-k),l[i]))
ans.sort()
if ans[0]==0:
    print(sum(ans))
else:
    print(sum(ans)-ans[-1])
p,q= input().split()
a,b =(int(p), int(q))
f = map(int,input().split())
f_list = list(f)
out_dis =f_list[0] + a -f_list[b-1]
dis_list=[out_dis]
for i in range(b-1):
    dis = f_list[i+1]-f_list[i]
    dis_list.append(dis)

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

res= a[n-1]

max=0
n_prev=0

for nn in a:
    if (nn-n_prev) < (k-a[n-1]):
        res=(nn-n_prev)
    n_prev=nn

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

max_l = k-a[-1]+a[0]


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



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

s1 = clist[n-1]-clist[0]
s2 = (k - clist[n-1]) + clist[n-2]

print(min(s1,s2))
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

ad_max = max(ad)
ad_max_ind = ad.index(ad_max)

print(ad_max_ind)

if ad_max_ind == (n-1):
    ans = a[n-1] - a[0]
if ad_max_ind != (n-1):
    ans = k - a[ad_max_ind+1] + a[ad_max_ind]
    
print(ans)
k, n = map(int, input().split())
an = list(map(int, input().split()))
bn = [an[i+1] - an[i] for i in range(n-1)]
bn.append(an[0]+20-an[-1])
print(20-max(bn))
K,N = map(int,input().split())
A = list(map(int,input().split()))+[K]
D = [abs(A[n+1]-A[n]) for n in range(N)]
print(K-max(D))
K,N=map(int,input().split())
A=list(map(int,input().split()))
a=[]
for i in range(N):
    if i==0:
        a.append(abs(A[i]-A[N-1]))
    else:
        a.append(abs(A[i]-A[i-1]))

print(K-max(a))
K, N = map(int, input().split())
A = list(map(int, input().split()))
num = (N-2)-N
l = []
for i in range(1,len(A)):
    if A[num] == 0:
        l.append(0)
    else:
        ans = A[-i] - A[num]
        num -= 1
        l.append(ans)
res = K - sum(l)
print(res)
k, n = map(int, input().split())
*a, = map(int, input().split())
diffs = []
for i in range(n):
    if i == n - 1:
        diff = k - a[i]
    else:
        diff = a[i + 1] - a[i]
    diffs.append(diff)
diffs.sort()
print(sum(diffs[:n - 1]))
k,n=map(int,input().split())
lista=list(map(int,input().split())) 
ans=[]
for i in range(n-1):
  ans.append(lista[i+1]-lista[i])
print(k-max(ans))
K,N=map(int,input().split())
A=list(map(int,input().split()))
ans=0
ans+=min(A[0]+abs(A[-1]-K),abs(A[0]-A[1]))
for i in range(1,N-1):
    ans+=min(abs(A[i-1]-A[i]),abs(A[i]-A[i+1]))
print(ans)
k,n=map(int,input('').split())
a=input('')
a=a.split(' ')
for b in range(n):
    a[b]=int(a[b])

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

k=k-m
print(k)
K, N = map(int, input().split())

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

A.sort()

minHouse = min(A)
maxHouse = max(A)

if minHouse == 0:
    for i in range(N):
        if A[i] != 0:
            minHouse = A[i]
            break

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

print(ans)
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
harf=k/2
harf1=[]
harf2=[]

for i in a:
  if i<=harf:
    harf1.append(i)
  else:
    harf2.append(i)

ans1=max(a)-min(a)
ans2=max(harf1)+(k-min(harf2))
  
if ans1<=ans2:
  print(ans1)
else:
  print(ans2)
K, N = map(int, input().split())
A = list(map(int, input().split()))
# A = [i for i in map(int, input().split())]


AN = A[0]+(K-sum(A))
A2 = A[1:N]
A2.append(AN)

print(sum(A2)-max(A2))
a,b=map(int,input().split())
l=list(map(int,input().split()))
m=[]
for i in range(b):
  if i<=b-2:
    m.append(abs(l[i+1]-l[i]))
  else:
    m.append(abs(l[b-1]-l[0]))
m.sort(reverse=True)
print(a-m[0])
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
m = 0
for i in range(1, len(A)):
    if A[i]-A[i-1]>m:
        m=A[i]-A[i-1]
if K-A[-1]+A[1]>m:
    print(A[-1]-A[1])
else:
    print(K-m)
def main():
    k, _ = map(int, input().split())
    a = list(map(int, input().split()))
    a.append(k)
    before = a[0]
    new = []
    for i in a[1:]:
        new.append(i - before)
        before = i
    print(new)
    print(sum(new) - max(new))


if __name__ == "__main__":
    main()
import numpy as np

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

K = a[0]
N = a[1]

c = np.zeros(N)

for i in range(N-1):
  c[i] = abs(b[i+1] - b[i])
  
c[N-1] = abs(K - b[N-1] + b[1])

r = max(c)

print(int(K-r))