peterverebics/CodeNet_Python_118 huggingface dataset:
n,m=map(int,input().split())
lis=list(map(int,input().split()))
li=[lis[i+1]-lis[i] for i in range(m-1)]
li.append(n-lis[-1]+lis[0])
print(sum(li)-max(li))
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
A.append(K+A[0])
diff_list = []
for i in range(len(A) - 1):
    diff_list.append(A[i+1]-A[i])
print(K - max(diff_list))
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=a+[k+a[0]]
b=[a[i+1]-a[i] for i in range(n)]
m=max(b)
print(k-m)
K, N = map(int, input().split())
A = list(map(int, input().split()))
distance = [A[i+1] - A[i] for i in range(N-1)]+[A[0]-(A[-1]-K)]
result = K - max(distance)
print(result)
import sys
input = sys.stdin.readline

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[0]-A[-1])

print(K-max(d))
k, n = map(int, input().split())
array = list(input().split())
array2 = []
for i in range(len(array)-1):
    array2.append(int(array[i+1]) - int(array[i]))
first = int(array[0]) + k - int(array[len(array)-1])
array2.append(first)
print(k - max(array2))
K, N = map(int, input().split())
A = list(map(int, input().split()))
D = list()
D.append(A[0] + K - A[-1])
for i in range(N - 1):
    D.append(A[i + 1]- A[i])

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

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

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

print(k - mx)
k,n=map(int,input().split())
a=list(map(int,input().split()))
dis=[]

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

dis.append(a[0]+k-a[n-1])
  
print(str(k-max(dis)))
k, n = map(int, input().split())
L = list(map(int, input().split()))
b = k - L[n-1] + L[0]
for i in range(n-1):
    R = L[i+1] - L[i]
    if R > b:
      b = R
print(k - b)
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

A = A + [K]

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

#一番長い距離を引くか, 端から端までか
print(min(sum(lis[:N-1]), K - max(lis)))
k,n=map(int,input().split())
a=list(map(int,input().split()))

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

min = a[-1] - a[0]

for i in range(1, n-1):
    dist = k + a[0+i] - a[1+i]
    if min > dist:
        min = dist

print(min)
k, n = (int(each) for each in input().split(' '))

a = [int(each) for each in input().split(' ')]
distance = []
for i in range(len(a)-1):
    distance.append(a[i+1]-a[i])

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

print(k - max(distance))
# -*- coding: utf-8 -*-
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
D = []
D.append(A[N-1]-A[0])
for i in range(1, N):
  D.append(A[i-1]+K-A[i])

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

a = K + A[0]
A = A + [a]

#print(A)

B = []
for i in range(N):
    B.append(A[i+1] - A[i])
B = sorted(B,reverse = False)

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

for i in range(N-1):
  now = i
  pre = now-1
  post = now +1
  #nowとpreの距離を求める
  if now == 0:
    preDic = (K-A[N-1])+A[now]
  else:
    preDic = A[now]-A[pre]
  postDic = A[post]-A[now]  
  if preDic >= postDic:#時計回りに、i-1の位置まで進む
    ans.append(K-preDic)
  else:
    ans.append(K-postDic)
  
print(min(ans))
k,n = (int(x) for x in input().split())
list = [int(i) for i in input().split()]
list.append(k+list[0])
long = 0
for i in range(n):
    long = max(long,list[i+1]-list[i])

print(k-long)
k,n,*a_l = map(int, open(0).read().split())
a_before = 0
del_a = k-a_l[-1] + a_l[0]
for a in a_l:
    del_a = max(del_a, a - a_before)
    a_before = a
print(k - del_a)
K,N = list(map(int, input().split()))
A_list = list(map(int, input().split()))

sub_A_list = A_list.copy()[1:]
sub_A_list.append(K + A_list[0])

for i in range(N):
    sub_A_list[i] -= A_list[i]

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

lis = []

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

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

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

print(k-tmp)
kk,nn = map(int, input().split(' '))
ll = list(map(int, input().split(' ')))
ll.append(kk)

aida = []
for j in range(len(ll)-1):
    #print(j)
    if j==0: aida.append(ll[-1]-ll[-2]+ll[0])
    else: aida.append(ll[j]-ll[j-1])
print(sum(aida)-max(aida))
k, n = map(int, input().split())
A = list(map(int, input().split()))


start_diff = A[0] + k - A[-1]
diff = [start_diff]
answer = 0
start = A[0]

for i in range(1, n):
    diff.append(A[i] - start)
    start = A[i]

# print(diff)
# print(start_diff)

answer = sum(diff) - max(diff)
print(answer)
K,N=map(int,input().split())
A=list(map(int,input().split()))
a=[]
for i in range(len(A)-1):
  a.append(A[i+1]-A[i])
a.append(K-A[-1]+A[0])
print(sum(a)-max(a))
k,n,*a=map(int,open(0).read().split())
a+=[k+a[0]]
print(k-max(y-x for x,y in zip(a,a[1:])))
K, N = map(int, input().split())
A = [int(i) for i in input().split()]
dist = [A[i + 1] - A[i] for i in range(N - 1)]
dist.append(A[0] + K - A[N - 1])
print(K - max(dist))
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [abs(x - y) for x, y in zip(A[1:], A)]
B += [K - A[-1] + A[0]]

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

for i in range(len(Ai)):
    if i == len(Ai) - 1:
        Di[i] = (K - Ai[i]) + Ai[0]
    else:   
        Di[i] = Ai[i + 1] - Ai[i]

ans = K - max(Di)

print(ans)
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
d = 0
for i in range(N):
    tmp = abs(A[i] - A[i-1])
    if tmp > K / 2:
        tmp = K - tmp
    if tmp > d:
        d = tmp
print(K - d)
k,n,*a=map(int,open(0).read().split())
a+=[a[0]+k]
d=[abs(a[i]-a[i+1]) for i in range(n)]
print(k-max(d))
import sys
a=sys.stdin.buffer.read
K,N,*A=map(int,a().split())
A+=[A[0]+K]
print(K-max(y-x for x,y in zip(A,A[1:])))
K,N = map(int,input().split())
A = list(map(int,input().split()))

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

print(K-dist_max)
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    k, n = map(int, readline().rstrip().split())
    A = sorted(list(map(int, readline().rstrip().split())))
    maxA = k + A[0] - A[n-1]
    for i in range(n-1):
        a = abs(A[i+1] - A[i])
        maxA = max(maxA, a)
    print(k - maxA)


if __name__ == '__main__':
    main()
k, n = map(int, input().split())
a = [int(i) for i in input().split()]
ans = a[-1] - a[0]
for i in range(1,n):
    ans = min(ans, k - a[i] + a[i-1])
print(ans)
def main(k,n,a):
    lst = []
    lst.append(a[0]+(k-a[-1]))
    for i in range(n-1):
        lst.append(a[i+1]-a[i])
    return k-max(lst)
    
k,n = map(int,input().split())
a = list(map(int,input().split()))
print(main(k,n,a))
k,n = map(int,input().split())
a_list = list(map(int,input().split()))
re_a = []
for x in a_list:
    re_a.append(x-k)
a_list_add_re = re_a+a_list
result = []
x = n - 1
for i in range(len(a_list_add_re)-x):
    result.append(a_list_add_re[i+x] - a_list_add_re[i])
    
print(min(result))
k, n = map(int, input().split())

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

ans = 0

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

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

print(k - ans)
k,n = map(int,raw_input().split())
a = map(int,raw_input().split())
b = []
for i in range(1,len(a)):
	b.append(a[i] - a[i-1])
b.append(min(a[-1] - a[0], k - a[-1] + a[0]))
print(k - max(b))
import numpy as np

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

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

print(K - maxdist)
k, n, *A = map(int, open(0).read().split())
B = [a1-a0 for a0, a1 in zip(A, A[1:])] + [k-A[-1]+A[0]]
print(sum(B) - max(B))
K,N = map(int,input().split())
A=list(map(int,input().split()))
gap=A[0]-A[N-1]+K

for i in range(N-1):
    gap=max(gap,A[i+1]-A[i])
print(K-gap)
#!/usr/bin/env python3
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] = 0
a.append(k-sum(a))
ans = k-max(a)
print(ans)
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[b[0]+(a[0]-b[a[1]-1])]

for i in range(a[1]-1):
  c.append(b[i+1]-b[i])
print(sum(c)-max(c))
k, n = map(int, input().split())
A = input().split()
a = [int(i) for i in A]

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

for i in range(n):
    if kyori < a[i] - a[i-1]:
        kyori = a[i] - a[i-1]
print(k-kyori)
#!/usr/bin/env python3
K, N = [int(str) for str in input().strip().split()]
A = [int(str) for str in input().strip().split()]

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

print(K - max(diffs))
if __name__ == "__main__":
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    # print(A)
    A_dif = []
    for i in range(len(A)-1):
        # print(A[i + 1])
        # print(A[i])
        A_dif.append(A[i + 1] - A[i])
    A_dif.append((abs(K - A[-1]) + A[0]))
    # print(A_dif)
    A_dif.sort(reverse=True)
    ret = sum(A_dif[1:])
    print(ret)
K,N=map(int, input().split())
As=list(map(int, input().split()))
ans=K
m=0
for i in range(len(As)-1):
    if As[i+1]-As[i] > m:
        m=As[i+1]-As[i]
if As[0]+K-As[-1]>m:
    m=As[0]+K-As[-1]
print(ans-m)
a,b = map(int,input().split())
aaa = [int(i) for i in input().split()]

aaa.append(aaa[0] + a)

result = [c1 - c0 for c0,c1 in zip(aaa,aaa[1:])]

print(a - max(result))
K, N = map(int, input().split())
A = list(map(int, input().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()))
ans = []
for i in range(len(a)-1):
    ans.append(a[i+1]-a[i])
ans.append(k-a[-1]+a[0])
print(k-max(ans))
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.append(A[0]+k)
B = [0 for i in range(n)]
for j in range(n):
    B[j] = A[j+1] - A[j]
#print(A)
#print(B)
B.sort()
B.pop()
#print(B)
ans = sum(B)
print(ans)
def readinput():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    return k,n,a

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

if __name__=='__main__':
    k,n,a=readinput()
    ans=main(k,n,a)
    print(ans)
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))

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


main()
def resolve():
    K, N = map(int, input().split())
    A = tuple(map(int, input().split()))
    diff_max = 0
    A_sum = 0

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

        diff_max = max(diff_max, diff)
        A_sum += diff

    print(A_sum - diff_max)
    
if __name__ == '__main__':
    resolve()
import math


def resolve():
    import sys
    input = sys.stdin.readline
    # row = [int(x) for x in input().rstrip().split(" ")]
    kn = [int(x) for x in input().rstrip().split(" ")]
    k = kn[0]
    a = [int(x) for x in input().rstrip().split(" ")]

    max_dist = max([a[i+1] - a[i] for i in range(len(a)-1)])
    max_dist = max_dist if max_dist > a[0] + k - a[len(a)-1] else a[0] + k - a[len(a)-1]

    print(k - max_dist)


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

D = [None] * N
last = A[-1] - K
for i, a in enumerate(A):
  diff = (a - last, i)
  D[i] = diff
  last = a

i = max(D)[1]
if i == 0:
  print(A[i - 1] - A[i])
else:
  print(K - A[i] + A[i - 1])
k, n = [int(elem) for elem in input().split()]
a_lst = [int(elem) for elem in input().split()]

diff = []
for i, a in enumerate(a_lst[:-1]):
    diff.append(a_lst[i+1] - a)
diff.append((a_lst[0] + k) - a_lst[-1])

res = sum(diff) - max(diff)
print(res)
K,N= map(int,input().split())
A= list(map(int,input().split()))
A = A + [A[0]+K]
diff = [b-a for a,b in zip(A,A[1:])]
mA = max(diff)
print(K-mA)
perimeter, n = map(int, input().split())
houses = list(map(int, input().split()))

e = []
max = 0

for i in range(n-1) :
  e.append(houses[i + 1] - houses[i])
e.append(perimeter - houses[n-1] + houses[0])

for i in range(n):
  if e[i] > max:
    max = e[i]

print(perimeter - max)
K, N = map(int,input().split())
lists = list(map(int,input().split()))
maxdist = 0
for i in range(N-1):
    dist = lists[i+1] - lists[i]
    if dist > maxdist:
        maxdist = dist
adis = K-lists[N-1]+lists[0]
if adis > maxdist:
    maxdist = adis

print(K-maxdist)
K, N = map(int, input().split())
A = list(map(int, input().split()))
li = []
for i in range(N):
    if i == 1:
        cadA = A[N-1] - A[0]
    else:
        cadA = K - A[i] + A[i-1]
    if i == N-1:
        cadB = A[N-1] - A[0]
    else:
        cadB = A[i] + (K - A[i+1])
    li.append(min(cadA, cadB))
print(min(li))
k,n=map(int,input().split())
a=list(map(int,input().split()))
ans=0
for i in range(n-1):
  ans=max(a[i+1]-a[i],ans)
ans=max(ans,k-a[-1]+a[0])
print(k-ans)
K, N = map(int, input().split())
house_list = list(map(int, input().split()))

saidai_kyori = 0
sou_kyori = 0
for i, position in enumerate(house_list):
    kyori = 0
    if i == 0:
        kyori = K + house_list[0] - house_list[-1]
    else:
        kyori = house_list[i] - house_list[i-1]

    if kyori > saidai_kyori:
        saidai_kyori = kyori
    
    sou_kyori += kyori

print(sou_kyori - saidai_kyori)
#!/usr/bin/env python3
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
a.append(k+a[0])
a.reverse()
for i in range(n):
    a[i]-=a[i+1]
ans = k-max(a)
print(ans)
k,n = map(int, input().split())
al = list(map(int, input().split()))

res = al[-1] - al[0]
for i in range(1,n):
    r = k - al[i] + al[i-1]
    res = min(res, r)

print(res)
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 = max(d,A[i+1]-A[i])

print(K-d)
K, N = map(int, input().split())
A_i = list(map(int, input().split()))


# 20 3 20メートルで3件の家
# 0 5 15

# [5, 10] + [0 + (20 -15)]
# [5, 10, 5]
# max([5, 10, 5]) => 10
# 20 - 10

D_i = []
D_i.append(A_i[0] + K - A_i[-1])


for i in range(N-1):
    D_i.append(A_i[i+1] - A_i[i])

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

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

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

if(m>max):
    max=m

kyori=K-max

print(kyori)
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[-1]+a[0])

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

ans = sum(dis) - max(dis)
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))
mx = 0
for i in range(N - 1):
    mx = max([mx, A[i+1] - A[i]])
mx = max([mx, A[0] + K - A[N-1]])
print(K-mx)
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
maxi=0
for i in range(n-1):
    tmp=a[i+1]-a[i]
    maxi=max(tmp,maxi)

if maxi<=a[0]+(k-a[-1]):
    print(max(a)-min(a))
else:
    print(k-maxi)
k,n,*l=map(int,open(0).read().split());print(min((l[i-1]-l[i])%k for i in range(n)))
k, n = list(map(int, input().split()))

a = list(int(i) for i in input().split())
s = []
for i in range(n-1):
   s.append(a[i+1] - a[i])

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

print(k - max(s))
f=lambda:map(int,input().split())
k,n=f()
l=list(f())
m=k+l[0]-l[-1]
for i in range(n-1):
  m=max(m,l[i+1]-l[i])
print(k-m)
KN = list(map(int, input().split()))
A = list(map(int, input().split()))

answer = []

for i,home in enumerate(A):
    if home < A[i - 1]:
        answer.append(home - A[i - 1] + KN[0])
    else:
        answer.append(home - A[i - 1])

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

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

print(sum(l)-max(l))
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=[]
A.append(A[0]+K)
for i in range(N):
    l.append(A[i+1]-A[i])
print(K-max(l))
k, n = map(int, input().split())
l_1 = list(map(int, input().split()))
l_1.append(l_1[0] + k)
l_2 = [l_1[i+1] - l_1[i] for i in range(n)]
print((k) - max(l_2))
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.append(K+A[0])
d=0
for i in range(N):
    d=max(d,A[i+1]-A[i])
print(K-d)
M,N = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(M+A[0])
dis = 0
for i in range(N):
    if A[i+1]-A[i] >= dis:
        dis = A[i+1]-A[i]
print(M-dis)
import math

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

path = 0
lastPosition = 0

for i in range(KN[1]):
    if (i == 0):
        path = KN[0] + A[0] - A[KN[1] - 1]
        lastPosition = A[0]
    else:
        currentPosition = A[i]
        if (path < currentPosition - lastPosition):
            path = currentPosition - lastPosition
        lastPosition = currentPosition

print(KN[0] - path)
k, n = map(int, input().split())
a = list(map(int, input().split()))

diff = []

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

print(k - max(diff))
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()))
D = []
for i in range(n-1):
    D.append(A[i+1]-A[i])
else:
    D.append(k-(A[n-1]-A[0]))
D.sort(reverse=True)
print(k-D[0])
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

k,n = map(int,input().split())
A = list(map(int,input().split()))
mans = k + A[0] - A[-1]
for i in range(n-1):
  mans = max(mans,A[i+1]-A[i])
print(k-mans)
K,N=map(int,input().split())
A=list(map(int,input().split()))
memo=A[0]
for i in range(N-1):
	Bi=A[i+1]-A[i]
	if Bi>memo:
		memo=Bi
if memo<=K-A[N-1]+A[0]:
	print(A[N-1]-A[0])
else:
	print(K-memo)
from collections import deque

k, n = [int(i) for i in input().split()]
data = [int(i) for i in input().split()]

d = deque(data)
d.appendleft(0)
d.pop()

bet = [i-j for i, j in zip(data, d)]

bet[0] += k - data[-1]

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

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

d = []
for i in range(n-1):
    d.append(a[i+1] - a[i])
d.append(k-a[n-1]+a[0])
print(sum(d) - max(d))
k,n = map(int,input().split())
a = list(map(int,input().split()))
res = k+a[0]-a[n-1]
for i in range(1,n):
    res = max(res,a[i]-a[i-1])
print(k-res)
K, N = list(map(int,input().split()))
A = list(map(int,input().split()))
d = [A[i+1]-A[i] for i in range(N-1)]
de = K - A[N-1] + A[0]
d.append(de)
D = sorted(d)
dis = 0
for i in range(N-1):
  dis += D[i]
print(dis)
#create date: 2020-03-28 21:04

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    k, n = na()
    a = na()
    lmax = a[0] + (k-a[-1])
    for ai, aj in zip(a[0:-1], a[1:]):
        lmax = max(lmax, (aj-ai))
    print(k-lmax)

if __name__ == "__main__":
    main()
k,n=map(int,input().split())
a=list(map(int,input().split()))
d=[]

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

ans=sum(d)-max(d)
print(ans)
k,n=list(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(k-A[-1]+A[0])
print(sum(l)-max(l))
k, n = map(int, input().split())
a = [int(x) for x in input().split()]

diff = [a[i + 1] - a[i] for i in range(n - 1)] + [k - a[-1] + a[0]]
ans = sum(diff) - max(diff)
print(ans)
#def main(S, b):
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)

    #print(A)
    ans = 10000000000
    for i in range(n):
        #print(ans, A[i + n - 1], A[i])
        if (ans > A[i + n - 1] - A[i]):
            ans = A[i + n - 1] - A[i]

    print(ans)
from sys import stdin
#n = int(stdin.readline().rstrip())
#l = list(map(int, stdin.readline().rstrip().split()))
k,n = map(int, stdin.readline().rstrip().split())
#S = [list(map(int, stdin.readline().rstrip().split())) for _ in range(h)]#hの定義を忘れずに
As = list(map(int, stdin.readline().rstrip().split()))

kyori = []

for i in range(1, n):
    kyori.append(As[i] - As[i-1])
kyori.append(As[0]+k-As[-1])
kyori.append(As[-1]-As[-2])

print(k - max(kyori))
#12
K,N = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
dist = A[0]
Sum = 0
log1 = 0
log2 = 0

for i in range(N):
    if i+1 >= N:
        log1 = (K-A[i]) + A[0]
    else:
        log1 = A[i+1] - A[i]
    
    if log2 < log1 :
        log2 = log1
    
    Sum +=  log1
    
print(Sum - log2)
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(k-m)
K,N=map(int,input().split())
A=list(map(int,input().split()))
A=sorted(A)
A.append(A[0]+K)
i=0
b=0
for i in range(N):
    if b<A[i+1]-A[i]:
        b=A[i+1]-A[i]
print(K-b)
import copy

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

now = 0
ans = 2000000000000000000000000

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

if a[n-1]-a[0] < ans:
  ans = a[n-1]-a[0]
        
print(ans)
k,n=[int(j) for j in input().split()]
a=[int(j) for j in input().split()]
l=[]
for i,j in zip(a,a[1:]):
    l.append(j-i)
l.append(k-a[-1]+a[0])
print(k-max(l))
k,n = map(int, input().split())
A = list(map(int, input().split()))

longest = k - (A[-1] - A[0])

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

if a[n-1]-a[0] <= k-a[n-1]+a[0]:
    print(a[n-1]-a[0])
else:
    for i in range(n):
        ans=max(ans,a[i]-a[i-1])
    ans=max(ans,k-a[n-1]+a[0])
    print(k-ans)
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(n-max)
k, n = map(int, input().split())
A = list(map(int, input().split()))
r = k

for i in range(n - 1):
    if i == 0:
        j = n - 1
        r = min(r, A[j] - A[i])
    else:
        j = i + 1
        r = min(r, A[i] + k - A[j])
print(r)