peterverebics/CodeNet_Python_118 huggingface dataset:
n=int(input())
ans=0
ans=n//500*1000+ans
ans=n%1000//5*5+ans
print(ans)
import math
x = int(input())
ans = 0
if math.floor(x/500) >=1 :
    ans += math.floor(x/500)*1000
    x -= math.floor(x/500)*500
if x > 0 and math.floor(x/5) >=1 :
    print("a")
    ans +=  math.floor(x/5)*5
print(ans)
x=int(input())
ans=(x//500)*1000
rem=x%500
ans+=(rem//5)*5
l = int(input())
print(l*2 if l%10==0 else (l-(l%10*6))*2 + (l%10*5))
money = int(input())
if money>500:
  val = money//500
  money=money-val
  val = val + money//5
  print(val)
else:
  val = money//5
  print(val)
x=int(input())
print((x//500)*1000+(x//(500*5))//5*5)
import math

X = int(input())
happy_1000 = X//500
X = math.floor(X%500)
print(X)
happy_5 = X//5
res = happy_1000*1000 + happy_5*5
print(res)
X = int(input())

happy = ((X // 500) * 1000 ) + ((X % 5) * 5)
print(happy)
X = int(input())
fhun = X//500
amari = X % 500
fiv = amari//5
ure = fhun + fiv
X = int(input())

a = int(X/500)
b = (X - 500*a)/5
H = 1000 * a + 5 * b
print(int(H))
X = int(input())
a = X%500
print(a*1000 + 5*((X-a*500)%5))
#500-1000, 5-5

x = int(input())
fiveHundred = x//500
x -= fiveHundred
fives = x//5
print(fiveHundred*1000+fives*5)
coin = int(input())
fivet_yen = 0
five_yen = 0
if coin == 0:
    print(0)
else:
   while(coin > 0):
    if coin >= 500:
        coin = coin - 500
        fivet_yen = fivet_yen + 1000
    else:
        if coin >= 5:
            coin = coin - 5
            five_yen   = five_yen + 5
        else:    
            coin = coin - 1


print(fivet_yen + five_yen)
a = []
a = input().split()
K = int(a[0])
N = int(a[1])
#print('K=', K, 'N=', N)
A = []
A = input().split()
A = [int(i) for i in A]
#print('A=', A)

l = []

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

#print('l=', l)

M = max(l)

#print('M=', M)

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

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

for i in range(1,n):
    dp[i] = abs(a[i]-a[i-1])

print(sum(dp) - max(dp))
K,N=map(int,input().split())
A=list(map(int,input().split()))
A+=[K+A[0]]
B=[A[i+1]-A[i] for i in range(N)]
print(K-max(B))
k, n = map(int, input().split())

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

max_diff = 0
for i in range(1,n):
    if i != 0:
        max_diff = max(abs(An[i] - An[i-1]), max_diff)

max_diff = max(max_diff, k-An[n-1] + An[0])
print(k-max_diff)
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] + array[0]
sum_length += last_length
max_length = max(max_length, last_length)
print(sum_length - max_length)
K, N = map(int, input().split())
house = list(map(int,input().split()))
min = house[N - 1] - house[0]
 
for i in range(N - 1):
  if K - (house[i + 1] - house[i]) < min:
    min = K - (house[i + 1] - house[i])
 
print(min)
k,n = map(int, input().split())
a = list(map(int, input().split()))

mx = k-a[-1] +a[0]

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

dA = []
for i in range(1, N):
    dA.append(A[i] - A[i-1])
    if i == N - 1:
        dA.append(A[0] - (A[i] - K))

dA = sorted(dA)
print(sum(dA[:N-1]))
k,n = list(map(int,input().split(" ")))
A = list(map(int,input().split(" ")))
max_dist = 0
for i in range(len(A)-1):
  if A[i+1]-A[i] > max_dist:
    max_dist = A[i+1]-A[i] 
if A[0]+k-A[-1] > max_dist:
  max_dist = A[0]+k-A[-1]
print(k-max_dist)
k,n = map(int, input().split())
a = list(map(int, input().split()))

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

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

print(min(k-counter,a[n-1]-a[0]))
K,N = map(int, input().split())
As  = list(map(int, input().split()))
Bs = As + [K+As[0]]
diff = [abs(Bs[i+1]-Bs[i]) for i in range(N)]
# print(diff)
mx = 0
print(sum(diff) - max(diff))
k , n = map(int, input().split())
a_list = list(map(int, input().split()))
a_last = a_list[0] + k
a_list.append(a_last)
distance = -1
for i in range(n):
    distance_btw = a_list[i + 1] - a_list[i]
    if distance_btw > distance:
        distance = distance_btw
print(k - distance)
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))

    A.append(K+A[0])
    ans = K

    for i in range(0, N):
        tmp = K - (A[i+1] - A[i])
        if tmp < ans:
            ans = tmp
    print(ans)







    return 0


if __name__ == '__main__':
    main()
k,n=map(int,input().split())
a=[int(i) for i in input().split()]

saitanhiku=a[2]-a[1]
for i in range(n-1):
    if a[i+1]-a[i]>=saitanhiku:
        saitanhiku=a[i+1]-a[i]
if (k-a[n-1])+a[0]>=saitanhiku:
    saitanhiku=k-a[n-1]+a[0]
print(k-saitanhiku)
def main():
    import sys
    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()
K, N = map(int, input().split())
A = list(map(int, input().split()))
dist=0
max_dist=0
max_i=0
for i in range(N):
	a=A[i%N]
	b=A[(i+1)%N]
	tmp=b-a if b>a else K+b-a
	if max_dist<tmp:
		max_dist=max(max_dist,tmp)
		max_i = i
for i in range(N-1):
	i2 = i+max_i+1
	a=A[i2%N]
	b=A[(i2+1)%N]
	tmp=b-a if b>a else K+b-a
	dist+=tmp
print(dist)
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))

a.sort()

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

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

dmax = K - A[N-1] + A[0]
apre = -(K - A[N-1])
for a in A:
  d = a - apre
  if d > dmax:
    dmax = d
  apre = a

print(K - dmax)
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)

_kankaku_max = max(kankaku)
print(K - _kankaku_max)
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(sum(dist)-max(dist))
K, N = map(int, input().split())
A = list(map(int, input().split()))

dis = [(K+A[0]-A[-1])]

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


        
print(K - max(dis))
# %%
K, N = map(int, input().split())
A = list(map(int , input().split(" ")))

# %%
ma = 0
for i in range(N-1):
    ma = max(ma, A[i+1]- A[i])

# %%
print(min(K-ma,A[N-1]-A[0]))

# %%
k,n=map(int,input().split())
poses=list(map(int,input().split()))
print(k-max(k-poses[-1]+poses[0],max(pos1-pos0 for pos1,pos0 in zip(poses[1:],poses))))
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 28 22:05:17 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

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

ans = K - max(B)

print(ans)
k,n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ans = a[-1]-a[0]
for i in range(n-1):
  chk = a[i] + k-a[i+1]
  ans = min(ans,chk)
print(ans)
import numpy as np
N,K = list(map(int, input().split()))
A = list(map(int, input().split()))
A_diff = np.diff(A)
A_diff_sum = np.sum(A_diff)
max_diff = np.max([np.max(A_diff), (N - A[-1] + A[0])])
ans = A_diff_sum - max_diff + (N - A[-1] + A[0])
print(ans)
K, N = map(int, input().split())
A = list(map(int, input().split()))

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

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

dif_max = 0

for i in range(N):
    if i == 0:
        dif = A[0] + (K - A[-1])
    else:
        dif = abs(A[i] - A[i-1])
    if dif > dif_max:
        dif_max = dif

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

ans = 10**9
for i in range(n):
  if i == 0:
    a1 = a[-1]-a[0]
    a2 = a[0]+(k-a[1])
    ans = min(ans,a1,a2)
  else:
    a1 = (k-a[i])+a[i-1]
    ans = min(ans,a1)
print(ans)
K, N = map(int, input().split())

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

A = sorted(a)

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

if maxl <= 0:
    maxl = -1 * maxl


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

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

ans = K - maxl

print(ans)
K, N = map(int, input().split(' '))
A_ls = list(map(int, input().split(' ')))
rst = -1
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]
    if rst == -1:
        rst = l
    else:
        rst = max(rst, l)
print(K - rst)
from sys import stdin

k,n = [int(x) for x in stdin.readline().strip().split()]
a_lst = [int(x) for x in stdin.readline().strip().split()]
diff_lst = [a_lst[i+1] - a_lst[i] for i in range(len(a_lst)-1)] + [k - a_lst[-1] + a_lst[0]]

print(k - max(diff_lst))
K,N = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
A.append(K+A[0])

max_distance = 0
for i in range(1,len(A)):
  max_distance = max(max_distance,A[i] - A[i-1])

ans = K - max_distance

print(ans)
import numpy as np


def solve():
    dists = np.zeros(n, dtype=int)
    for i in range(n):
        dists[i] = abs(a[(i+1)%n]-a[i%n])
        dists[i] = min(dists[i], k-dists[i])
    ans = k - np.max(dists)
    print(ans)
    return




k, n = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
solve()
k,n = map(int,input().split())
a =list(map(int,input().split()))
kyori = [0]*n
# print(a)
for i in range(1,n):
    # print(i)
    kyori[i]=a[i]-a[i-1]
kyori[0]=a[0]+(k-a[n-1])
print(k-max(kyori))
K,N=map(int,input().split())
A = [int(v) for v in input().split()]
D = []
for i in range(len(A)-1):
  D.append(A[i+1]-A[i])
D.append(A[0]+(K-A[-1]))
print(K-max(D))
K,N=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
dist=[A[0]+K-A[-1]]+[A[i]-A[i-1] for i in range(1,N)]
print(K-max(dist))
K,N = map(int,input().split())
L = [i for i in map(int,input().split())]
L.append(L[0]+K)
dist = []
for i in range(N):
    dist.append(abs(L[i+1]-L[i]))
print(sum(dist)-max(dist))
k,n = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
from collections import deque
d = deque()
d.append(k-l[n-1]+l[0])
for i in range(n-1):
  d.append(l[i+1]-l[i])
print(k-max(d))
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 28 22:05:17 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

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

ans = K - max(B)

print(ans)
#!/usr/bin/env python3
def main():
    K, N = map(int, input().split())
    A = [int(x) for x in input().split()]

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


if __name__ == '__main__':
    main()
kn = input().split()
a = input().split()

al = [int(i) for i in a]
k = int(kn[0])
n = kn[1]

ml = k - abs(al[len(al)-1] - al[0])


for i in range(len(al)-1):
	ml = max(ml,abs(al[i]-(al[i+1])))


print(k-ml)
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]+A[0]>m:
    print(A[-1]-A[0])
else:
    print(K-m)
K, N = map(int, input().split())
A = list(map(int, input().split()))

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

print(K - max(diff))
K,N = map(int, input().split())
house = [int(i) for i in input().split()]

answer = [0]

for index, i in enumerate(house):
  if i == house[-1]:
    X = (K - i) + house[0]
    answer.append(X)
  else:
    X = house[index + 1] - i
    answer.append(X)
print(K - max(answer))
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
print(min((k - a[max]) + a[max - 1], a[n - 1] - a[0]))
K, N = map(int, input().split())
A = [int(x) for x in input().split()]
B = [A[i] - A[i-1] for i in range(1, N)]
B.append(K - A[-1] + A[0])
ans = sum(B) - max(B)
print(ans)
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(k-M)
K, N = map(int, input().split())
lis_A = list(map(int, input().split()))
dif = [0]*len(lis_A)
dif[0] = K-lis_A[-1] + lis_A[0]
for i in range(1,len(lis_A)):
    dif[i] = lis_A[i]-lis_A[i-1]
ans = sum(dif)-max(dif)
print(ans)
#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc160/tasks/abc160_c

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

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

print(K - max(dist))
k,n = map(int,input().split())
l= list(map(int,input().split()))
li = []

for i in range(n):
  if i != n-1:
    li.append(l[n-i-1]-l[n-i-2])
  else:
    li.append(k-l[n-1]+l[0])

li.sort(reverse=True)
print(k-li[0])
K,N=map(int,input().split())
n=list(map(int,input().split()))
if n[N-1]-n[0] < (K-n[1])+n[0]:
  dist=n[N-1]-n[0]
else:
  dist=(K-n[1])+n[0]
for i in range(1,N-1):
  if dist > (K-n[i-1])+n[i]:
    dist= (K-n[i-1])+n[i]
  if dist > (K-n[i+1])+(n[i]-0):
    dist= (K-n[i+1])+(n[i]-0)
print(dist)
K,N = list(map(int, input().split()))

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

max = 0

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

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

A.insert(0, A[N-1] - K)
diffs = []
for i in range(1, len(A)):
    diff = A[i] - A[i-1]
    diffs.append(diff)

print(K - max(diffs))
K, N = map(int, input().split())
A_list = list(map(int, input().split()))
max1 = -1
for i in range(N):
    if i!=N-1:
        dis = abs(A_list[i]-A_list[i+1])
    else:
        if A_list[0]!=0:
            dis = abs(K-A_list[-1])+A_list[0]
        else:
            dis = abs(K-A_list[-1])
    if dis > max1:
        max1 = dis
print(K-max1)
k, n = map(int, input().split())
alst = list(map(int, input().split()))
alst.append(alst[0] + k)
minus = 0
for i in range(n):
  minus = max(minus, alst[i + 1] - alst[i])
print(k - minus)
k, n = map(int, input().split())
li = list(map(int, input().split()))
li_2 = []
for i in range(1,len(li)):
  li_2.append(li[i]-li[i-1])
print(min(k-max(li_2), li[-1]-li[0]))
k, n = list(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[-1]+a[0])

print(k - max(b))
k , n = map(int, input().split())
a = list(map(int, input().split()))
d = []

for i in range(1, n):
    y = a[i]
    x = a[i-1]
    d.append(y-x)
d.append(k - (a[n-1] - a[0]))
d.sort(reverse = True)
print(k-d[0])
import heapq

k, n = map(int, input().split())
a = [int(x) for x in input().split()]
qa = []
heapq.heapify(qa)

for x in range(len(a) - 1):
    heapq.heappush(qa, abs(a[x] - a[x + 1]))
heapq.heappush(qa, k - a[-1] + a[0])

dis = 0
for x in range(len(qa) - 1):
    dis += heapq.heappop(qa)

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

KUKAN = [0] * N

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

print(str(sum(KUKAN) - max(KUKAN)))
import numpy as np
k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0] + k)
a = np.array(A)
 
max_dist = np.max(np.diff(A))
print(k - max_dist)
k,n=map(int,input().split())
A=list(map(int,input().split()))
ans=-1
for i in range(n-1):
  ans=max(ans,A[i+1]-A[i])
print(k- max(ans,k-(A[n-1]-A[0])))
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import numpy as np

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

    va = np.array(A)
    vb = np.array(A[1:] + [A[0] + K])

    vv = vb - va

    print(K - max(vv))


if __name__ == '__main__':
    main()
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
distance = [abs(A[i] - A[i-1]) for i in range(N)]
distance[0] = abs(distance[0]-K)
print(sum(distance) - max(distance))
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

dist = [x[i] - x[i - 1] for i in range(1, n)]
dist.append(x[0] + k - x[n - 1])

# print(len(dist))
ans = k - max(dist)

print(ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))
d = a[0]+k-a[n-1]
for i in range(n-1):
    t = a[i+1] - a[i]
    d = max(t, d)
print(k-d)
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 30 20:19:25 2020

@author: black
"""


K, N = map(int, input().split())
li = list(map(int, input().split()))
li.append(K + li[0])
sub = [li[i+1]-li[i] for i in range(N)]
MAX = max(sub)
total = sum(sub)
print(total - MAX)
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(K-A[-1]+A[0])
print(sum(d)-max(d))
k,n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
dmax = 0
for i in range(1,n):
  if a[i]-a[i-1]>dmax:
    dmax = a[i]-a[i-1]
if k-a[-1]+a[0] > dmax:
  dmax = k-a[-1]+a[0]
print(k-dmax)
K, N = map(int,input().split())
a = list(map(int,input().split()))
x = [a[0]-a[-1]+K]
for i in range(1,N):
    x.append(a[i]-a[i-1])
print(K-max(x))
import sys


input = sys.stdin.readline


def main():
  k, n = map(int, input().rstrip().split())
  a = [int(_) for _ in input().rstrip().split()]
  
  dist = [(a[i] - a[i - 1]) % k for i in range(n)]
  
  print(k - max(dist))


if __name__ == "__main__":
  main()
k, n = map(int, input().split())
List = list(map(int, input().split()))

L2 = []

for i in range(n-1):
    L2.append(List[i+1] - List[i])
L2.append(k - List[-1] + List[0])
print(k - max(L2))
import sys
read = sys.stdin.buffer.read
k, n, *A = read().split()
far = int(k) + int(A[0]) - int(A[-1])
for x, y in zip(A[1:], A):
    if far < int(x) - int(y):
        far = int(x) - int(y)
print(int(k)-far)
k,n=map(int,input().split())
a=list(map(int,input().split()))
lenmax=k-a[n-1]+a[0]
for i in range(n-1):
    if(lenmax<-1*(a[i]-a[i+1])):
        lenmax=-1*(a[i]-a[i+1])
print(k-lenmax)
K, N = map(int, input().split())
Alst = list(map(int, input().split()))
zero = Alst[0] + K
M = 0
now = Alst[0]
for i in Alst:
    dis = i - now
    if dis > M:
        M = dis
    now = i

last = zero - now
if last > M:
    M = last

print(K - M)
K, N = map(int,input().split())
A = sorted(list(map(int,input().split())))
dis = []
for i in range(N-1):
  dis.append(abs(A[i] - A[i+1]))
dis.append(K - A[-1] + A[0])

print(K-max(dis))
k,n=map(int, input().split())
a=list(map(int, input().split()))
ans = a[0]+k-a[n-1]
for start in range(1,n):
    ans = max(ans,a[start]-a[start-1])
    
print(k-ans)
import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)



def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())

K,N=acinput()

A=acinput()
A.append(A[0]+K)
B=np.array(A)+K
#A.extend(B)
a=np.array(A)

print(K-np.max(a[1:]-a[:-1]))
line1 = input().split()
k = int(line1[0])
n = int(line1[1])
line2 = input().split()
a = [int(i) for i in line2]
dist_list = [a[i] - a[i - 1] for i in range(1, len(a))]
dist_list.append(k-a[-1] + a[0])
max_dist = max(dist_list)
index = len(dist_list) - dist_list[::-1].index(max_dist) - 1
start_index = (index + 1) % len(dist_list)
ans = 0
for i in range(start_index, start_index + n - 1):
  ans += dist_list[i % len(dist_list)]
print(ans)
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 k - (arr[i] - arr[0]) > ans:
            ans = k - (arr[i] - arr[0])
print(k - ans)
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

cur_dist = k + a[0] - a[-1]
if cur_dist > max_dist:
	max_dist = cur_dist

print(k - max_dist)
import sys

K,N = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))
# K = 20
# N = 3
# A =[0, 5, 15]
# A =[5, 10, 15]

distance = []
# 家間距離
for i,a in enumerate(A):
    if i == len(A) - 1:
        distance.append(K - A[i] + A[0])
    else:
        distance.append(A[i+1] - A[i])

# print(distance)
print(sum(distance) - max(distance))
K, N = map(int, input().split())
A =  list(map(int, input().split()))

mini = K
right = left = K
for i in range(N):
	l_begin = i-1
	r_begin = i+1
	if i == 0:
		l_begin = N-1
		right = A[l_begin] - A[i]
		left = K - (A[r_begin]-A[i])
	elif i == N-1:
		r_begin = 0
		right = K - (A[i]-A[l_begin])
		left = A[i] - A[r_begin]
	else:
		right = K - (A[i]-A[l_begin])
		left = K - (A[r_begin]-A[i])
	local = min(right, left)
	if local < mini:
		mini = local
		

print(mini)
k,n = map(int, input().split())
lst = list(map(int, input().split()))

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

K = a[0]
N = a[1]

A_nplus1 = K + A[0]
A.append(A_nplus1)

max = -1
max_id = -1

for i in range(0,N):
    if A[i+1]-A[i] > max:
        max = A[i+1] - A[i]
print(K-max)
K, N = map(int, input().split())
A = list(map(int, input().split()))
m = K + A[0] - A[N-1]
for i in range(N-1):
  d = A[i+1] - A[i]
  m = max([d, m])
print(K-m)
K, N = map(int, input().split())
A = list(map(int, input().split()))

if N == 2:
    print(min(A[1] - A[0], K + A[0] - A[1]))

max_distance = 0

if N > 2:
    for i in range(N - 1):
        s = A[i + 1] - A[i]
        if s > max_distance:
            max_distance = s

    print(min(K - max_distance, A[N - 1] - A[0]))
K, N = map(int, input().split())
A = list(map(int, input().split()))
long = A[0] + K - A[N-1]

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

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

A.append(K + A[0])

distance = 0
for x in range(len(A) - 1):
    distance = max(distance, A[x + 1] - A[x])

print(K - distance)
k, n = map(int, input().split())
a = list(map(int, input().split()))
A = []
for i in range(n-1):
    A.append(a[i+1]-a[i])
A.append(k-a[n-1]+a[0])
print(k-max(A))
def main():
    length, num = map(int, input().split())
    a = list(map(int, input().split()))
    max_dis = 0
    first_h = a[0]
    prev_h = a[0]
    for h in a[1:]:
        dis = h - prev_h
        max_dis = max(max_dis, dis)
        prev_h = h

    dis = (length - prev_h) + first_h
    max_dis = max(max_dis, dis)
    print(length - max_dis)


if __name__ == "__main__":
    main()