peterverebics/CodeNet_Python_118 huggingface dataset:
K, N = [int(x) for x in input().split()]
A_list = [int(x) for x in input().split()]

if N <= 1:
  print(0)
 
max_distance = 0
max_distance_idx = 0
for i in range(0, N-1):
  dist = A_list[i+1]-A_list[i]
  if max_distance < dist:
    max_distance = dist
    max_distance_idx = i
 
if max_distance <= (K-A_list[N-1]) + (A_list[0]):
  print(max_distance)
else:
  print((A_list[max_distance_idx]) + (K-A_list[max_distance_idx+1]))
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = k - a[n-1]
l = []
l.append(k - a[n-1])
for i in range(1, n):
    dis = a[i] - a[i - 1]
    l.append(dis)
    
print(sum(l) - max(l))
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)
print(N-d)
K, N = map(int, input().split())
A = list(map(int, input().split()))

dist = [0] * (N-1)

for i in range(N-1):
  dist[i] = A[i+1] - A[i]
    
print(K - max(dist))
k,n=map(int,input().split())
a=[int(i) for i in input().split()]
s=0
for i in a:
    if i!=0:
        s+=1
print(sum(a)//s)
K_N_nyuryoku = input().split()
K = K_N_nyuryoku[0]
N = K_N_nyuryoku[1]

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

print(A)

kyori = 0
for i in A:
    kyori += i - kyori

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

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

b.sort()

print(k - b[0] - b[1])
#!/usr/bin/env python3

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

if a[0] != 0:
    print((a[n//2]-a[0])+(a[n-1]-a[n//2]))
else:
    print((a[n//2]-a[0])+(k-a[n-1]))
K,N = map(int,input().split())
house = list(map(int,input().split()))


first = 0
for i in house:
    if i > 0:
        first += i
        break
print(house[-1] - first)
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append(2*np.pi*(K-A[i]+A[0])/K)
    else:
        theta.append(2*np.pi*(A[i+1]-A[i])/K)

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=(s*K)/(2*np.pi)
print(int(l_min))
K, N = map(int, input().split())
A = list(map(int, input().split()))

A.insert(0, 0)
A.append(K)

mixi = 0
sum_len = 0


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

if space == 0 or space == len(A) - 1:
    result = sum_len - (mixi + (K - A[-2]))
else:
    result = sum_len - mixi

print(result)
import numpy as np

K,N = map(int, input().split())
An_list = np.array(input().split(), dtype=int)

An_delta_list = np.array([], dtype=int)

for i in range(len(An_list)-1):
    An_delta_list = np.append(An_delta_list, An_list[i+1] -An_list[i])
An_delta_list = np.append(An_delta_list, K - An_list[-1])

An_delta_max = np.max(An_delta_list)
result = np.sum(An_delta_list) - An_delta_max

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

inv_ans = 0

for i in range(N-1):
  if inv_ans < A[i+1] - A[i]:
    inv_ans = A[i+1] - A[i]

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

l=[]
a.append(k)
for i in range(0,n):
  abs_= abs(a[i]-a[i+1])
  l.append(abs_)
l.pop(l.index(max(l)))
print(sum(l))
k,n = map(int,input().split())
li = list(map(int,input().split()))

li_diff = []
for i in range(len(li) - 1):
    li_diff.append(li[i + 1] - li[i])
li_diff.append(k - li[len(li) - 1])

ans = sum(li_diff) - max(li_diff)
print(ans)
import numpy as np

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

diff_A = []
for i in range(N-1):
    diff_A.append(A[i+1] - A[i])
diff_max = max(diff_A)
print(K - diff_max)
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)
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])
print(diff)

res = sum(diff) - max(diff)
print(res)
K,N = (int(x) for x in input().split())
al = [int(x) for x in input().split()]
d = [al[x+1]-al[x] for x in range(0, N-1)]
d.append(al[-1]-al[0])
d.remove(max(d))
print(sum(d))
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/2:
    d.append((k / 2 - 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)
K,N = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

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

for n in A:
  if n != 0 and n > K/2:
    Ap.append(n-K)
  else:
    Ap.append(n)


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


Ap.sort()
lp = 0
for j in range(N-1):
  lp += abs(Ap[i]-Ap[i+1])


if l >= lp:
  result = lp
elif l < lp:
  result = l 

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

hutuu = max(a) - min(a)

a_inv = []
for i in a:
    if i > (k//2):
        a_inv.append(i - k)
    else:
        a_inv.append(i)
inv = max(a_inv) - min(a_inv)

if hutuu >= inv:
    print(inv)
else:
    print(hutuu)
K,N = map(int,input().split())
house = list(map(int,input().split()))
inter = []
for i in range(N-1):
  inter.append(house[i+1]-house[i])
inter.append((20-house[N-1])+house[0])
print(K-max(inter))
k,n = map(int, input().split())
A = list(map(int, input().split()))
B = []
for i in range(n-1):
    B.append(A[i+1] - A[i])
B.append(A[0] + k - A[n-1])
print(max(B))
K, N = map(int,input().split())
A = list(map(int, input().split()))

print(min(K - A[-1] + A[-2] - min(A),max(A)-min(A)))
def calc(K, N, A):
    B = [A[i + 1] - A[i] for i in range(N - 1)] + [A[0] + K - A[N - 1]]
    print(B)
    return K - max(B)


(K, N) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
print(calc(K, N, A))
k,n = map(int, input().split())
a = list(map(int, input().split()))

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

print(k-max(b))
k, n= map(int,input().split())
a = list(map(int,input().split()))
for i in range(1,n):
  a[i] -= a[i-1]
a[0] = 0
a += [k-sum(a)]
print(k-max(a))
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
max_diff = 0
for idx in range(len(a)-1):
  diff = a[idx+1] - a[idx]
  if max_diff < diff:
    max_diff = diff

ans = k - max_diff
print(ans)
k, n = map(float, input().split())
m = list(map(float, input().split()))
first = m[0] if not m[0] == 0 else m[1]
last = m[-1]
half_k = float(k/2)

if last < half_k or first > half_k:
    print(int(last - first))
else:
    left_to_right = int(k + first - last)
    right_to_left = int(last - first)
    if left_to_right > right_to_left:
        print(right_to_left)
    else:
        print(left_to_right)
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

sub = A[1] - A[0]
max = N * sub
sub_2 = K - max

all = sub * (N - 1)

if K < all:
    print(all % K)
elif sub_2 >= sub:
    print(sub * (N - 1))
else:
    print(sub * (N - 2) + sub_2)
"""Boot-camp-for-Beginners_Easy012_C_Next-Prime_30-August-2020.py"""
import numpy as np

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

theta = []
for i in range(len(A)):
    if i == len(A)-1:
        theta.append(2*np.pi*(K-A[i]+A[0])/K)
    else:
        theta.append(2*np.pi*(A[i+1]-A[i])/K)

#print("sum",sum(theta))

theta.remove(max(theta))
#print(theta)
s=0
for j in range(len(theta)):
    s+=theta[j]
#print(s)
l_min=(s*K)/(2*np.pi)
print(int(l_min))
K, N = map(int, input().split())
a = list(input().split())

a = [int(i) for i in a]

diff = list()
diff.insert(0,K-a[N-1]) # seq 1
for i in range(2,N+1):  # seq 2 to N-1
  diff.insert(i-1,abs(a[i-1]-a[i-2]))

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

count = []

for i in range(n):
  if (a[i-1]-a[i])<0:
    count.append(a[i-1]-a[i]+20)
  else:
    count.append(a[i-1]-a[i])
print(min(count))
k,n=map(int,input().split())
A=list(map(int,input().split()))

ans=max(A)
for i in range(n):
  A[i]=k-A[i]
ans-=min(A)
print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
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)
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

k,n = miinps()
a = lmiinps()

l = -1

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

if a[0] == 0:
    l = max(l,k-a[n-1])
else:
    l = max(l,a[n-1]-a[0])

print(k-l)
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

front = A[N-1] - A[0]

rear = A[N-2] - (A[N-1]-K)

if front < rear:
    print(front)
else:
    print(rear)
K, N = map(int, input().split())
A = list(map(int, input().split()))
B=[]
for i in range(N-1):
    B.append(A[i+1]-A[i])
B.append(A[N-1]-A[0])
print(K-max(B))
K, N = map(int,input().split())
A = list(map(int,input().split()))

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

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

# 時計回り
if a[0] + a[n-1] >= k:
    print(a[n-1] - a[0])

# 反時計回り
else:
    max_n = 0
    for i in range(n-1):
        diff = abs(a[i] - a[i+1])
        max_n = max(diff, max_n)
    dif = abs(a[0] - a[n-1])
    max_n = max(diff, max_n)
    print(k - max_n)
k,n=map(int,input().split())
x=list(map(int,input().split()))
xxx=[]
for i in x:
    if i==k:
        xxx.append(0)
    else:
        xxx.append(i)
xx=list(set(xxx))
y=[]
for i in x:
    if i*2>=k:
        y.append(i-k)
    elif i==k:
        y.append(0)
    else:
        y.append(i)
yy=list(set(y))
print(min(max(xx)-min(xx),max(yy)-min(yy)))
#C traveling salesman around lake

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

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

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

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

M = 0
for i in range(N):
    if M <= A[i] - A[i-1] :
        M = A[i] - A[i-1]
ans = K - M
print(ans)
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    K, N = map(int, readline().split())
    A = list(map(int, readline().split()))
    d = [A[i]-A[i-1] for i in range(1,N)]
    d.append(K - max(A) + min(A))
    ans = min(K-max(d), max(d))
    print('d:',d)
    print(ans)
    return

if __name__ == '__main__':
    main()
k, n = map(int, input().split())
a = list(map(int, input().split()))
dmin = k - (a[n - 1] - a[0])
for i in range(n - 1):
    if (a[i + 1] - a[i]) < dmin:
        dmin = a[i + 1] - a[i]
print(k - dmin)
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 string
#first_input = input().split(" ")

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

A_list = second_input

A_list.append(K)

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

print(sum(dist_list)-max(dist_list))
K, N = map(int, input().split())
Aj = list(map(int, input().split()))
Aj.sort()
Aj.append(K)

Rj = []
for i in range(len(Aj)-1):
	Rj.append(Aj[i+1]-Aj[i])
Rj.sort()

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

diff = 0
total = 0
for i in range(n-1):
    diff = max(abs(a[i] - a[i+1]), diff)
    total += abs(a[i] - a[i+1])
diff = max(abs(a[n-1] - k), diff)
total += abs(a[n-1] - k) - diff
print(total)
k, n = map(int, input().split())
an = list(map(int, input().split()))
bn = list(map(lambda x: abs(x-k) if x>k/2 else x, an))
print(min((an[-1]-an[0]), (max(bn)+bn[-1])))
k,n=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
if l[0]==0:
    l=l[1:]
t=l[0]
p=0
for val in l:
    p+=(val-t)
    t=val

l=l[::-1]
t=l[0]
r=0
for val in l:
    r+=(t-val)
    t=val
print(min(r,p))
K,N=map(int,input().split())
mtrx=list(map(int,input().split()))
mtrx.append(K)
length = []
for i in range(len(mtrx)-1):
    length.append(mtrx[i+1]-mtrx[i])
print(K-max(length))
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

a = 0
b = A[-1] - A[0]
c = K - A[-1]
for i in range(N - 1):
    if A[0] == 0:
        a = A[i + 1] - A[i]
        c = max(a, c)
        ans = K - c
    else:
        a = A[i + 1] - A[i]
        b = max(a, b)
        ans = K - b
print(ans)
K,N =map(int,input().split())
list_A =input().strip().split()
list_B=[]
list_B.append(int(list_A[0])+(K-int(list_A[-1])))
for i in range(N-1):
    list_B.append(int(list_A[i+1])-int(list_A[i]))
print(max(list_B))
K,N = map(int,input().split())
A = list(map(int,input().split()))
sub = [abs(A[i] - A[i+1]) for i in range(N-1)]
sub.append(K-A[N-1])
print(K-max(sub))
a,b = map(int,input().split())
l = list(map(int,input().split()))
if l[0] != 0:
  print(l[-1] - l[0])
else:
  print(min(a - l[-1] + l[1],l[-1]))
k, n = map(int, input().split())
home_positions = list(map(int, input().split()))


def max_distance_homes(homes):
    max_distance = 0
    for i in range(len(homes) - 1):
        distance = homes[i + 1] - homes[i]
        if max_distance < distance:
            max_distance = distance
    distance = max(homes[-1] - homes[0], k + homes[0] - homes[-1])
    if max_distance < distance:
        max_distance = distance
    return max_distance
# 1行目の入力を空白で分割された数値として扱う
K, N = map(int, input().split())
# 2行目の入力を数値のリストとして扱う
A = list(map(int, input().split()))

# 答えの準備
ans = 0
max = 0

# 距離の取得
for i in range(0, N -1):
    d = A[i+1] - A[i]
    ans += d
    if d > max:
        max = d

# 一周した最後
dl = K - A[N - 1]
if dl < max:
    ans = ans - max + dl

# 出力
print(ans)
x = input()
num = int(x.split()[1])
perimeter = int(x.split()[0])
houses = [int(i) for i in input().split()]

maxDis = perimeter - houses[-1] + houses[0]
total = maxDis
for i in range(1, len(houses)):
    maxDis = max(maxDis, houses[i]-houses[i-1])
    total += houses[i]-houses[i-1]
print(total)
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
length = []
for i in range(len(A) - 1):
  length.append(A[i + 1] - A[i])
length.append(K - A[-1])
print(sum(length) - max(length))
k, n = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()

ans = 0
if a[0] == 0 and len(a) == 3:
    ans += a[1]
    ans += k - a[-1]

elif a[0] == 0 and len(a) > 3:
    ans += a[1]
    ans += k - a[-1]
    for i in range(2,len(a)):
        ans += a[i] - a[i-1]
else:
    ans += max(a) - min(a)

print(ans)
k, n = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
B = [0]*n
if n == 2:
    ans = A[1] - A[0]
elif n >= 3:
    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])
#C

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

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

d = []
#d[]は各家の距離
d.append(a[0])
for i in range(n-1):
    d.append(a[i+1] - a[i])

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

# print(max(d))

# print(sum(d))

print(k - max(d))
K, N = input().split()
A = list(map(int, input().split()))
x = max(A) - min(A)
print(x)
N,K = map(int,input().split())
A = list(map(int,input().split()))
AbsA = []
for i in range(0,K):
    if i<(K-1):
        AbsA = AbsA + [abs(A[i+1]-A[i])]
    else:
        AbsA = AbsA + [abs(A[0] -A[i])]
maxA = max(AbsA)
print(N-maxA)
k,n = map(int,input().split())
a = list(map(int,input().split()))
min = 10000000
for i in range(n-1):
    if min > a[i+1]-a[i]:
        min = a[i+1]-a[i]
if min > k-a[n-1]+a[0]:
    min = k-a[n-1]+a[0]
print(k-min)
K,N=map(int,input().split())
A = list(map(int,input().split()))
ans1=A[N-1]-A[0]
ans2=K-A[N-1]+A[N-2]
if ans1 <= ans2:
    print(ans1)
else:
    print(ans2)
k,n = map(int, input().split())

a = [ int (x) for x in input().split() ]

kyori_sa = []

print(len(a)-1)


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

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

if 0 in a and n >= 4 :
  a.sort()
  print((k - max(a)) + a[1])
else :
  print(max(a) - min(a))
a = list(map(int,input().split()))
b = list(map(int,input().split()))

print(max(b) - min(b))
k,m = map(int,input().split())
a = input().split()
a =[int(s) for s in a]
a.sort(reverse=True)
a.insert(0,k)

dis = []

for i in range(m):
  part = int(a[i]) - int(a[i+1])
  dis.append(part)
  del part

dis.append(int(a[m-1]))
dis.sort()

print(sum(dis[:m-1]))
# 160
K,N=map(int,input().split())
A=[int(i) for i in input().split()]

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

dist_right = max(A)-min(A)
dist_left = X-dist_right

print(min(dist_left, dist_right))
K, N = map(int, input().split())
 
A_list = list(map(int, input().split()))

distance = []
diff = 0

for i in range(0, N):
    
    if i == N-1:
        diff = K + A_list[i]
        distance.append(diff)
        
    else:
        diff = A_list[i+1] - A_list[i]
        distance.append(diff)
        

print(sum(distance) - max(distance))
K,N=list(map(int,input().split(' ')))
A=list(map(int,input().split(' ')))
k=0
Anew=[]
for i in range(1,N):
    Anew.append(abs(A[i]-A[i-1]))
print(K-max(Anew))
K,N=map(int,input().split())
a=list(map(int,input().split()))
if 0 not in a:
  a_=[]
  for i in range(N-1,-1,-1):
    a_.append(-a[i])
  a=a_+a
  ans=K
  for i in range(N+1):
    ans=min(ans,a[i+N-1]-a[i])
  print(ans)
else:
  a_=[]
  for i in range(N-1,0,-1):
    a_.append(-a[i])
  a=a_+a
  ans=K
  for i in range(N):
    ans=min(ans,a[i+N-1]-a[i])
  print(ans)
t,n=map(int,input().split())
x=list(map(int,input().split()))
x.insert(0,x[-1]-t)
dis=[x[i+1]-x[i] for i in range(n)]
dis1=dis[:n-1]
dis2=dis[1:]
print(min(sum(dis1),sum(dis2)))
k,n=map(int,input().split())
a=list(map(int,input().split()))
p=[]
for i in range(n-1):
    p.append(a[i+1]-a[i])
if a[0]==0:p.append(k-a[n-1])
else:p.append(a[n-1]-a[0])
print(k-max(p))
k,n = map(int, input().split())
l = [int(x) for x in input().split()]
ans_1 = l[n-1]-l[0]
ans_2 = (k-l[n-1])+l[0]
if 0 in l:
    del l[0]
    ans = k-l[n-2]+l[0]
    print(ans)
elif ans_1*2>k:
    print(ans_2)
else:
    print(ans_1)
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.append(A[0]+K)
for i in range(N-1):
    l=0
    l=max(l,A[i+1]-A[i])

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

kyori = []
for i in range(0,N-1):
	#print(i,i+1)
    kyori.append(A[i+1]-A[i])
kyori.append(A[0]+K)
print(sum(kyori)-max(kyori))
K,N=map(int,input().split())
a_list=list(map(int,input().split()))
b_list=[]
for i in range(N-1):
    X=a_list[i+1]-a_list[i]
    b_list.append(abs(X))
b_list.append(K-a_list[-1]+a_list[0])
print(b_list)
print(K-max(b_list))
K,N=input().split()
A=input().split()
n=0
m=1
S=list()
for i in range(int(N)-1):
  S.append(int(A[n+1])-int(A[n]))
  n=n+1
S.append(int(A[0])+(int(K)-int(A[-1])))

ans=int(K)-int(max(S))
print(ans)
print(S)
K,N=list(map(int,input().split()))
A=list(map(int,input().split()))
ans=0
t=[]
s=K/2
for i in range(N):
    t.append(A[i]-s)
ans=K-max(t)*2
ans1=max(A)-min(A)
if ans>ans1:
    print(int(ans1))
else:
    print(int(ans))
K,N = map(int,input().split())
A = list(map(int,input().split()))

diff = []


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


DIFF = sorted(diff)
print(DIFF)

del DIFF[-1]

print(sum(DIFF))
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]:
	print(A[N-1]-A[0])
else:
	print(K-memo)
k, n = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
kyoris = []
for i in range(len(a)-1):
    kyoris.append(abs(a[i]-a[i+1]))
lastkyori = (k - a[-1]) + a[0]
kyoris.append(lastkyori)
print(max(kyoris))
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

mini, maxi = min(A), max(A)
A_ = []
for a in A:
    if a != maxi:
        A_.append(a + K)
    else:
        A_.append(a)

mini2, maxi2 = min(A_), max(A_)

ans = min(maxi - mini, maxi2 - mini2)

print(ans)
N = [int(i) for i in input().split()]
M = [int(i) for i in input().split()]

a = 0
for i in range(N[1] - 1):
    if M[i+1] - M[i] >= N[0]/2:
        a = a + abs(M[i + 1] - N[0])
    else:
        a = a + M[i + 1] - M[i]

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

print(max(A)-min(A))
# coding: utf-8

K, N = map(int, input().split())
A = list(map(int, input().split()))
# print(K, N)
# print(A)
ans = A[0]
for i in range(N - 1):
    ans = abs(ans - A[i + 1])
print(ans)
K, N = map(int, input().split())
houselist = list(map(int, input().split()))
max=0
for i in range(1,N-1):
  if houselist[i]-houselist[i-1]>max:
    max=houselist[i]-houselist[i-1]
if K+houselist[0]-houselist[N-1]>max:
  max=K+houselist[0]-houselist[N-1]
print(K-max)
e = list(map(int,input().split()))
K = e[0]
N = e[1]
a = list(map(int,input().split()))
c = [a[i+1] - a[i] for i in range(N-1)]
f = abs(a[N-1] - a[0])
c.append(f)
d = max(c)
print(K - d)
K, N = map(int, input().split())
A = list(map(int, input().split())) 
d = []
interval = 0
for i in range(N):
  d.append(A[i] - interval)
  interval += A[i]
d.append(K-interval+A[0])
print(K - max(d))
K, N = input().split()
A = list(map(int, input().split()))
if 0 in A:
   A.remove(0)
x = max(A) - min(A)
print(x)
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
m = 0
for i in range(1, len(A)):
    if A[i]-A[i-1]>m:
        m=A[i]-A[i-1]
if K-A[-1]>m:
    print(A[-1])
else:
    print(K-m)
K,N = map(int,input().split(" "))
A = list(map(int,input().split(" ")))
A.sort()
distance_list = []
for i in range(N-1):
    distance = A[i+1] - A[i]
    distance_list.append(distance)
distance_list.append(K-max(A))
distance_list.remove(max(distance_list))
ans = sum(distance_list)
print(ans)