peterverebics/CodeNet_Python_118 huggingface dataset:
import numpy as np

N,M=(int(x) for x in input().split())
A=list(map(int, input().split()))
A=np.array(A)
B=A[1:]-A[:-1]
b=max(B)
c=A[-1]-A[0]
if c > N/2:
    c=int(c-N/2)
else:
    c=c
d=max(b,c)
e=N-d
if e > N/2:
    print(N-e)
else:
    print(e)
import numpy as np
KN = input().split()
A = input().split()
K = int(KN[0])
N = int(KN[1])
a = [0]*N
for i in range(N):
    a[i] = int(A[i])
a.sort()
AM = max(a)
Am = min(a)
s = AM-Am
a = np.array(a)
if K/2>=s:
    print(s)
else:
    print(K-(max(a[1:]-a[:-1])))
k,n=input().split()
k=int(k)
n=int(n)
l=list(map(int, input().split()))
ans=[0]
num=l[0]
for i in range(1,len(l)):
    ans.append(min(abs((k-l[i])+num),abs(l[i]-l[i-1])))
print(sum(ans))
K, N = map(int, input().split())
A = list(map(int, input().split()))
sum = 0

if A[0] == 0:
  for i in range(2, N):
    sum += abs(A[i-i] - A[i])
  print(sum)
else:
  for i in range(1, N):
    sum += abs(A[i-1] - A[i])
  print(sum)
k,n = map(int,input().split())
a = list(map(int,input().split()))
maru = [0]*n
ans = 999999999999
maru[0] = abs(a[-1] - k)
for i in range(1,n):
    maru[i] = abs(a[i]-a[i-1])
for i in range(n-(n-1)+1):
    tmp = sum(maru[i:i+(n-1)])
    ans = min(ans,tmp)
print(ans)
k, n = map(int,input(). split())
lstr_a = input().split(" ")
lint_a = [int(n) for n in lstr_a]
alist = sorted(lint_a)
t = alist.count(0)

for i in range(n-1):
  if alist[i]-alist[i-1]>=k/2:
    answer = k-alist[i]+alist[i-1]
    print(answer)
    break

answer = alist[n-1]-alist[0]
print(answer)
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l = []

if a[0] == 0:
    l.append(abs(k - a[n - 1]))
else:
    l.append(abs(a[0] - a[n - 1]))
for i in range(1, n):
    dis = abs(a[i] - a[i - 1])
    l.append(dis)
 
print(sum(l) - max(l))
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] + 20)
    else:
        answer.append(home - A[i - 1])

print(20 - max(answer))
k,n=map(int,input().split())
a=list(map(int,input().split()))
d=abs(a[0]-a[-1])
for i in range(n-1):
  d=max(d,abs(a[i]-a[i+1]))
print(k-d)
K, N=map(int, input().split())
A=list(map(int, input().split()))
for i in range(N-1):
  m=A[N-1]-A[0]
  w=A[i]+K-A[i+1]
  m=min(w, m)
print(m)
K,N = map(int,input().split())
a = [int(i) for i in input().split()]
dist1 = max(a)-min(a)
a1 = []
for i in a:
    if i <= 10:
        a1.append(-i)
    else:
        a1.append(K-i)
dist2 = max(a1)-min(a1)
a2 = []
for i in a:
    if i < 10:
        a2.append(-i)
    else:
        a2.append(K-i)
dist3 = max(a2)-min(a2)
print(min(dist1,dist2,dist3))
import numpy as np
K, N = map(int, input().split())
A = np.array(input().split(), dtype=np.int32)
B = A + (A > K // 2) * (-K)
print(min(A.max() - A.min(), B.max() - B.min()))
k,n=map(int,input().split())
a=list(map(int,input().split()))
s=0
for i in range(n-1):
	s+=a[i+1]-a[i]
	
print(s)
n= [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
lake = n[0]
num_ie = n[1]
kyori = []
for i in range(num_ie-1):
    kyori.append(l[i+1]-l[i])
kyori.append(lake - l[num_ie-1] + l[0])
output = 0
output += sum(kyori) 
output -= max(kyori)

print(kyori)
print(output)
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

A.sort()
max_distance = A[0] + K-A[-1]

old = None
for a in A:
    if not old:
        old = a
        continue
    distance = a - old
    if distance > max_distance:
        max_distance = distance

answer = K - max_distance
print(answer)
k, n = list(map(int,input().split()))
a = list(map(int,input().split()))
max_d = 0
a.append(a[0] + k)
print(a)
for idx, i in enumerate(a[:-1]):
    if max_d < a[idx + 1] - i:
        max_d = a[idx + 1] - i
    
print(k - max_d)
K,N = map(int,input().split())
A = [0] + list(map(int,input().split()))

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

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

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

print(sum(dist))
from sys import stdin,setrecursionlimit
k,n = map(int,stdin.readline().rstrip().split())
a = list(map(int,stdin.readline().rstrip().split()))

print(min(a[-1]-a[0],a[-2]+k-a[-1]))
k, n = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for i in range(n):
    if i + 1 <= n - 1:
        if arr[i + 1] - arr[i] > ans:
            ans = arr[i + 1] - arr[i]
    else:
        if arr[i] - arr[0] > ans:
            ans = arr[i] - arr[0]
print(ans)
k,n=map(int, input().split())
l=list(map(int, input().split()))
m=k//2
l_ = [i+m for i in range(len(l)-1)]
l_ += [l[-1]]
sorted(l_)
print(min(l[-1]-l[0], l_[-1]-l_[0]))
k, n = map(int, input().split())
a = list(map(int, input().split()))

a.append(k)

diff = []

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

ans = sum(diff) - max(diff)
print(ans)
K,N=map(int,input().split())
A=[int(s) for s in input().split(" ")]

Smax=0
for i in range(len(A)):
    S=0
    if i!=len(A)-1:
        S=abs(A[i]-A[i+1])
    else:
        S=abs(A[-1]-A[0])
    if S>Smax:
        Smax=S

print(K-Smax)
import copy
k,n = map(int,input().split())
n_list = list(map(int,input().split()))
new_list = copy.copy(n_list)
for i in range(len(new_list)):
  if new_list[i] > k-new_list[i]:
    new_list[i]=-(k-new_list[i])
new_list = sorted(new_list)
if (n_list[-1]-n_list[0]) < (new_list[-1]-new_list[0]):
  print((n_list[-1]-n_list[0]))
else:
  print((new_list[-1]-new_list[0]))
K, N = map(int,input().split())
A = list(map(int, input().split()))
B = A[N-1] - A[0]
C = K - A[N-1] + A[N-2]

if B > C:
    print(C)
else:
    print(B)
k, n = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()

if a[0] == 0:
    print(abs(min(a[1:])-max(a[1:])))
else:
    print(max(a) - min(a))
K, N = map(int,input().split())
A = list(map(int,input().split()))
right = A[len(A)-1] - A[0]
left = A[0] + K - (A[len(A)-1]-A[1])
if right >= left:
    print(left)
else:
    print(right)
k,n = map(int,input().split())
a = list(map(int, input().split(' ')))
x = k - a[-1]
for i in range(n-1):
    y = a[i+1]-a[i]
    if y > x:
        x = y
print(k-x)
hoge = input().split()
K = int(hoge[0])
N = int(hoge[1])
A_list = list(map(int,input().split()))

kankaku = []
for i in range(len(A_list) - 1):
  kankaku.append(A_list[i+1] - A_list[i])
kankaku.append(A_list[0] - A_list[-1] + K)

print(kankaku)
_kankaku_max = max(kankaku)
print(K - _kankaku_max)
K,N=map(int,input().split())
A=list(map(int,input().split()))
n=2*10^6
for i in range(N):
  m = abs(A[i-1] - A[i])
  if n > m:
    n = m
print(m)
k,n=map(int,input().split())
a=list(map(int, input().split()))
max_dis = 0
for i in range(n):
  if i == n-1:
    dis = k - a[i]
  else:
    dis = a[i+1] - a[i]
  max_dis = max(max_dis,dis)
print(k-max_dis)
def main():
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    cost = 10*10
    for i in range(n-1):
        cost = min(cost, k - abs(a[i]-a[i+1]))
    print(min(cost, k-abs(a[0]+k-a[n-1])))

if __name__ == "__main__":
    main()
length, num = map(int, input().split())
list1 = [int(i) for i in input().split()]
list2 = []
for j in range(num-1):
    list2.append(list1[j+1] - list1[j])
list2.append(length - list1[num-1])
list2.pop(list2.index(max(list2)))
print(sum(list2))
ddd = [input() for i in range(2)]
eee = ddd[0].split()
fff = ddd[1].split()
longest_distance = 0
for i in range(len(fff) - 1):
    distance = int(fff[i+1]) - int(fff[i])
    if longest_distance < distance:
        longest_distance = distance
shortest_road = int(eee[0]) - longest_distance
print(shortest_road)
k,n=[int(i) for i in input().split()]
A=[int(i) for i in input().split()]
m=100000
s=0
for i in range(len(A)-1):
    s+=abs(A[i]-A[i+1])
x=A[len(A)-1]
x=k-x
for i in range(len(A)-1):
    x+=A[i]
x2=A[0]
x2=x2+abs(k-A[len(A)-1])
for i in range(len(A)-1,0,-1):
    x2+=abs(A[i]-A[i-1])
print(min(x,s,x2))
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

K, N = na()
A = na()
A.append(K)
t = [A[i+1] - A[i] for i in range(N)]
print(sum(t) - max(t))
K, N = map(int, input().split())
lst = [int(x) for x in input().split()]
a = 0
for i in range(N - 1):
    a = max(a, lst[i + 1] - lst[i])
print(K - a - lst[0])
k,n = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
m = k-a[n-1] + a[0]
for i in range(1,n):
  ans = 0
  ans = k-a[i]+a[i-1]
  m = min(ans,m)
print(m)
k, n = map(int, input().split())
a = list(map(int, input().split()))

d = 0
pre = a[0]
for i in a[1:]:
    if i - pre > d:
        d = i - pre
    pre = i
if k - a[-1] + a[0] > d:
    d = a[-1] - a[0]
print(k - d)
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=0
for i in range(N):
    if (A[i]>K):
        A[i]=A[i]%K
A.sort()
for i in range(N-2):
    if (A[i+2]-A[i+1])>=(A[i+1]-A[i]):
        l=A[i+2]-A[i+1]
if (K-A[N-1]+A[0])>=l:
    l=K-A[N-1]+A[0]
print(K-l)
def main():
    k, n = map(int, input().split())
    num_list = list(map(int, input().split()))

    max_num = num_list[n-1]
    min_num = num_list[0]
    if min_num == 0:
        min_num = num_list[1]

    ans1 = max_num - min_num

    ans2 = k - max_num + num_list[n-2]

    print(min(ans1, ans2))

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

a = A[1] - A[0]

for i in range(1,N):
    b = A[i] - A[i-1]
    c = max(a,b)

d = K - A[N-1] + A[0]

e = max(c,d)

print(min(e,K-e))
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]-b[0]))
    elif i!=a[1]-1:
        c.append(abs(b[i]-b[i+1]))
print(a[0]-max(c))
import numpy as np
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.append(k)
A.sort()
dist = np.diff(A)
ans = k-max(dist)
print(ans)
KN = list(map(int, input().split()))
A = list(map(int, input().split()))

answer = []

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

print(20 - max(answer))
K, N = map(int, input().split())
A = [int(x) for x in input().split()]
diff = []
for a, b in zip(A, A[1:]):
    diff.append(b - a)
diff.append(A[0] - 0 + K - A[-1])
a = max(diff)
print(min(a, K - a))
K, N = input().split()
K = int(K)
N = int(N)
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[N-1] - A[0]) and A[0] > 0:
    max = A[N-1] - A[0]
print(K - max)
K, N = map(int, input().split())
A = list(map(int, input().split()))
A = A + [K] # len = N+1

diff = []
for i in range(len(A)):
    if i < N:
        diff.append(A[i+1] - A[i])

longest_interval = diff.index(max(diff))
diff.pop(longest_interval)
ans = sum(diff)

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

print(k-c)
K,N=map(int,input().split())
Alist=list(map(int,input().split()))
kyori=[]
ans=0
for i in range(N-1):
    kyori.append(Alist[i+1]-Alist[i])
kyori.append(K-Alist[N-1])
kyori.remove(max(kyori))
for i in range(len(kyori)):
    ans+=kyori[i]
print(ans)
k,n = map(lambda x: int(x),input().split())
a = list(map(lambda x: int(x),input().split()))
a.append(k)
sabun = [a[i+1] -a[i] for i in range(n)]

print(sum(sorted(sabun)[:n-1]))
K,N = map(int,input().split())
A = list(map(int,input().split()))

S =[A[0]]+ [0]*(N)

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

Smax = S.index(max(S))

ans = sum(S)-S[Smax]
print(ans)
k,n = map(int,input().split())
lst_a = list(map(int,input().split()))
dis_max = 0
lo_last = lst_a[0]
for i in range(1,n):
    ilo = lst_a[i]
    dis_max = max(dis_max, ilo-lo_last)
    lo_last = ilo
ilo = lst_a[0]
dis_max = max(dis_max, ilo-lo_last)
print(k-dis_max)
K,N = map(int,input().split())
L = list(map(int,input().split()))
L.append(min(L)+max(L))
L.remove(min(L))
res = 0
for i in range(N-1):
    res += abs(L[i+1]-L[i])
print(res)
K, N = map(int, input().split())
points = list(map(int, input().split()))
max_index = -1
max_value = -1

for index in range(1, N):
  d = points[index] - points[index-1] 
  if d > max_value:
    max_index = index
    max_value = d

start = K - points[max_index]
end   = points[max_index-1]

print(start + end)
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]
a.append(k-sum(a))
ans = k-max(a)
print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
s = []
count = 0
for i in a:
  if i > k / 2:
    s.append((k - a[count]) * -1)
  else:
    s.append(a[count])
  count += 1
ans_1 = abs(min(s)) + abs(max(s))
d = []
counts = 0
for j in a:
  if i < k:
    d.append((k - a[counts]) * -1)
  else:
    d.append(a[counts])
  counts += 1
ans_2 = max(a) - min(a)
ans_3 = max(d) - min(d)
ans = min(ans_1, ans_2, ans_3)
print(ans)
data = input().split()
k = int(data[0])
n = int(data[1])

data = input().split()
house = [int(s) for s in data]

h_distance = [house[0]]
for i in range(1, n):
    h_distance.append(house[i] - house[i - 1])

m_distance = max(h_distance)
total_d = 0
for i in h_distance:
    total_d += i

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

if A[N-1]-A[N-2] >= 10:
  ans = (K-A[N-1])+A[N-2]
else:
  ans = A[0]+A[N-1]
  
print(ans)
k,n=map(int,input().split())
a=list(map(int,input().split()))
diff=0
for i in range(len(a)-1):
  diff_a=a[i+1]-a[i]
  if diff<diff_a:
    diff=diff_a
if a[n-1]-a[0]>diff:
  diff=a[n-1]-a[0]
print(k-diff)
def main():
    k, n = map(int, input().split())
    num_list = list(map(int, input().split()))

    max_num = num_list[n-1]
    min_num = num_list[0]
    # if min_num == 0:
    #     min_num = num_list[1]

    ans1 = max_num - min_num

    ans2 = k - max_num + num_list[n-2]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()
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//2 - l3[0]),l[-1] - l[0]))
k, n = map(int,input().split())
a = list(map(int, input().split()))
ans1 = max(a) - min(a)
for i in range (0,n):
    if a[i] > (k/2):
        a[i] = (k/2)-a[i]
ans2 = int(abs(max(a) - min(a)))
print(min(ans1,ans2))
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[n-1])

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

ans = []
for i in range(len(A) - 1):
    base = abs(A[i+1] - A[i])
    comp = abs(A[0] - (K - A[i + 1]))
    if base < comp:
        ans.append(base)
    else:
        ans.append(comp)

print(sum(ans))
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
a=A[-1]-A[0]
for i in range(N):
    A[i]=K//2-A[i]
A.sort()
b=A[-1]-A[0]
print(min(a, b))
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [0] * n
for i in range(n-1):
     b[i] = a[i+1] - a[i]
b[n-1] = k - a[n-1]
print(sum(b)-max(b))
def main():
  k, N = (int(x) for x in input().split())
  min_LIST = []
  LIST = []
  LIST = list(map(int,input().split()))

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(min_LIST[i] <= (LIST[i + 1] - LIST[i])):
      x = (LIST[i + 1] - LIST[i])

  if(N != 2):
    min_LIST.append(LIST[0] + (k - LIST[-1]))
    if(x <= (LIST[0] + (k - LIST[-1]))):
      x = (LIST[0] + (k - LIST[-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()
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))
import sys
input = sys.stdin.readline

def main():
    n, m = map(int, input().split())
    lis = list(map(int, input().split()))
    su = 0
    for i in range(m - 1):
        su += lis[i + 1] - lis[i]
    ou = lis[0] - lis[-1] + n
    for i in range(m - 2):
        ou += lis[i + 1] - lis[i]
    print(min(ou,su))

if __name__ == '__main__':
    main()
K = 20
N = 3
A = [5, 10, 15]

ans = 0
for i in range(N - 1):
    S = [A[i] - A[i + 1]]
    S.append((K - A[N - 1]) + A[0])
    S.sort
    for n in range(N - 1):
        ans += S[i]
print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
if a[0] != 0: print(a[-1] - a[0])
else:
    a_ = [a[i] - a[i-1] for i in range(1, n)]
    a_.append(k - a[-1])
    print(k - max(a_))
k,n=map(int,input().split())
a=list(map(int,input().split()))
print(max(a)-min(a))
K, N = map(int, input().split())
A = list(map(int, input().split()))
if (N == 2):
    print(A[1] - A[0])
    exit(0)
if (A[0] == 0): A[0] = A[1]
a = A[N - 1] - A[0]
b = abs(K - A[N - 1]) + A[0]
print(min(a, b))
k,n=map(int,input().split())
a=list(map(int,input().split()))

print(min(k-a[-1]+a[-2],a[-1]-a[0]))
K,N=map(int,input().split())
dis=list(map(int,input().split()))
dis.sort()
A=0
for i in range(N):
    A=max(A,dis[i]-dis[i-1])
print(K-A)
import numpy as np

k, n = [int(i) for i in input().split()]
a = np.array([int(i) for i in input().split()])

b = np.min(a)

print(np.max(a-b))
k,n = map(int,input().split())
A = list(map(int,input().split()))
r = 0
for i in range(1, n):
    r = max(r, A[i]-A[i-1])
r = max(r, k-A[-1])
print(k-r)
K,N = input().split()
K = int(K)
N= int(N)
A = [int(x) for x in input().split()]

dis=[]
now = 0

for i in range(N):
    dis.append(A[i] - now)
    now = A[i]

dis.append(K - A[N -1])

distance = K - max(dis)

print(distance)
k, n = map(int, input().split())
a = list(map(int, input().split()))
d = 0
for v in range(n-1):
    d = max(0, a[v+1]-a[v])
print(d)
k, n = map(int, input().split())
*a, = map(int, input().split())
diffs = [a[i + 1] - a[i] if i < n - 1 else k - a[i] for i in range(n)]
diffs.sort()
print(sum(diffs[:-1]))
k, l = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 0)
a.append(k)
e = []
for i in range(len(a)-1):
    e.append(a[i + 1] - a[i])
ans = sum(e) - max(e)-(e[1]-e[0])-(e[-1]-e[-2])
print(ans)
K,N = map(int,input().split())
L = list(map(int,input().split()))
res = 0
for i in range(N-1):
    res += min(abs(L[i]-L[i+1]),abs(L[i]-L[i-1]))
print(res)
n,m = map(int,input().split())
lst = list(map(int,input().split()))
prev = 0
m =0
for i in range(m):
    m = max(m, abs(prev - lst[i]))
    prev = lst[i]
last = n - lst[-1] + lst[0]
k = max(m, last)
print(n - k)
def check(a):
  if a < 0:
    return a * -1
  else:
    return a

def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = []
  for x in range(N-1, -1, -1):
    if As[x] == 0:
      r.append(check(K - As[x-1]))
      continue
    elif As[x-1] == 0:
      r.append(check(As[x]))
      continue
    solve = As[x] - As[x-1]
    r.append(check(solve))
  ans = sorted(r)
  ans.remove(ans[-1])
  sum_ans = sum(ans)
  print(sum_ans)

if __name__ == "__main__":
  main()
K,N=map(int,input().split())
A=[int(x) for x in input().split()]

m=max(A[i+1]-A[i] for i in range(N-1))
print(A[N-1]-A[0] if K>2*m else K-m)
round,house_num = map(int,input().split())

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

min = distance[-1] - distance[0]

for i in range(house_num - 1):
  if round-(distance[i+1]-distance[i]) < min:
    min = round-(distance[i+1]-distance[i])
  print(min)
k,n = map(int,input().split()) 
a =list(map(int,input().split()))
for i in range (n) :
    dist1 = (a[i-1]-a[i])%k
    dist2 = (a[i]-a[(i+1)%n])%k
    ans =min(dist1,dist2)
print(ans)
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [0 * n for i in range(n)]
for i in range(n-1):
  b[i] = a[i+1] - a[i]

b[-1] = a[0] + (k - a[-1])

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

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

    print(ans)


if __name__ == "__main__":
    main()
k, n = map(int, input().split())
A = list(map(int, input().split()))
s = A[0]
for i in range(n):
  A[i] -= s 
ans = 0
idx = n
for i in range(n):
  if A[i] > k-A[i]:
    ans += k-A[i]
    idx = i
    break

ans += A[idx-1]
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for a in A:
    if a > K // 2:
        ans += a - K // 2
    else:
        ans += a

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

if l[0]==0:
    l.pop(0)
    n-=1

a=l[n-1]-l[0]
b=k-a

print(min(a,b))
import numpy as np

K, N = map(int, input().split())
A = [int(x) for x in input().split()]
A.append(K)

diff_A = []
for i in range(N):
    diff_A.append(A[i+1] - A[i])
diff_max = max(diff_A)
print(K - diff_max)
KN = list(map(int, input().split()))
dis = list(map(int, input().split()))
lists = []
for i in range(KN[1]-1):
    lists.append(dis[i+1]-dis[i])

lists.append(KN[0]+dis[0]-dis[KN[1]-1])
print(lists)
print(KN[0]-max(lists))
k, n = map(int, input().split())
a = list(map(int, input().split()))
if n == 2 :
  a.sort(reverse=True)
  print(min(k - max(a), a[0] - a[1]))  
elif 0 in a :
  a.sort(reverse=True)
  print((k - max(a)) + a[1])
else :
  print(max(a) - min(a))
k, n = map(int, input().split())
array = list(map(int,input().split()))
max_length = 0
sum_length = 0
for i in range(n - 1):
  length = array[i + 1] - array[i]
  sum_length += length
  if length > max_length:
    max_length = length
last_length = k - array[n - 1]
sum_length += last_length
max_length = max(max_length, last_length)
print(sum_length - max_length)
K, N = map(int, input().split())
A = list(map(int, input().split())) 

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

print(d)
print(sum(d) - max(d))
K, N = map(int,input().split())
A = list(map(int,input().strip().split()))
su=0
for i in range(N):
	su=abs(su-A[i])
print(su)
def travel(ar,K):
    m = K//2
    s = 0
    for a in ar:
        s+= a%m
    return s

def io():
    K,N = map(int,input().split())
    ar = list(map(int,input().split()))
    print(travel(ar,K))

if __name__=='__main__':
    io()
k, n = map(int, input().split())
a = list(map(int, input().split()))
l = []
if a[0] == 0:
    l.append(abs(k - a[n - 1]))
else:
    l.append(abs(a[0] - a[n - 1]))
for i in range(1, n):
    dis = abs(a[i] - a[i - 1])
    l.append(dis)
    
print(sum(l) - max(l))
# 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(abs(k - a[i]))
  else:
  	distance.append(abs(a[i+1] - a[i]))
distance.sort()
print(sum(distance[:-1]))
k,n=map(int,input().split())

a=list(map(int,input().split()))
l=0
r=0
t=a[n-1]
ans=0

for i in a[:n-1]:
    right=t-i
    left=k-right
    tl=0
    tr=0
    if left>l:
        tl=left-l
    if right>r:
        tr=right-r

    if ans+tl<ans+tr:
        l=left
        ans=ans+tl
    else:
        r=right
        ans=ans+tr

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

for i in range(N):
    if i + 1 < N - 1:
        sa = A[i+1] - A[i]
    else:
        sa = A[0] + (K - A[N-1])
    print(sa)
    gap.append(sa)
    i += 1
    
dist = max(gap)
ans = K - dist
print(ans)
K, N = input().split()
A = list(map(int, input().split()))
if 0 in A:
    A.remove(0)
x = max(A) - min(A)
print(x)