peterverebics/CodeNet_Python_118 huggingface dataset:
K, N = map(int, input().split())
an = list(map(int, input().split()))


dis =[]

dis.append(an[0] + K - an[len(an)-1])

for i in range(N-1):
    dis.append(an[i+1]-an[i])

work = sorted(dis)
print(work[0]+ work[1])
# 隣り合う長さの最大値をKから引く
circ_length, n_house = map(int, input().split())
places = list(map(int, input().split()))

delta_places = []
for i in range(n_house):
  d = abs(places[i] - places[i-1])
  delta_places.append(d)

result = circ_length - max(delta_places)
print(result)
K, N = map(int, input().split())
A = list(map(int, input().split()))
r1 = A[N-1] - A[0]
r2 = (K - A[N-1]) + A[N-2]
print(min(r1, r2))
k,n=map(int,input().split())
hoge=list(map(int,input().split()))
ans=hoge[0]+k-hoge[-1]
for i in range(n-1):
  ans=max(ans,hoge[i+1]-hoge[i])
  
print(ans)
K, N = [int(n) for n in input().split()]
A = [0]
A += [int(n) for n in input().split()]
A += [K]
dx = []
for i in range(N+1):
    dx.append(A[i+1] - A[i])
m = max(dx)
print(K-m if dx.index(m) != 0 else K-dx[-1]-dx[0])
syu,n=map(int,input().split())
a=[int(i) for i in input().split()]
clock=a[n-1]-a[0]
han_clock=(a[n-2]-a[1])+(a[0]+syu-a[n-1])
print(min(clock,han_clock))
k, n = map(int, input().split())
a = list(map(int, input().split()))

x = 0
index = 0

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

if x < k-a[-1]+a[0]:
    x = k-a[-1]+a[0]

print(k - x,k,  x,k-a[-1]+a[0])
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-max(a))
ans = max(a)
print(ans)
K, N = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
Ar = [i for i in A]
for i,a in enumerate(A):
    if K - a < a:
        Ar[i] = a - K
print(min(max(A) - min(A), max(Ar) - min(Ar)))
import numpy as np


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

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

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

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

A_list = second_input

A_list.append(K+A_list[0])

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

print(sum(dist_list)-max(dist_list))
k,n = map(int,input().split())
a=list(map(int,input().split()))
ans=0
for i in range(n):
  ans=max(ans,abs(a[i]-a[i-1]))
print(ans)
k, n = map(int, input().split())
a=[0]*n
a=list(map(int, input().split()))
for i in range(n):
    if a[i]>(k+0.5)//2:
        a[i]=int((k+0.5)//2)-a[i]
la=sorted(a)
print(max(a)-la[0])
K, N = map(int,input().split())
A = input().split()
for i in range(N):
    A[i] = int(A[i])
max = 0
for i in range(N-1):
    diff = abs(A[i] - A[i+1])
    if diff >= max:
        max = diff

if abs(K - A[N-1]) >= max:
    max = abs(K - A[N-1])

print(K - max)
k,n=map(int, input().split())
a=list(map(int,input().split()))
t=k/2
min1=t
max1=0
min2=k
max2=t
for i in range(n):
    if a[i]<=t:
        if a[i]<min1:
            min1=a[i]
        if a[i]>max1:
            max1=a[i]
    else:
        if a[i]<min2:
            min2=a[i]
        if a[i]>max2:
            max2=a[i]
print(int(min(k-min2+max1,max2-min1)))
k, n = map(int, input().split())
#print(k)
#print(n)
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

for i in reversed(range(0, n)):
  if l_si_i[i] == 0:
    l_si_i.pop(i)

#print(l_si_i)

minIndex = l_si_i.index(min(l_si_i))

#print(minIndex)

if minIndex != 0:
  temp = l_si_i.pop(minIndex)
  l_si_i.insert(0, temp)

#print(l_si_i)

sub = 0
l = len(l_si_i)
for i in range(l):
  if i != 0:
    sub += l_si_i[i] - l_si_i[i-1]

print(sub)
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
import numpy as np

k,n = mi()
a=np.array(li())

b = a[1:] - a[:n-1]
b_max = np.max(b)
c = a[0] + k-a[n-1]

print(max(b_max,c))
K, N = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]
#print(K,N,A)
min_dis = K+1

for i in range(len(A) -1):
  dis = A[i+1] - A[i]
  if dis < min_dis:
    ans = (i+1)+1
    min_dis = dis
print(ans)
def main():
    K, N = map(int, input().split(' '))
    half_k = K // 2
    A = list(map(int, input().split()))
    A.sort()
    a_sub = A[-1] - A[0]
    if A[-1] > half_k:
        a_sub = (A[-1] - half_k) + A[0]
    if a_sub < 0:
        a_sub *= -1
    for n in range(N - 1):
        if A[n + 1] - A[n] > a_sub:
            a_sub = A[n + 1] - A[n]
    print(K - a_sub)


if __name__ == '__main__':
    main()
n,k = map(int,input().split())
a = sorted([i for i in map(int,input().split())])
maxi = -1
ans = (n + (n - a[-1]%n))%n + a[0]
#print(ans)
for i in range(1,len(a)):
    maxi = max(maxi,a[i] - a[i - 1])
    ans += a[i] - a[i - 1]
maxi = max(maxi,(a[-1] + (a[-1] - a[0]))%n)
ans -= maxi
print(ans)
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,len(num_list)):
    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)
K,N=map(int,input().split())
A=sorted(list(map(int,input().split())))

max_a = max(A)
min_a = min(A)
diff_min = K

if max_a - min_a <= K/2:
    print(max_a-min_a)
else:
    if K - A[1] - min_a < max_a - min_a:
        print(K - A[1] - min_a)
    else:
        print(max_a - min_a)
def main():
  K,N = map(int, input().split())
  A_list = list(map(int, input().split()))

  space_max = 0

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

  for i in range(N-1):
    space =  A_list[i+1] - A_list[i]
    if space > space_max:
      space_max = space
    else:
      pass
  
  print(K-space_max)


if __name__ == "__main__":
  main()
from collections import deque
k,n = map(int,input().split())
a = deque(list(map(int,input().split())))
b = []
for i in range(n):
	b.append(a[i]-k)
b = deque(reversed(sorted(b)))
for i in range(n):
	a.appendleft(abs(b[i]))
a = list(a)
x = []
sa = []
#print(a)
sb = []
for i in range(len(a)):
	sa.append(sum(a[:i+1]))
for i in range(len(a)-n):
	sb.append(sa[i+n]-sa[i])

print(min(sb))
k,n = map(int,input().split())
a = list(map(int,input().split()))
l = []
l.append(abs(k-abs(a[0]-a[n-1])))
for i in range(n-1):
	l.append(abs(a[i]- a[i+1]))
print(l)
def main():
    K,N,*A=map(int, open(0).read().split())
    A+=[A[0]+K,A[1]+K]
    print(K-min(abs(A[i+2]-A[i]) for i in range(N)))

if __name__ == "__main__":
    main()
k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [0] + a + [k[0]]
sa = [b[i + 1] - b[i] for i in range(len(b) - 1)]
c = sa[1:-1]
if k[1] % 2 == 1:
  temae = (len(sa) // 2) -1
  oku = temae + 1
  d = sum(sa) - sa[temae]
  e = sum(sa) - sa[oku]
  print(min([sum(c), d, e]))
else:
  x = len(sa) // 2
  d = sum(sa) - sa[x]
  print(min([sum(c),d]))
import sys
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().strip()

def main():
    K,N = map(int,input().split())
    A_list = list(map(int,input().split()))
    dist_list = []
    A_list.append(K)
    for i in range(1,len(A_list)):
        dist_list.append(A_list[i]-A_list[i-1])
    d_sorted = sorted(dist_list)
    print(int(sum(d_sorted[0:N-1])))
main()
import numpy as np

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

# A=list(map(int, input().split()))
K,N=[20, 4]

A=[5, 10, 15]

A=np.array(A)

lin=np.array(A[:-1])
le=np.array(A[1:])

length=lin-le

rem=abs(K-A[-1]+A[0])

A=np.append(length,rem)

M=np.max(A)

print(K-M)
k, n = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(n):
    A[i] = A[i] - A[0]
    if abs(A[i]) >= k/2:
        A[i] = abs(A[i]) - k
A.sort()
#print(A)
ans = A[n-1] - A[0]
print(ans)
k, n = map(int , input().split())
lis = list(map(int,input().split()))
mi = [min(lis)] * n
li = [x - y for (x, y) in zip(lis, mi)]

ans1 = 0
for i in range(n - 1):
    ans1 += li[(i + 1)] - li[i]

chuten = k / 2
for i,j in enumerate(li):
  if j > chuten:
    li[i] = j - k
li.sort()
ans2 = 0
for i in range(n - 1):
    ans2 += li[(i + 1)] - li[i]

print(min(ans1, ans2))
k,n = map(int,input().split())
arr = list(map(int,input().split()))

res = 0
for i in range(n):
    if arr[i] != 0:
        res += abs(arr[i]-(k/2))
print(int(res))
K,N = map(int,input().split())
A = list(map(int,input().split()))
X = 0
Y = 0
for i in range(N):
    if A[i] == 0:
        A[i] = K
A.sort()
b = A.copy()
for i in range(N-1):
    #時計回り
    X +=  b[i+1]-b[i]
c = A.copy()
c.reverse()
for j in range(N-1):
    Y += A[i+1]-A[i]
print(min(X,Y))
k, n = map(int,input().split())
a = list(map(int,input().split()))
a.append(k)

ans = 0
max_dis = 0

for i in range(n):
    dis = a[i+1] - a[i]
    max_dis = max(max_dis,dis)
    ans += dis
    
ans -= max_dis
print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
max = 0
len = [0] * n
for i in range(n):
    if i == 0:
        len[i] = a[i]
    else:
        len[i] = a[i] - a[i - 1]
        if len[i] > len[max]:
            max = i
if len[max] > int(k / n) and len[max] > (k - a[n - 1]) and (a[n - 1] - a[0]) > len[max]:
    ans = (k - a[max]) + a[max - 1]
else:
    ans = a[n - 1] - a[0]
print(ans)
n,k=map(int,input().split())
l=list(map(int,input().split()))


s=0
check=0
f=0
save=0
for i in range(1,k):
	s=s+l[i]-l[i-1]
	if(f==0 and l[i]>= n//2):
		check=i
		f=1
	else:
		if(f==0):
			save=s

s1=0
#print(save,check)
for i in range(check+1,k+1):
	s1=s1+n-abs(l[i%k]-l[(i-1)%k])

s1=s1+save
#print(s,s1)
print(min(s,s1))
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.append(n)
d=[]
for i in range(len(a)-1):
    d.append([i,a[i+1]-a[i]])
l=sorted(d,key=lambda x:x[1])

o=l[-1][0]
g=a[o]+n-a[o+1]
print(g)
k=list(map(int, input().split(' ')))
a=list(map(int, input().split(' ')))
m=a[0]
for i in range(k[1]):
    if i==k[1]-1:
        if k[0]-a[i]+a[0]>m:
            m=k[1]-a[i]+a[0]>m
    else:
        if a[i+1]-a[i]>m:
            m=a[i+1]-a[i]>m
print(k[0]-m)
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [0]*n
a.sort()
amin = a[n-1]-a[0]
for i in range(n):
  if a[i] < k/2:
    b[i] = a[i] + k
  else:
    b[i] = a[i]
b.sort()
bmin = b[n-1]-b[0]

print(min(amin,bmin))
K,N = map(int,input().split())
L = [i for i in map(int,input().split())]
L.append(L[0])
dist = []
for i in range(N):
  if L[i+1] == 0 or L[i+1] == K:
    dist.append(min(abs(0-L[i]),abs(K-L[i])))
  else:
    dist.append(abs(L[i+1]-L[i]))
print(sum(dist)-max(dist))
K,N = map(int,input().split())
A = list(map(int,input().split()))
ls = []
for i in range(N):
    #print(i)
    if i != N-1:
        ls.append(A[i+1]-A[i])
    else:
        ls.append(abs(A[0]-A[i]))
#print(ls)
print(sum(ls)-max(ls))
k,n = map(int,input().split())
a = list(map(int,input().split()))
aa = a + a

for j in range(n,2*n):
    aa[j] = aa[j] + k

dis = k

for i in range((2*n)-1):
    tonari = aa[i+1]-aa[i]
    dis = min(dis,k-tonari)

print(dis)
print(aa)
k, n = list(map(int, input().split()))
an = list(map(int, input().split()))
bn = []
for i in range(n-1):
  bn.append(an[i+1]-an[i])
bn.append(k-an[n-1])
print(sum(bn)-max(bn))
K,N=map(int,input().split())
A=list(map(int,input().split()))
for i in range(1,N):
  L=0
  L=max(L,A[i]-A[i-1])
L=max(L,K-A[-1]+A[0])
print(K-L)
K,N=map(int,input().split())
A=list(map(int,input().split()))
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 (20-A[N-1]+A[0])>=l:
    l=20-A[N-1]+A[0]
print(K-l)
K,N=map(int,input().split())
A=list(map(int,input().split()))
l=0
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 (20-A[N-1]+A[0])>=l:
    l=20-A[N-1]+A[0]
print(K-l)
k,n = map(int,input().split())
a = list(map(int,input().split()))
for i in range(n-1):
    a.append(k+a[i])
ans = a[n-1]-a[0]
for i in range(n,n*2-1):
    ans = min(ans,a[i]-a[i-n])
print(ans)
class atcorder:
    
    K,N=map(int,input().split())
    A=list(map(int,input().split()))
    ans=0
    i=N-1
    if A[i]-A[i-1]<K-A[i]+A[0]:
        for n in range(i):
            ans+=A[i-n]-A[i-n-1]
    else:
        ans+=K-A[i]-A[0]
        for n in range(i-1):
            ans+=abs(A[n]-A[n+1])
    print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
if len(a) == n:
    if 0 in a:
        a.remove(0)
    a.sort()
    ans = 0
    n = 1
    for i in a:
        try:
            ans += a[n] - a[n-1]
            n += 1
        except:
            pass
    print(ans)
# スペース区切りの文字列の入力
KN = input().split()
A = input().split()

i  = ans = 0
max = 0

for i in range(int(KN[1])):
  if(i == int(KN[1]) - 1):
    A2 = int(KN[0])
  else:
    A2 = int(A[i+1])
    
  # 距離が最大のもの以外をansに加算
  if(max < abs(A2 - int(A[i]))):
      ans += max
      max = abs(A2 - int(A[i]))
  else:
    ans += abs(A2 - int(A[i]))
print(ans)
k,n,*a=map(int,open(0).read().split())
a+=[k+a[0]]
print(min(y-x for x,y in zip(a,a[1:])))
k,n = map(int, input().split())
l = list(map(int, input().split()))
d = []
for i in range(n):
  if i + 1 < n:
    d.append(l[i+1]-l[i])
  else:
    d.append(k - l[i])
sum = sum(d)
a = []
for i in range(n):
  a.append(sum - d[i])
print(min(a))
K, N = map(int, input().split())
A = list(map(int, input().split()))

dist = [A[i+1] - A[i] for i in range(N-1)]

if A[N-1] - A[0] <= K / 2:
    res = A[N-1] - A[0]
else:
    res = K - max(dist)

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


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

B = sorted(B,reverse = False)

print(sum(B[:N-1]))
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(k)

s = 0
for i in range(n):
    s += a[i + 1] - a[i]
ans = s
for i in range(n):
    ans = min(ans, s - a[i + 1] - a[i])

print(ans)
K,N=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
d1=l[len(l)-1]-l[0]
for j in range(len(l)):
	if l[j]>K/2:
		l[j]-=K
l.sort()
d2=l[len(l)-1]-l[0]
print(min(d1,d2))
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[]
for i in(A):
  if(i!=0):
    B.append(i)
B.sort()

if((B[-1]-B[0])<(B[0]+K-B[-1])):
   print(B[-1]-B[0])
else:
   print(B[0]+K-B[-1])
a, b = map(int, input().split())
r = list(map(int, input().split()))

ri = r[-1] - r[-2]
le = r[0] + a - r[-1]

if ri < le:
    print(r[-1] - r[0])
else:
    num = 0
    for i in range(1, len(r) - 1):
        num += (r[i] - r[i-1])
    print(a - r[-1] + num)
k, n = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
B = [0]*n

for i in range(n):
    B[i] = A[i] - A[0]
    if B[i] > k/2:
        B[i] = B[i] - k
B.sort()
print(B[n-1] - B[0])
k, n = map(int, input().split())
a = list(map(int, input().split()))

d = [None]*n

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

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

print(k - max(d))
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=a
for i in range(1,n):
    a[i]-=b[i-1]
print(k-max(a))
import sys
sys.setrecursionlimit(4100000)

k,n = map(int,input().split())
a = list(map(int,input().split()))
b = []
for v in range(n-1):
    b.append(a[v+1]-a[v])

b.append(k-a[n-1]+a[0])
print(b)
anss = max(b)
ans = k - anss
print(str(ans))
k, n = map(int, input().split())
a = list(map(int, input().split()))

space = []
for i in range(n - 1):
    s = a[i + 1] - a[i]
    space.append(s)
space.append(k - a[n - 1])

print(k + a[1] - max(space))
K, N = map(int, input().split())
A = sorted(map(int, input().split()))

length = K//2
for i in range(N-1):
    length = max(A[i+1] - A[i] , length)
    if length == A[i+1] - A[i]:
        ans = A[i] + K - A[i+1]
      
if length <= K//2:
    ans = A[-1] - A[0]
print(ans)
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
A = np.array(A)
ma = 0
for i in range(N-1):
  tmp = abs(A[i] -A[i+1])
  if min(tmp,K - tmp) > ma:
    ma = min(tmp,K-tmp)
print(K - ma)
K=int(input().split()[0])
lst=list(map(int,input().split()))
lst.append(K-sum(lst))
print(K-min(lst))
import numpy as np
a, b = map(int, input().split())
l = list(map(int, input().split()))
f = b - 1
g = b - 2
c = int(l[-1])
d = int(l[0])
v = list([1]*b)
for i in range (f):
  	v[i] = l[i+1]-l[i]
v[f] = a + l[0] - l[-1]
print(v)
if	max(v) == v[f]:
	print(l[-1]-l[0])
else:
	print(a-max(v))
k, n = map(int , input().split())
li = list(map(int,input().split()))

ans1 = 0
for i in range(n - 1):
    ans1 += li[(i + 1)] - li[i]
#print(ans1)

chuten = k / 2
for i,j in enumerate(li):
  if j > chuten:
    li[i] = j - k
li.sort()
#print(li)
ans2 = 0
for i in range(n - 1):
    ans2 += li[(i + 1)] - li[i]
#print(ans2)

print(min(ans1, ans2))
data = input().split()
k = int(data[0])
n = int(data[1])
 
data = input().split()
house = [int(s) for s in data]
 
ans = min(house[len(house) - 1], k - house[0])
print(ans)
K,N = map(int, input().split())
A = list(map(int, input().split()))
A.append(K)
s = 0
for i in range(N):
    if A[i+1] - A[i] > s:
        s = A[i+1] - A[i]
print(A[N] -s)
K, N = map(int, input().split())
A = list(map(int, input().split()))
lst = []
for i in range(N-1):
    diff = abs(A[i + 1] - A[i])
    lst.append(diff)

if A[0] == 0:
    lst.append(K - A[N - 1])

else:
    lst.append(A[N - 1] - A[0])

print(lst)
print(K - max(lst))
K, N =map(int,input().split())
A_n =list(map(int,input().split()))

A_n.sort()

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

max1 = 0
for i in range(1,n-1):
    max1 = max((a[i]-a[i-1]),max1)
max1 = max(max1,(a[0]+(k-a[n-1])))
print(k - max1)
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)

#入力
k,n = map(int,input().split())
a = list(map(int,input().split()))
diff = []
for i in range(len(a)):
    d = abs(a[i-1] - a[i])
    diff.append(d)
diff.sort()
print(k-diff[-1])
#解答
#出力
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
dist=[0]*(n)

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

right=a[n-1]-a[0]
left=k-dist[0]

ret=min(left,right)

for i in range(1,n):
    right+=dist[(i+n-2)%n]-dist[i-1]
    left+=dist[i-1]-dist[(i+n-2)%n]
    ret=min(left,right)

print(ret)
K,N = map(int,input().split())
A = [0] + list(map(int,input().split())) + [K]

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

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

for i in range(N):
  if A[i] > K/2:
    A[i] = K/2 - A[i]
 
A.sort()
print(A)

print(int(A[N-1] - A[0]))
import numpy as np
a,b=list(map(int,input().split()))
C=list(map(int,input().split()))

def getnear(list,num):
  near=np.abs(np.asarray(list)-num).argmin()
  return list[near]

S=sorted(C)
H=getnear(C,a/2)
r=abs(H+abs(a-S[0]))
l=abs(H+abs(a-S[-1]))
if r>l:
  print(l)
else:
  print(r)
K, N = map(int, input().split())
As = [int(i) for i in input().split()]

nums = [K - As[-1]]
for idx in range(len(As)-1):
    nums.append(As[idx+1] - As[idx])
print(sum(nums) - max(nums))
K,N=map(int,input().split())
A=list(map(int,input().split()))
B=sorted(A)
s=K-B[N-1]
t=B[N-1]-B[N-2]
if s>t:
  m=B[N-1]-B[0]
else:
  m=B[N-2]+K-B[N-1]
print(m)
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]
dis = 0
for i in range(N-2):
  dis += d[i]
if d[N-2] <= de:
  dis += d[N-2]
else:
  dis += de
print(dis)
import numpy as np
k,n = [int(i) for i in input().split()]
a = np.array([int(i) - k if k - int(i) < int(i) else int(i) for i in input().split()])
r = 0
s = 0
for _ in range(1,n):
    m = np.mean(np.abs(s - a))
    r += a[np.argmin(abs(m - a))]
    np.delete(a,np.argmin(abs(m - a)))
print(r + a[0])
inputNumber = input()
K = int(inputNumber.split(" ")[0])
N = int(inputNumber.split(" ")[1])

inputNumber = input()
A=inputNumber.split(" ")
for i in range(len(A)):
    A[i] = int(A[i])

result=0

for i in range(len(A)):
    if i == len(A) - 1:
        if result < (K - A[i]) + A[0]:
            result = (K - A[i]) + A[0]
    else:
        if result < A[i] - A[i-1]:
            result = A[i+1] - A[i]

print(result)
K, N = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
if (A[N-1] - A[0]) > (K+A[0] - A[N-1]):
    A = [x+K if x <= A[N-1]/2 else x for x in A]

print(max(A) - min(A))
K = 20
A = [5,10,15]
A.sort()
distance_list = []
for i,a in enumerate(A):
    try:
        distance = abs(a - A[i+1])
        distance_list.append(distance)
    except IndexError:
        break
distance_list.append(K-max(A))
distance_list.remove(max(distance_list))
ans =sum(distance_list)
print(ans)
K, N = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

maxA = max(A)
m = A[min(((i, abs(maxA / 2 - n)) for i, n in enumerate(A)), key=lambda x: x[1])[0]]
print(max((maxA - m), (m - min(A))))
K,N = map(int, input().split())
*A, = map(int, input().split())
A.append((A[0]+K))
ans = 0
for i in range(len(A)-1):

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

print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
b = sorted(a)
for i in range(len(b)):
    if b[i] > 10:
        b[i] -= 20
b = sorted(b)
if a[-1] - a[0] < b[-1] - b[0]:
    print(a[-1] - a[0])
else:
    print(b[-1] - b[0])
K,N = [int(e) for e in input().split(" ")]

A = [int(e) for e in input().split(" ")]

A.sort(reverse = True)

sum1 = 0
tmp = 0

if A[len(A) - 1] == 0:
    del A[len(A) - 1]

for i in range(len(A) - 1):
    tmp =  A[i] - A[i + 1]
    sum1 = sum1 + tmp
    
print(sum1)
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, lst[-1] - lst[0])
    else:
        dis = max(dis, lst[i] - lst[i - 1])

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

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

A = sorted(a)

minl = 0

for i in range(N-1):
    l = A[i] - A[i+1]

    if l <= 0:
        l = -1 * l
    
    if minl < l:
        minl = l

ans = K - minl

print(ans)
k,n=map(int,input().split())
arr=list(map(int,input().split()))
arr2=[i for i in arr if i!=0]
print(k-(max(arr2)-min(arr2)))
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*2
b[0]=max(a)-min(a)
a.sort()
b[1]=a[-1]-a[-2]
print(min(b))
n,m =map(int, input().split())
l = list(map(int, input().split()))
print(abs(l[0]-l[m-1]))
K, N = map(int, input().split())
li = list(map(int, input().split()))
half = K/2
if li[0] == 0 and li[1] >= half:
    print(li[1])
else:
    re_li = [v-K if v > half else v for v in li]
    dist1 = max(li)-min(li)
    dist2 = max(re_li)-min(re_li)
    print(dist1 if dist1 < dist2 else dist2)
k,n=map(int,input().split())
a=[int(i) for i in input().split()]
if(a[0]!=0):
    ans=0
    for i in range(n-1):
        ans+=a[i+1]-a[i]
    print(ans)
else:
    ans=0
    for i in range(n-2,0,-1):
        ans+=a[i]-a[i-1]
    ans+=k-a[n-1]
    print(ans)
I=lambda:list(map(int,input().split()))
p,n=I()
l=I()
ne=[]
ma=10**9+7
for i in range(n-1):
    ne.append(abs(l[i]-l[i+1]))
ne.append(abs(sum(l)-l[-1]))
g=sum(ne)
for i  in ne:
    ma=min(ma,g-i)
print(ma)
k, n = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
b = sorted(a)
for i in range(len(b)):
    if b[i] >= k/2:
        b[i] -= k
b = sorted(b)
if a[-1] - a[0] < b[-1] - b[0]:
    print(a[-1] - a[0])
else:
    print(b[-1] - b[0])
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
c = min(abs(k-max(a)), min(a))
if c == 0:
   c = k-max(a)
calc = c + abs(a[n-2]-min(a))
print(calc)
k,n = list(map(int, input().split()))
a = list(map(int, input().split()))
sa = 0
for i in range(len(a)):
    if(abs(a[i]-a[i-1]) > sa):
        sa = abs(a[i]-a[i-1])
print(k - sa)