Problem:
 Score : 300 points    Problem Statement There is a circular pond with a perimeter of K meters, and N houses around them.  The i -th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.  When traveling between these houses, you can only go around the pond.  Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.      Constraints  2 \leq K \leq 10^6  2 \leq N \leq 2 \times 10^5  0 \leq A_1 < ... < A_N < K  All values in input are integers.         Input Input is given from Standard Input in the following format:  K  N  A_1  A_2  ...  A_N       Output Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.        Sample Input 1 20 3
5 10 15      Sample Output 1 10  If you start at the 1 -st house and go to the 2 -nd and 3 -rd houses in this order, the total distance traveled will be 10 .       Sample Input 2 20 3
0 5 15      Sample Output 2 10  If you start at the 2 -nd house and go to the 1 -st and 3 -rd houses in this order, the total distance traveled will be 10 .

Solution (Python):
k, n = map(int, input().split())
a_list = list(map(int, input().split()))
d_list = [a_list[0] + k - a_list[n - 1]]
for i in range(0, n - 1):
  d_list.append(a_list[i + 1] - a_list[i])
ans = sum(d_list) - max(d_list)
print(ans)

Solution (Python):
k,n=map(int,input().split())
a = list(map(int,input().split()))
m = k-a[n-1]+a[0]
for i in range(n-1):
	m = max(m,a[i+1]-a[i])
x = k-m
print(x)

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
b = [-1]*n
for i in range(n-1):
    b[i] = a[i+1] - a[i]
b[-1] = k-sum(b[:-1])
print(k-max(b))

Solution (Python):
K, N = map(int,input().split())
A = list(map(int,input().split()))
Longest = A[0] + (K - A[-1])
for i in range(N-1):
    Longest = max(Longest, A[i+1] - A[i])
print(K-Longest)

Solution (Python):
k, n = input().split()
house_list = list(map(int, input().split()))

distances = []
for i in range(int(n)-1):
    distances.append(house_list[i+1] - house_list[i])
distances.append(int(k) - house_list[int(n)-1] + house_list[0])
print(int(k) - max(distances))

Solution (Python):

k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = -1*float('inf')
for i in range(n-1):
  
  ans = max(ans, a[i+1]-a[i])
  
ans = max(k+a[0]-a[n-1], ans)


print(k-ans)

Solution (Python):
k,n=map(int,input().split())
*l,=map(int,input().split())
d=0
for i in range(n-1):
  d = max([d,l[i+1]-l[i]])
d=max([d,l[0]+k-l[-1]])
print(k-d)

Solution (Python):
K,N = map(int, input().split())
 
A=[]
 
A=list(map(int, input().split()))

D=[]

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

Dmax=0
Sum=0
for i in range(N):
  if int(D[i])>Dmax:
    Dmax=int(D[i])
  Sum=Sum+int(D[i])   
  
print(Sum-Dmax)

Solution (Python):
import sys
 
k, n = map(int, sys.stdin.readline().split())
a = [int(i) for i in sys.stdin.readline().split()]
a.sort()
max_dist = 0
for i in range(1, n):
	max_dist = max(a[i] - a[i - 1], max_dist)
    
if len(a) != 0:
	max_dist = max(a[0] + k - a[-1], max_dist)
sys.stdout.write(str(k - max_dist))

Solution (Python):
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)

Solution (Python):
k, n = map(int, input().split())
an = list(map(int, input().split()))
ans = k
for i in range(n):
  ans = min(ans, (an[i-1]-an[i])%k, (an[i]-an[(i+1)%n])%k)
print(ans)

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = -1*float('inf')
for i in range(n-1):
  ans = max(ans, a[i+1]-a[i])
ans = max(k+a[0]-a[n-1], ans)
print(k-ans)

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

Solution (Python):
data1 = input()
data2 = input()
circle = int(data1.split()[0])
house = int(data1.split()[1])
distance = [int(s) for s in data2.split()]

between = []
for i,d in enumerate(distance):
  if i != house-1:
    between.append(distance[i+1]-d)
  else:
    between.append(circle-d+distance[0])
print(sum(between)-max(between))

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

Solution (Python):
lake, _ = [int(e) for e in input().split()]
arr = [int(h) for h in input().split()]
length = [n2-n1 for n1, n2 in zip(arr[:-1], arr[1:])]
length.append(lake-arr[-1]+arr[0])
length.sort()
print(lake-length[-1])

Solution (Python):

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

b = a+[a[0]+k]
longest_distance = 0
for bi,ai in zip(b[1::],a):
    longest_distance=max(longest_distance,bi-ai)
print(k-longest_distance)

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

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

answer = K - max_length
print(answer)

Solution (Python):
kp = list(map(int, input().rsplit()))
A = list(map(int, input().rsplit()))
max_range = kp[0]-A[-1]+A[0]
for idx in range(len(A)-1):
  dif = A[idx+1]-A[idx] 
  if dif >max_range:
    max_range = dif
 
print(kp[0] - max_range)

Solution (Python):
a,b = map(int, input().split())
c = list(map(int, input().split()))
ma = [c[0]+a-c[b-1]]
for i in range (0,b-1):
    ma.append(c[i+1]-c[i])
d = max(ma)
print(a-d)

Solution (Python):
k, n = map(int, input().split())
li_a = list(map(int, input().split()))
li_d = list()
for idx, a in enumerate(li_a):
    if idx == (n - 1):
        li_d.append(abs(k - li_a[idx] + li_a[0]))
    else:
        li_d.append(abs(li_a[idx] - li_a[idx + 1]))
print(sum(li_d) - max(li_d))

Solution (Python):
K, N = tuple(int(v) for v in input().split())

A_list = list(int(a) for a in input().split())
















A_list += [a + K for a in A_list[:-1]]
print(min(A_list[i+N-1] - A_list[i] for i in range(N)))

Solution (Python):

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]))

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


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

print(K-max(B))

Solution (Python):
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]))

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
d = a[0]+k-a[n-1]
for i in range(n-1):
    d = max(d,a[i+1]-a[i])
print(k-d)

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

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

if __name__ == '__main__':
    main()

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

max = 0

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

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

print(k-max)

Solution (Python):
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])+a[0])
print(k-max(b))

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

max_val = 0

for i in range(n):
    lg = 0
    if i == 0:
        lg = int(a[i] + (k - a[n-1]))
    else:
        lg = int(a[i] - a[i-1])

    if lg > max_val:
        max_val = lg
print(k - max_val)

Solution (Python):
circum_total = [int(n) for n in input().split()]

circum = circum_total[0]
total = circum_total[1]

distances = [int(n) for n in input().split()]

mindist = circum
for index in range(len(distances)):
    if index == len(distances) - 1:
        rightdist = distances[0] + circum - distances[index]
    else:
        rightdist = distances[index+1] - distances[index]
    if index == 0:
        leftdist = distances[index] - distances[index-1] + circum
    else:
        leftdist = distances[index] - distances[index-1]
    maxdist = max(rightdist,leftdist)
    mindist = min(mindist, circum - maxdist)

print(mindist)

Solution (Python):
k, n = map(int, input().split())
array = [int(i) for i in input().split()]
ans = []
for i in range(n - 1):
    ans.append(array[i+1] - array[i])
ans.append(array[0] + k - array[n-1])
print(sum(ans) - max(ans))

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
ans=0
for i in range(N):
    if i==N-1:
        dis=K+A[0]-A[N-1]
    
    else:
        dis=A[i+1]-A[i]
    ans=max(dis,ans)
print(K-ans)

Solution (Python):
K,N = list(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)

Solution (Python):
k, n = map(int,input().split())
a = list(map(int,input().split()))
l = 0
a.append(a[0]+k)
for i in range(n):
    l = max(l, a[i+1] - a[i])
print(k-l)

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

max = 0

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

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

print(k - max)

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

cost = max(a[0]+k-a[-1],a[-1]-a[-2])
for i in range(1,n-1):
    cost = max(a[i]-a[i-1],a[i+1]-a[i],cost)
print(k-cost)

Solution (Python):
K, N = map(int, input().split(' '))
A = list(map(int, input().split()))
m = 0

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

print(K - m)

Solution (Python):
k, n = map(int, input().split())
A = list(map(int, input().split()))

A.append(A[0]+k)
l = 0
for i in range(n):
    l = max(l, A[i+1]-A[i])
print(k-l)

Solution (Python):
from itertools import *
K, N = map(int, input().split())
A, B = tee(map(int, input().split()), 2)

first = next(B)
M = max(b-a for a, b in zip_longest(A, B, fillvalue=first+K))

print(K-M)

Solution (Python):
K,N=map(int,input().split())
A=[int(e) for e in input().split()]
dis=[]
for i in range(N):
    if i==N-1:
        dis.append(K-A[i]+A[0])
    else:
        dis.append(A[i+1]-A[i])
dis.sort()
ans=0
for i in range(N-1):
    ans+=dis[i]
print(ans)

Solution (Python):
input1=list(map(int,input().split()))
input2=list(map(int, input().split()))

distance=[]
for order,value in enumerate(input2):
    if order==0:
        pass
    else:
        distance.append(value-input2[order-1])
distance.append(input1[0]-sum(distance))

print(input1[0]-max(distance))

Solution (Python):
K,N = map(int,input().split())
ls_A = list(map(int,input().split()))
d_ls_A = []
for i in range(len(ls_A)-1):
    d_ls_A.append(ls_A[i+1]-ls_A[i])
d_ls_A.append(K-ls_A[N-1]+ls_A[0])
print(K-max(d_ls_A))

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

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

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

Solution (Python):
s=input().split()
k,n=int(s[0]),int(s[1])
s=[]
a=input().split()

for i in range(0,n):
  
  if(i == n-1):
    s.append(abs(k-int(a[i])+int(a[0])))
  else:
  	s.append(abs(int(a[i])-int(a[i+1])))
print(k-max(s))

Solution (Python):

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

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

for i in range(N-1):
    diff = max(diff , A[i+1]-A[i])

print(K-diff)

Solution (Python):
def solve(string):
    k, n, *a = map(int, string.split())
    b = [a[-1]] + [_a + k for _a in a]
    return str(min(a1 - a0 for a0, a1 in zip(a, b)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
k, n = map(int, input().split())
l = list(map(int, input().split()))
maxi = k-l[-1]+l[0]
for i in range(1, n):
    maxi = max(l[i] - l[i-1], maxi)
print(k-maxi)

Solution (Python):
K,N =(int(x) for x in input().split())
array = list(map(int, input().strip().split()))
between = [0] * N
for i in range(N-1):
    between[i] = array[i+1] -array[i]
between[N-1] = K- array[N-1]  + array[0]

print(K-max(between))

Solution (Python):
dist_lake, num_home = map(int, input().split())
dist_start_to_home = tuple(map(int, input().split()))

max_length = 0

for index, dist in enumerate(dist_start_to_home):
  	
    if index == len(dist_start_to_home) - 1:
      	length = dist_lake - (dist_start_to_home[index] - dist_start_to_home[0])
    else:
      	length = dist_start_to_home[index + 1] - dist_start_to_home[index]
    if max_length < length:
    	max_length = length
print('{}'.format(dist_lake - max_length))

Solution (Python):
k, n = list(map(int, input().split()))
dist = list(map(int, input().split()))
dist.append(k+dist[0])
l = 0

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

print(k - l)

Solution (Python):
def C():
    kn=list(map(int,input().split()))
    A=list(map(int,input().split()))
    d=[]
    sum_ans=[]
   
    for i in range(kn[1]):
        
        if i==kn[1]-1:
            d.append(kn[0]-A[i])
        else:
            d.append(A[i+1]-A[i])
        
        
    sum_ans.append(A[kn[1]-1]-A[0])
    for i in range(1,kn[1]):
        sum_ans.append(min(kn[0]-d[i],kn[0]-d[i-1]))
    
    print(min(sum_ans))    
        
    
if __name__=='__main__':
    C()

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
max = 0
for i in range(N):
    len = 0
    if i == 0:
        len = int(A[i] + (K - A[N - 1]))
    else:
        len = int(A[i] - A[i - 1])

    if len > max:
        max = len

print(K - max)

Solution (Python):
k,n = map(int,input().split())
a = [int(i) for i in input().split()]
a.append(k)
dist = []
for i in range(1,n+1):
    cou = abs(a[i-1] - a[i])
    dist.append(cou)
if a[0] !=0:
    dist[-1]+=a[0]
dist.sort()
print(sum(dist[:n-1]))

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

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

Solution (Python):
k,n=map(int,input().split())
v=list(map(int,input().split()))
a=0
for i in range(1,n):
   a=max(a,v[i]-v[i-1])
print(k-max(a,k-v[n-1]+v[0]))

Solution (Python):
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
max = k - a[len(a) - 1] + a[0]
for i in range(len(a) - 1):
    t = a[i + 1] - a[i]
    if(t > max): max = t

print(k - max)

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = []
for i in range(n-1):
    ans.append(a[i+1]-a[i])
ans.append(a[0]+k-a[-1])
print(sum(ans)-max(ans))

Solution (Python):
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(1, n):
    ans = min(ans, k-a[i]+a[i-1])
print(ans)

Solution (Python):
K, N, *A = map(int, open(0).read().split())

A.append(K + A[0])

B = [b - a for a, b in zip(A, A[1:])]
print(sum(B) - max(B))

Solution (Python):
import numpy as np

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

a_np = np.append(a, a[0] + k)
print(k - max(np.diff(a_np)))

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

Solution (Python):
k,n=map(int,input().split())
arr=list(map(int,input().split()))
ans=10**18
for i in range(n):
  dist1=(arr[i-1]-arr[i])%k
  dist2=(arr[i]-arr[(i+1)%n])%k
  ans=min(ans,dist1,dist2)
print(ans)

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

b = [a[i+1]-a[i] for i in range(n-1)]
c = b.append(int(a[0]+k-a[-1]))
d = max(b)

print(int(k-d))

Solution (Python):
k, n = input().split()
k, n = int(k), int(n)
a = input().split()
a = [int(dist) for dist in a]
dMax = a[0]+(k-a[n-1])
for dist in range(1, n-1):
    dMax = max(dMax, a[dist+1]-a[dist])
print(k-dMax)

Solution (Python):
k , n = map(int, input().split())
arr = list(map(int, input().split()))

dist = 0

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

print(k - dist)

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

distance = []

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

dis = K - A[N - 1] + A[0]
distance.append(dis)

index = distance.index(max(distance))

print(K - distance[index])

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

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

print(K - max_distance)

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

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

print(K-d)

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

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

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

s = []
for i in range(N):
  if N == i + 1:
    s.append(A[0] + abs(K - A[i]))
  else:
    s.append(abs(A[i+1] - A[i]))

total = 0
srtd = sorted(s)
for i in range(len(srtd) - 1):
  total += srtd[i]

print(total)

Solution (Python):
K,N = map(int,input().split())
L = list(map(int,input().split()))

L = [0] + L + [K]



max_distance = 0

for i in range(1,N):
    max_distance = max(max_distance,L[i+1]-L[i])

max_distance = max(max_distance,(L[-1]-L[-2])+(L[1]-L[0]))

print(K - max_distance)

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

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

print(k-wk)

Solution (Python):
import numpy as np
import statistics

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

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


b = np.array(a)


b = b%k
b.sort()



dis = 0

for i in range(1,n):
    dis = max(dis,abs(b[i]- b[i-1]))

dis = max(dis,(b[0]+k-b[n-1]))


print(k - dis)

Solution (Python):
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[0]-A[-1])

print(K-max(d))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    K, N = map(int, rl().split())
    A = list(map(int, rl().split()))
    
    max_d = K - A[-1] + A[0]
    for i in range(1, N):
        max_d = max(max_d, A[i] - A[i - 1])
    print(K - max_d)


if __name__ == '__main__':
    solve()

Solution (Python):

"""
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)

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

Solution (Python):
kn = list(map(int,input().split()))
A = list(map(int,input().split()))
K = kn[0]
N = kn[1]
x = []
for i in range(N - 1):
    x.append(A[i + 1] - A[i])
x.append(abs(K - A[N - 1] + A[0]))
print(sum(x) - max(x))

Solution (Python):
def main():
    K, N = map(int, input().split())
    A = [int(i) for i in input().split()]
    
    max_dt = K - A[-1] + A[0]
    
    for i in range(len(A) - 1):
        max_dt = max(max_dt, A[i+1] - A[i])

    print('{}'.format(K - max_dt))

if __name__ == '__main__':
    main()

Solution (Python):
k,n = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.append(k+A[0])
m = max(y-x for x,y in zip(A,A[1:]))
ans = k-m
print(ans)

Solution (Python):
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))

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
dis=0
for i in range(N-1):
    dis=max(dis,A[i+1]-A[i])
dis=max(dis,A[0]+K-A[N-1])    
print(K-dis)

Solution (Python):
k,n=map(int,input().split())
A=sorted(list(map(int,input().split())))
A+=[A[0]+k]
B=max(y-x for x,y in zip(A,A[1:]))
print(k-B)

Solution (Python):
import math
import string
import collections
from collections import Counter


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

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

tokusyu = (k-(k-a[n-1]+a[0]))



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

Solution (Python):
n = [int(e) for e in input().split(" ")]
a = [int(e) for e in input().split(" ")]
a.append(n[0] + a[0])
m = 0
for i in range(len(a) - 1):
    d = a[i + 1] - a[i]
    if(m < d):
        m = d
else:
    print(n[0] - m)

Solution (Python):
import numpy as np

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


locations = input().split()
dists = []
prev_location = None

for location in locations:
  if prev_location is None:
    prev_location = int(location)
  else:
	  dists.append(int(location) - prev_location)  
	  prev_location = int(location)
dists.append(N - int(locations[-1]) + int(locations[0]))


print(N - np.array(dists).max())

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

d = {}
for i in range(n):
  if i == 0:
    d[i] = a[i]
  elif i == n-1:
    d[i] = a[i] - a[i-1]
    d[0] += k - a[i]
  else:
    d[i] = a[i] - a[i-1]

d2 = sorted(d.items(),key=lambda x:x[1], reverse=True)
print(k - d2[0][1])

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

r = a[0] + (k - a[n - 1])

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

print(k - r)

Solution (Python):
K, N = map(int, input().split())
A = [int(i) for i in input().split()]
ans = A[-1] - A[0]

for i in range(1, N-1):
    ans = min(ans, K - A[i] + A[i-1], A[i] + (K - A[i+1]))

print(ans)

Solution (Python):
k, n = map(int, input().split())
a = sorted(list(map(int, input().split())))
distance = []
for i in range(len(a)):
    if i == 0:
        distance.append(a[i] + (k - a[len(a) - 1]))
    else:
        distance.append(a[i] - a[i - 1])
distance.remove(max(distance))
print(sum(distance))

Solution (Python):
X, N = map(int,input().split())
A = list(map(int, input().split()))
dist = []
for i in range(N): 
    if i < N-1: 
        dist.append(X-(A[i+1]-A[i])) 
    else: 
        dist.append(X-(X-A[N-1]+A[0]))

print(min(dist))

Solution (Python):
k,n=map(int,input().split())
arr=list(map(int,input().split()))
ans=10**18
for i in range(n):
  dist1=(arr[i-1]-arr[i])%k
  dist2=arr[i]+(k-arr[(i+1)%n])%k
  ans=min(ans,dist1,dist2)
print(ans)

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

Solution (Python):
K , N = map(int,input().split())
A = list(int(i) for i in input().split())

maxL = (A[0]+K)-A[-1]
maxID =0

for x in range(1,N):
    dis = A[x]-A[x-1]
    if maxL <dis:
        maxL = dis
        maxID =x
print(K-maxL)

Solution (Python):
k, n = map(int, input().split())
A = list(map(int, input().split()))

DISTANCE = [k + A[0] - A[-1]]
for i in range(n - 1):
    DISTANCE.append(A[i + 1] - A[i])

print(k - max(DISTANCE))

Solution (Python):
import numpy as np

k, n = map(int, input().split())
a_list = list(map(int, input().split()))
a_list.append(k+a_list[0])
diff = list(np.diff(a_list))
diff.remove(max(diff))
print(sum(diff))

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
s=0
b=[0]*(n-1)
for i in range(n-1):
  b[i]=a[i+1]-a[i]
  
b.append(k+a[0]-a[n-1])

c=sorted(b,reverse=True)
for i in range(1,n):
  s+=c[i]
print(s)

Solution (Python):
K, N = list(map(int, input().split()))
As = list(map(int, input().split()))
diffs =[]
for base, switch in zip(As[:-1], As[1:]):
  diffs.append(switch-base)
diffs.append(As[0]+K-As[-1])
print(K-max(diffs))

Solution (Python):
K, N = list(map(int, input().split()))
As = list(map(int, input().split()))
dists = []
for base, switch in zip(As[:-1], As[1:]):
    dists.append(switch - base)
dists.append(As[0]+(K-As[-1]))
print(K - max(dists))

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


l = []

l.append(K - A[-1] + A[0])
for i in range(1, N):
  l.append(A[i] - A[i - 1])
l.pop(l.index(max(l)))
print(sum(l))

Solution (Python):




k, n = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0]+k)
diff = 0
for i in range(n):
    diff = max(A[i+1]-A[i], diff)
print(k-diff)

Solution (Python):
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(K - max(d))

Solution (Python):




k, n = map(int, input().split())
A = list(map(int, input().split()))
diff = []
for i in range(n-1):
    diff.append(A[i+1]-A[i])
diff.append((k-max(A))+min(A))
print(k-max(diff))

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
max_ans=0
for i in range(N):
    if i==N-1:
        ans=K-A[N-1]+A[0]
    else:
        ans=A[i+1]-A[i]
    if max_ans<ans:
        max_ans=ans
ans=K-max_ans
print(ans)

Solution (Python):
K, N = map(int, input().split(" "))
A = [int(a) for a in input().split(" ")]

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

print(sum(seg) - max(seg))

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

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

Solution (Python):

k,n=map(int,raw_input().split())
a=map(int,raw_input().split())
temp=0
for i in range(1,n):
 temp=max(a[i]-a[i-1],temp)
temp=max(k-a[n-1]+a[0],temp)
print k-temp

Solution (Python):
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
B = []


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

print(min(B))

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

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

for i in range(N-1):
    s = max(A[i+1] - A[i],s)

print(K-s)

Solution (Python):
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(K - max(B))

Solution (Python):
import numpy as np

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

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


A=np.array(A)

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

length=abs(lin-le)

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

A=np.append(length,rem)

M=np.max(A)

print(K-M)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


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

    ans = [K - A[i] + A[i - 1] for i in range(1, N)]
    print(min(min(ans), A[-1] - A[0]))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(K + A[0])
    B = np.array(A[1:])
    A = np.array(A[:-1])
    Diff = B - A
    max_diff = max(Diff)
    answer = K - max_diff
    return answer


if __name__ == "__main__":
    answer = main()
    print(answer)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


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

    ans = [K - A[i] + A[i - 1] for i in range(1, N)]
    print(min(min(ans), A[-1] - A[0]))


main()

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
distances = [0] * n
for i in range(n - 1):
  distances[i] = a[i + 1] - a[i]
distances[n - 1] = k - a[n - 1] + a[0]
print(k - max(distances))

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(K + A[0])
    Diff = [y - x for x, y in zip(A, A[1:])]
    answer = K - max(Diff)
    return answer


answer = main()
print(answer)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(K + A[0])
    Diff = [y - x for x, y in zip(A, A[1:])]
    answer = K - max(Diff)
    return answer


if __name__ == "__main__":
    answer = main()
    print(answer)

Solution (Python):
import numpy as np
from collections import deque
k,n=map(int,input().split())
lst=np.fromstring(input(),int,sep=' ')
dis=[]
for i in range(1,n):
    dis.append(lst[i]-lst[i-1])
print(k-max(k-lst[-1]+lst[0],max(dis)))

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


K, N = map(int, input().split())
A = list(map(int, input().split()))
A.append(K + A[0])
Diff = [y - x for x, y in zip(A, A[1:])]
answer = K - max(Diff)
print(answer)

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

a0 = a[-1]
ans = [a[0] - (a0 -k)]
a0 = a[0]
for i in a[1:]:
    ans.append(i - a0)
    a0 = i

print(k - max(ans))

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


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

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

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

print(ans)

Solution (Python):









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


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

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

print(K-MaxLen)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
A.append(A[0]+K)
maxl=0
for i in range(N):
  if A[i+1]-A[i]>maxl:
    maxl=A[i+1]-A[i]
print(K-maxl)

Solution (Python):
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(a[0]+k-a[-1])

s=sum(b)
m=max(b)
print(s-m)

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

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

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

print(k-max(d))

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

A += [K + A[0]]

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