peterverebics/CodeNet_Python_118 huggingface dataset:
N = [int(_) for _ in input().split()]
K, N = N[0], N[1]
A = [int(_) for _ in input().split()]

def solve(K, N, A):
    
    hoge = A[0]
    B = []
    for i in range(1, N):
        B.append(A[i] - hoge)
        hoge = A[i]
    
    B.append( K - A[N-1] + A[0])
    return sum(B) - max(B)

print(solve(K, N, A))
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(k-dis[n-1])
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(a[0] - a[n-1] + k)
print(k - max(d))
K, N = map(int, input().split())
A = [int(i) for i in input().split()]

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

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

lens = []
max_len = 0
for i in range(len(A)):
  if i == len(A) - 1:
    dist = K - A[i] + A[0]
  else:
    dist = A[i+1] - A[i]
  lens.append(dist)
print(sum(lens) - max(lens))
k,n = map(int,input().split())
a = list(map(int,input().split()))
max = 0
for i in range(n-1):
    if max < a[i+1]-a[i]:
        max = a[i+1]-a[i]
if max < k-a[n-1]+a[0]:
    max = k-a[n-1]+a[0]
print(k-max)
k,n = map(int, input().split())
a = list(map(int, input().split()))

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

if(max < (a[0] + (k-a[n-1]))):
    max = (a[0] + (k-a[n-1]))

print(k-max)
N,K = map(int,input().split())
A = list(map(int,input().split()))
max = 0
for i in range(K-1):
  value = A[i+1] - A[i]
  if max < value:
    max = value
value = (N - A[K-1]) + A[0]
if max < value:
  max = value
print(N - max)
K,N= map(int,input().split())
a_list = list(map(int,input().split()))
diff = []
for i in range(N-1):
    diff.append(a_list[i+1] - a_list[i])
diff.append(a_list[0] + (K-a_list[N-1]))
# print(max(diff))
print(K-max(diff))
k,n = map(int, input().split())
a = list(map(int, input().split()))

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

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

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

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

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

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

distance.sort()
longest = distance.pop()

print(K - longest)
KN = input().split(' ')
K = int(KN[0])
N = int(KN[1])
M = K/2
Al = input().split(' ')
A = []

for home in Al:
  A.append(int(home))

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

print(K-m)
K, N = [int(x) for x in input().split()]
list = [0]*N
list = [int(x) for x in input().split()]
list.append(K+list[0])

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

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

a = sorted(a)

d = -1

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

if a[0]+k - a[n-1] >= d:
    d = a[0]+k - a[n-1]

ans = k - d
print(ans)
kn=list(map(int, input().split()))
a=list(map(int, input().split()))
k=kn[0]
n=kn[1]
maxdis=k-a[n-1]+a[0]
#print(maxdis)
for i in range(n-1):
    if maxdis<(a[i+1]-a[i]):
        maxdis=(a[i+1]-a[i])
        #print(i)
        #print(maxdis)
print(k-maxdis)
K, N = map(int, input().split())
A = [int(i) for i in input().split()]
A.append(K+A[0])
B = [A[i+1]-A[i] for i in range(N)]
print(K - max(B))
K, N = map(int, input().split())
As = list(map(int, input().split()))
ds = []
for i in range(N):
  if i == 0:
    d = K+As[i]-As[i-1]
  else:
    d = As[i]-As[i-1]
  ds.append(d)
#print(ds)
r = K-max(ds)
print(r)
K,N=map(int,input().split())

house=list(map(int,input().split()))
longest=K-(house[N-1]-house[0])

for i in range(N-1):
    if house[i+1]-house[i]>longest:
        longest=house[i+1]-house[i]


print(K-longest)
def main():
  k, n = map(int, input().split())
  dis = tuple(map(int, input().split()))

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

  max_dis = 0
  max_idx = 0
  for i, d in enumerate(diff):
    if d > max_dis:
      max_dis = d
      max_idx = i

  diff[max_idx] = 0

  print(sum(diff))

main()
k,n = map(int,input().split())
a = list(map(int,input().split()))
a.insert(0,a[n-1]-k)
l = 0
for z in range(n):
    b = a[z+1] - a[z] 
    if b > l:
        l = b
print(k-l)
K,N = map(int,input().split())
AA = list(map(int,input().split()))

dlst = []

n = AA[-1] - K
for a in AA:
    dlst.append(a - n)
    n = a
    
print(K-max(dlst))
# print('input >>')
K, N = map(int,(input().split()))
As = list(map(int,(input().split())))

As = [A - As[0] for A in As]
As_diffs = [As[i+1] - As[i] for i in range(len(As)-1)]
As_diffs += [K - As[-1]]
As_fiff_max = max(As_diffs)

# print('-----output-----')
print(K - As_fiff_max)
K, N = map(int, input().split())
As = [int(n) for n in input().split()]

maxd = 0

b = 0
for a in As:
    maxd = max(maxd, a - b)
    b = a
maxd = max(maxd, K - As[-1] + As[0])

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

print(K-max_dis)
k, n = map(int, input().split())
a_list = list(map(int, input().split()))

interval = []
ans = 1e+9
for i in range(len(a_list)):
    if i == 0:
        distance = a_list[-1] - a_list[0]
    else:
        interval = a_list[i] - a_list[i-1]
        distance = k - interval
    ans = min(ans, distance)
print(ans)
K,N = map(int, input().split())
A =  [int(N) for N in input().split()]

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

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

B.sort()

Big = B[N-1]

print(K-Big)
k, n = map(int, input().split())
a = list(map(int, input().split()))
l = [k - a[-1] + a[0] if x == n - 1 else a[x+1] - a[x] for x in range(n)]

print(sum(l) - max(l))
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0] * (2 * N)

    for i in range(2*N):
        if i < N:
            B[i] = A[i]
        else:
            B[i] = K + A[i - N]

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

    ans = K - l
    print(ans)



if __name__ == "__main__":
    main()
K,N=map(int,input().split())
*A,=sorted(map(int,input().split()))
print(K-max([*[A[i+1]-A[i] for i in range(N-1)], A[0]+(K-A[-1])]))
k, n = map(int, input().split())
A = list(map(int, input().split()))

D = [0] * n
for i in range(len(A)-1):
    D[i] += A[i+1]-A[i]
D[n-1] += A[0] + k - A[n-1]

D.remove(max(D))
print(sum(D))
# abc160 c
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
a = [i - a[0] for i in a]
d = []
for i in range(len(a)):
    if i == len(a)-1:
        d.append(k - a[i])
    else:
        d.append(a[i+1] - a[i])
print(k - max(d))
k,n=map(int,input().split())
a=list(map(int,input().split()))
p=a[0]+k-a[-1]
for i in range(n-1):
    s=a[i+1]-a[i]
    p=max([p,s])
print(k-p)
k,n = map(int,input().split())
a = input().split()
s = 0
t = 0
while t < n:
  x = int(a[t-1])
  y = int(a[t])
  if y-x > s:
    s = y-x
  t += 1
l = int(a[0])+k - int(a[-1])

print(k-max(s,l))
k,n,*a=map(int,open(0).read().split())
dist=10**7
for i in range(n):
    if i==0:
#        print(a[-1]-a[0])
        dist=min(dist,a[-1]-a[0])
    else:
#        print(a[-1+i]+k-a[i])
        dist=min(dist,a[-1+i]+k-a[i])
print(dist)
# -*- coding: utf-8 -*-

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

A0 = A[1:] + [A[0] + K]
dis = [a0 - a for (a, a0) in zip(A, A0)]
ans = K - max(dis)

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

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

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

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

print(K - max_dist)
kn=input().split()
k=int(kn[0])
n=int(kn[1])
an=input().split()
num_list=[]
for a in an :
  num_list.append(int(a))
count_list=[]

for i in range(n) :
  value=num_list[i-1]-num_list[i]
  if value<0 :
    value+=k
  
  count_list.append(value)
  
print(min(count_list))
inputted = list(map(int, input().split()))
K = inputted[0]
N = inputted[1]
A = list(map(int, input().split()))

distancies = []
for i, Ai in enumerate(A):
    current = i
    to = (i + 1) % len(A)
    distance = A[to] - A[current] if i < len(A) - 1 else K - A[current] + A[0]
    distancies.append(distance)

answer = K - max(distancies)

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

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

B = []

for i in range(b):
    begin = A[i]
    # end = A[-1+i]
    if i == 0:
        end = A[-1+i]
    else:
        end = A[-1+i] + a
    B.append(end - begin)
print(min(B))
K, N = map(int, input().split())
A = list(map(int, input().split()))
maxd = 0
for i in range(N):
  if(i != (N-1)):
    if(A[i+1] - A[i] > maxd):
      maxd = A[i+1] - A[i]
  else:
    if(A[0] + K - A[i] > maxd):
      maxd = A[0] + K - A[i]
      
print(K - maxd)
K, N = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
B = A.copy()
for i in range(len(A)-1):
    B.append(A[i]+K)

min_ = float('inf')
for i in range(len(A)):
    if B[i+N-1]-B[i] < min_:
        min_ = B[i+N-1]-B[i]

print(min_)
k,n=map(int,input().split())
a=list(map(int,input().split()))
j=[int(k+a[0]-a[-1])]
for i in range(n-1):
    j.append(a[i+1]-a[i])
print(k-max(j))
def main():
    K, N = map(int, input().split())
    A = list(map(int,input().split()))
    ans = K
    for i in range(0,N,1):
        s = A[i]
        if i == 0:
            g = A[N-1]
            ans = min(ans, g-s)
        else:
            g = A[i-1]
            ans = min(ans, K-(s-g))

    print(ans)

main()
import numpy as np

K, N = list(map(int, input().split()))
a = np.array(list(map(int, input().split())))

print(K - max(np.diff(np.append(a, a[0] + K))))
import numpy as np

K, N = map(int, input().split())
an = [int(x) for x in input().split()]
arr = np.array(an)

dist = arr[1:] - arr[:-1]
dist = list(dist)
dist.append(K - an[N-1] + an[0])

print(K - max(dist))
import numpy as np

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

_a = np.append(a,a[0]+k)
_a = np.delete(_a,0)

dis = _a -a
ans = k -np.max(dis)

print(ans)
k,n = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
temp = 0
ls = []
for i in range(n-1):
    temp = a[i+1]-a[i]
    ls.append(temp)
    ans += temp
ls.append((k-a[-1])+a[0])
ans += (k-a[-1])+a[0]
lsm = max(ls)
lsi = ls.index(lsm)
if lsi+1 <= k:
    ans = ans - lsm
else:
    ans = ans - lsm + a[ls.index(lsm)+1]-a[ls.index(lsm)]
print(ans)
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = []

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

b = sorted(b)
print(sum(b[:-1]))
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])
d.append(a[0] + k - a[-1])
print(k - max(d))
K, N = list(map(lambda x: int(x) ,input().split(" ")))
l = list(map(lambda x:int(x), input().split(" ")))
sa = []
for i in range(len(l)-1):
  sa.append(l[i+1]-l[i])
sa.append(K+l[0]-l[N-1])

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

ans = a[n-1]-a[0]

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

    if ans > x:
        ans = x
print(ans)
k,n = [int(c) for c in input().split()]
a = [int(c) for c in input().split()]
ds = [0] * n # iとi+1の距離
for i in range(n-1):
    ds[i] = a[i+1] - a[i]
ds[n-1] = (k - a[n-1]) + a[0]
m = max(ds)
print(k - m)
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])
print(k-max(dist))
k, n = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n):
    a.append(k+a[i])
right = [0]

for i in range(1, 2*n):
    right.append(right[-1] + a[i] - a[0])

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

for i in range(0,List[1]):
    A.append(A[i]+List[0])

D = []
for i in range(0,List[1]):
    D.append(A[i+List[1]-1]-A[i])

print(min(D))
i = list(map(int, input().split()))
K = i[0]
N = i[1]
max = 0
count = 0


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

for j in range(len(A)-1):
    count += 1
    if abs(A[j+1] - A[j]) > max:
        max = abs(A[j+1] - A[j])
    if count == (len(A)-1):
        if max < abs(A[0] + (K - A[j+1])):
            max = abs(A[0] + (K - A[j+1]))

print(K - max)
K,N = map(int,input().split())
A = list(map(int,input().split()))
B = [0 for i in range(N)]
C = 0
for i in range(N):
  if i == N-1:
    B[i] = A[0] + K - A[i]
  else:
    B[i] = A[i+1] - A[i]
  if B[i] > C:
    C = B[i]
print(K-C)
k,n,*a=map(int,open(0).read().split())
a.sort()
print(k-max([i-j for i,j in zip(a[1:],a)]+[k-a[-1]+a[0]]))
K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
A = sorted(A)
m_list = []
for i in range(N):
  if i == N-1:
    m_list.append(A[0]+K-A[i])
  else:
    m_list.append(A[i+1]-A[i])
print(K-max(m_list))
K,N=map(int,input().split())
l=list(map(int,input().split()))
max_distance=0
for i in range(N-1):
    if l[i+1]-l[i]>max_distance:
        max_distance= l[i+1]-l[i]
if K+l[0]-l[N-1]>max_distance:
    max_distance=K+l[0]-l[N-1]
print(K-max_distance)
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

k, n = inpl()
a = inpl()
a.append(k+a[0])
l = 0
for i in range(n):
    l = max(l, a[i+1] - a[i])
print(k-l)
k,n=map(int,input().split())
l =list(map(int, input().split()))
a = []
a.append(l[n-1]- l[0])
for i in range(n-1):
    a.append(k-l[i+1]+l[i])
print(min(a))
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
max_l = 0
for i in range(N):
    l = A_ls[(i + 1) % N] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % N]
    max_l = max(max_l, l)
print(K - max_l)
n,m=map(int,input().split())
a=[int(j) for j in input().split()]
ma=0
sum=0
for i in range(1,m):
    diff=a[i]-a[i-1]
    ma=max(ma,diff)
    sum+=diff

diff=n-sum
ma=max(ma,diff)
print(n-ma)
##### ABC 160
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])

dist.sort()

ans = 0
for i in range(N-1):
    ans += dist[i]
    
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))
if A[0] == 0:
    A.remove(0)
if len(A) == 1:
    ans = A[0]
else:
    ans = A[-1] - A[0]
print(ans)
K,N = map(int,input().split())	
li = list(map(int,input().split()))	
le = []
for i in range(N):
    if i==N-1:
        le.append(K-li[i])
    else: 
        le.append(abs(li[i]-li[i+1]))
print(sum(le)-max(le))
k, n = map(int, input().split())
a = list(map(int, input().split()))
kankaku = [(a[i] - a[i-1]) % 20 for i in range(len(a))]
print(k-max(kankaku))
def main():

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

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



if __name__ =='__main__':
    main()
import sys
import numpy as np
import itertools

from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

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

a_np = np.array(a)
a_np = a_np.flatten()
a_np = np.hstack((a_np, k))
a_sa = abs(a_np[:-1] - a_np[1:])
print(a_sa.sum() - a_sa.max())
k=int(input().split(" ")[0])
a = list(map(lambda x: int(x), input().split("\n")[0].split(" ")))
df = max(a) - min(a)
print(min(df, k - df))
import statistics
K,N = map(int,input().split())
A = [int(n) for n in input().split()]

a1 = max(A)-min(A)

for i in range(len(A)-1):
    max_v = 0
    index = None
    if (A[i+1]-A[i])>max_v:
        max_v = A[i+1]-A[i]
        index = i+1

a2 = A[index-1]+abs(K-A[index])
print(a1) if a1<a2 else print(a2)
l = input().split(" ")
j = input().split(" ")
k = int(l[0])
n = int(l[1])
x = 0
for i in range(n-1):
  d = int(j[i+1]) - int(j[i])
  x = max(x,d)
dt = k - int(x)
print(dt)
a,b = map(int,input().split())
c = [int(i) for i in input().split()]

result = c[-1] - c[0]

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

dist = 0

for i in range(len(arr)):
    if i is 0:
        if k - arr[i-1] > dist:
            dist = k - arr[i-1]
    
    if arr[i] - arr[i-1] > dist:
        dist = arr[i] - arr[i-1]

print(k - dist)
k, n = (int(i) for i in input().split())
a = list(map(int, input().split()))

i=0
while i < n-1:
  if (a[i+1]-a[i]) >= k/2:
    print(str(k-a[i+1]+a[i]))
    break
  i += 1
else:
  print(str(a[n-1]-a[0]))
K,N=map(int, input().split())
A=list(map(int, input().split()))
dist=[]
for i in range(len(A)):
    if i==0:
        dist.append(K-A[len(A)-1]+A[i])
        dist.append(A[i+1]-A[i])

    elif i<len(A)-1:
        dist.append(K-A[i+1]-A[i])
        dist.append(A[i+1]-A[i])

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

ans1 = a[n - 1] - a[0]

ans2 = 0
flag = 0
for i in range(n):
    if a[i] <= (k / 2):
        continue
    else:
        if flag == 0:
            ans2 += a[i - 1]
            flag = 1
        ans2 += (k - a[i])

print(min(ans1, ans2))
_in = list(map(int, input().split()))
k = _in[0]
n = _in[1]
p = 0
_i = list(map(int, input().split()))
dis = 0
mdis = k - _i[n-1] + _i[0]
for i in range(1,n-2):
    dis = _i[i+1]- _i[i]
    if dis>mdis:
        mdis = dis
print(k-mdis)
K, N = map(int, input().split())
A = list(map(int, input().split()))
d = []

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

print(d)
d.remove(max(d))
print(sum(d))
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.append(a[0])
dis=[]
for i in range(n):
    d=abs(a[i]-a[i+1])
    if d>k/2:
        d=k-d
    dis.append(d)
print(dis,k-max(dis))
k, n = map(int, input().split())
ls = list(map(int, input().split()))

distance_list = []

i = 0
while i < n:
    if i == 0:
        distance_list.append(ls[i])
        i += 1
        continue
    distance_list.append(ls[i] - ls[i-1])
    i += 1

distance_list.append(k - ls[n-1])
distance_list.remove(max(distance_list))
if 0 not in distance_list:
    distance_list.remove(max(distance_list))
print(sum(distance_list))
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(maxA)



if __name__ == '__main__':
    main()
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
max_dis = abs(A[0]-A[-1])%K
pre = A[0]
for a in A[1:]:
    max_dis = max(max_dis, abs(a-pre))
    pre = a
ans = K - max_dis
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))

tmp1 = A[-1] - A[0]
tmp2 = A[-1] - (20 - A[0] - A[-1])

print(min(tmp1, tmp2))
# print(A[-1]-A[0])
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
sa=[]
be=k

for i in a:
  sa.append(be-i)
  be=i
  
print(sum(sa)-max(sa))
k, n = [int(elem) for elem in input().split()]
a_lst = [int(elem) for elem in input().split()]

res = 0
if (a_lst[-1] - a_lst[-2]) <= (k - a_lst[-1]):
    for i, a in enumerate(a_lst[:-1]):
        res += a_lst[i+1] - a
else:
    for i, a in enumerate(a_lst[1:]):
        res += a - a_lst[i-1]
    res += (a_lst[0] + k) - a_lst[-1] 
print(res)
k,n = map(int,input().split())

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

dis = []

for i in range(len(A)):
    if i+1 == len(A):
        break
    elif abs(A[i-1] - A[i]) > abs(A[i+1] - A[i]):
        dis.append(abs(A[i+1] - A[i]))
    elif abs(A[i-1] - A[i]) == abs(A[i+1] - A[i]):
        dis.append(abs(A[i-1] - A[i]))
    else:
        dis.append(abs(A[i-1] - A[i]))

        
print(sum(dis))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
for i in range(a[1]):
    if i==a[1]-1:
        if b[0]==0:
            b[0]=a[0]
            c.append(abs(b[i]-b[0]))
        else:
            c.append(abs(b[i]-a[0]))
    elif i!=a[1]-1:
        c.append(abs(b[i]-b[i+1]))
print(a[0]-max(c))
N , M = (map(int , input().split()))
A = list(map(int , input().split()))
A.append(N)
a_lis = []
for i in range(len(A)-1):
    a_lis.append(A[i+1]-A[i])
print(N - max(a_lis))
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

res = k - m
print(res)
s = input().split(" ")
k = int(s[0])
n = int(s[1])

d = input().split(" ")
dist = []

for i in range(0,n):
    dist.append(int(d[i]))

res = dist[n-1] - dist[0]

for i in range(i, n):
    diff = dist[i] - dist[i-1]
    aux = k - diff
    if aux < res:
        res = aux

print(str(res))
k,n=map(int,input().split())
a=list(map(int,input().split()))
a.append(k)
x=[]
for i in range(len (a)-1):
    x.append(a[i+1]-a[i])
print(sum(x)-max(x))
# coding: utf-8
# Your code here!

k,n = map(int,input().split())
num_list = list(map(int, input().split()))
min_num = num_list[0]+num_list[-1]

for i in range(0,int(len(num_list)/2)):
    res = num_list[-1]-k
    #print(num_list)
    num_list.pop(-1)
    #print(num_list)
    num_list.insert(0, res)
    #print(num_list)
    sum_num = abs(num_list[0])+abs(num_list[-1])
    #print("sum_num")
    #print(sum_num)
    
    if sum_num < min_num:
        min_num = sum_num

print(min_num)
import sys
input = sys.stdin.readline
# int(input()) # 入力が1つ
k, n = map(int, input().split()) # 入力が複数
houses = list(input().split())
houses = [int(h) for h in houses]
houses.sort()
houses.append(k)
max_dist = 0
total = 0
for i in range(n):
    d = houses[i + 1] - houses[i]
    max_dist = max(max_dist, d)
    total += d

ans = total - max_dist
print(ans)
k, n = map(int, input().split())
list_a = list(map(int, input().split()))

x = 0
longest = 0
for a in list_a:
  if a - x > longest:
    longest = a - x
    x = a - x
print(k-longest)
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))

    shiftA = A[1:] + [K]

    subA = [shiftA[i] - A[i] for i in range(N)]
    maxA = max(subA)
    subA.remove(maxA)

    print(sum(subA))

if __name__ == '__main__':
    main()
K,N=map(int,input().split())
A=list(map(int,input().split()))
ans=0
ans+=min(abs(A[0]-A[-1]),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)
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(m)
print(a-m[0])