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

import os

def remove_empty(target_list: list):
    empty_removed_list = [x for x in target_list if x]
    return empty_removed_list


def get_stdin_str_list(input_str=None):
    if input_str is None:
        input_str = input()
    input_str = input_str.strip()
    input_str_list = input_str.split()
    return input_str_list


def get_stdin_int_list(input_str=None):
    input_str_list = get_stdin_str_list(input_str)
    input_int_list = list(map(int, input_str_list))
    return input_int_list


def get_stdin_str_multi_list(input_str=None):
    if input_str is None:
        input_str = input()
    input_str = input_str.strip()
    input_str_line_list = input_str.split("\n")
    input_str_multi_list = list()
    for str_line in input_str_line_list:
        input_str_list = get_stdin_str_list(str_line)
        input_str_multi_list.append(input_str_list)
    return input_str_multi_list


def get_stdin_int_multi_list(input_str=None):
    input_str_multi_list = get_stdin_str_multi_list(input_str)
    input_int_multi_list = list()
    for str_list in input_str_multi_list:
        input_int_list = list(map(int, str_list))
        input_int_multi_list.append(input_int_list)
    return input_int_multi_list

def get_most_far_pos_index_from_both_neighbors(pos_list, end_pos_distance=None):
    if end_pos_distance is None:
        end_pos_distance = pos_list[-1]
    N = len(pos_list)
    max_distance = 0
    most_far_pos_index = 0
    for i, p in enumerate(pos_list):
        if i == 0:
            distance = end_pos_distance - pos_list[-1] + pos_list[i+1]
        elif i == N-1:
            distance = end_pos_distance - pos_list[i-1] + pos_list[0]
        else:
            distance = pos_list[i+1] - pos_list[i-1]
        
        if distance > max_distance:
            max_distance = distance
            most_far_pos_index = i

    return most_far_pos_index

def get_longest_interval(pos_list, end_pos_distance=None):
    if end_pos_distance is None:
        end_pos_distance = pos_list[-1]
    N = len(pos_list)
    longest_interval = 0

    for i, p in enumerate(pos_list):
        if i == N-1:
            pos_n_1 = end_pos_distance + pos_list[0]
        else:
            pos_n_1 = pos_list[i+1]

        interval = pos_n_1 - p

        if interval > longest_interval:
            longest_interval = interval

    return longest_interval

K, N = get_stdin_int_list()
A = get_stdin_int_list()
longest_interval = get_longest_interval(A, K)
shortest_distance = K - longest_interval
print(shortest_distance)

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])
c=max(b)
d=max(c,a[0]+k-a[n-1])
print(k-d)

Solution (Python):
K, N = map(int, input().split(" "))
A_list = [int(i) for i in input().split()]
max_diff = -1

for i in range(0, len(A_list)):
  if i == len(A_list)-1:
    if max_diff < (K-A_list[i]+A_list[0]):
      max_diff = K-A_list[i]+A_list[0]
  else:
    if max_diff < (A_list[i+1] - A_list[i]):
      max_diff = A_list[i+1] - A_list[i]

print(K - max_diff)

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

Solution (Python):


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

maxd = 0

for i in range(len(a)):
    if i == 0:
        maxd = a[0] + (k - a[len(a) - 1])
    else:
        maxd = max(a[i] - a[i - 1], maxd)


print(k - maxd)

Solution (Python):
import sys

k,n=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))

max=0
for i in range(n):
    if i==0:
        pre=a[i]
    else:
        now=a[i]
        if now-pre>max:
            max=now-pre
        pre=now
if k-pre+a[0]>max:
    max=k-pre+a[0]
print(k-max)

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

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

Solution (Python):
m, n = map(int, input().split())

a = list(map(int, input().split()))
b = []


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

b.append((m + a[0]) - a[n - 1])

b.sort()
res = 0
for i in range(len(b) - 1):
  res += b[i]
print(res)

Solution (Python):

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


difl=[]

for i in range(n-1):
    difl.append(al[i+1]-al[i])
difl.append(al[0]+k-al[n-1])

print(k-max(difl))

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

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

diffarr = [arr[i+1]-arr[i] for i in range(n-1)]
diffarr.append(arr[0]+k-arr[n-1])

print(k-max(diffarr))

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

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

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

Solution (Python):
K, N = map(int, input().split())
houselist = list(map(int, input().split()))
max=0
for i in range(1,N):
  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]
if max==0:
  print(0)
else:
  print(K-max)

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

print(K-max(distance))

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
b = []
b.append(k - a[n-1] + a[0])
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())
L = [i for i in map(int,input().split())]
L.append(L[0]+K)
dist = []
for i in range(N):
    dist.append(L[i+1]-L[i])
print(sum(dist)-max(dist))

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

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

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
a2 = []
for i, d in enumerate(a):
    if i != 0:
        a2.append(d - a[i - 1])
    else:
        a2.append(d + k - a[n - 1])

a2.sort(reverse=True)

print(k - a2[0])

Solution (Python):
k, n = map(int, input().split())
a = list(map(int,input().split()))[::-1]
diff = 0

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

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

ans = k-diff

print(ans)

Solution (Python):
KN = list(map(int, input().split()))
dis = list(map(int, input().split()))
lists = []
for i in range(KN[1]-1):
    lists.append(dis[i+1]-dis[i])

lists.append(KN[0]+dis[0]-dis[KN[1]-1])
print(KN[0]-max(lists))

Solution (Python):






import functools

if __debug__:
    debug = lambda *x: None
else:
    import logging
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    debug = logger.debug

k, n, *a = (int(x) for x in input().split())
a = [int(x) for x in input().split()]

debug(k)
debug(n)
debug(a)

aa = a.copy()
aa.pop(0)
aa.append(k + a[0])
debug(aa)


d = [y - x for x, y in zip(a,aa)]
debug(d)
d.sort()
debug(d)
d.pop()
debug(d)
print(sum(d))

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):
import scipy as sp

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

a_list = sp.array(a_list)
sub_a_list = (a_list - sp.roll(a_list, 1))[1:]
sub_a_list = sp.append(sub_a_list, a_list[0]+k-a_list[-1])
print(k-max(sub_a_list))

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

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

Solution (Python):
k, n = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
t = 0
id = 0
l = 0
for i in range(n):
    if i < n - 1:
        d = abs(a[i] - a[i + 1])
    else:
        d = k - a[n - 1] + a[0]
    if t < d:
        t = d
        id = i + 1 if i < n - 1 else 0
for i in range(n - 1):
    l += abs(a[(i + id) % n] - a [(i + id + 1) % n]) if i + id != n - 1 else k - a[n - 1] + a[0]
print(l)

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

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

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

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

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

A.append(A[0] + K)

max_distance = A[1] - A[0]
for i in range(N):
    if A[i + 1] - A[i] > max_distance:
        max_distance = A[i + 1] - A[i]
print(K - max_distance)

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

a.append(a[0] + k)

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

print(k - max_distance)

Solution (Python):
E = [input() for i in range(2)]
E0 = E[0].split()
K = int(E0[0])
N = int(E0[1])
E12 = E[1].split()
E1 = [int(s) for s in E12]
E1.append(K + E1[0])
d = 0
M = 0
gap = []
for k in range(len(E1)-1):
    gap.append(int(E1[k+1]) - int(E1[k]))
    d += gap[k]
    M = max(M, gap[k])


d -= M

print(d)

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

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

out = K - max(sa)

print(out)

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

Solution (Python):
def solve(k, n, a):
    a.append(a[0] + k)
    return min((k - a[i+1] + a[i]) for i in range(n))

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

Solution (Python):
N , N_point = map(int,input().split())
list_point = input().split()

for i in range(len(list_point)):
  list_point[i] = int(list_point[i])


distance = []
for i in range(len(list_point)):
  if i == 0:
    distance.append(list_point[i] + N-list_point[len(list_point)-1])
    distance.append(list_point[i+1]-list_point[i])
    continue
  if i == len(list_point)-1:
    break
  distance.append(list_point[i+1]-list_point[i])


distance.sort()

distance.remove(max(distance))


print(sum(distance))

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
B=[0 for a in A]
B[0]=K+A[0]-A[-1]
s=B[0]
t=B[0]
for n in range(N-1):
  B[n+1]=A[n+1]-A[n]
  s+=B[n+1]
  if t<B[n+1]:
    t=B[n+1]
print(s-t)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    K, N = input_nums()
    A = input_nums()
    dist = []
    for i in range(len(A)-1):
        dist.append(A[i+1]-A[i])
    dist.append(K+A[0]-A[-1])
    maxv = max(dist)
    print(sum(dist) - maxv)
    

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    K, N = input_nums()
    A = input_nums()
    dist = []
    for i in range(len(A)-1):
        dist.append(A[i+1]-A[i])
    dist.append(K+A[0]-A[-1])
    maxv = max(dist)
    print(sum(dist) - maxv)
    

if __name__ == '__main__':
    main()

Solution (Python):
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] + A[0]) >= max:
    max = abs(K - A[N-1] + A[0])

print(K - max)

Solution (Python):
import sys

read = sys.stdin.buffer.read
K,N,*A = map(int, read().split())
A += [A[0]+K]
gap = max(y-x for x,y in zip(A, A[1:]))

print(K-gap)

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

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

maxd = 0

for i in range(n):
  if i < (n-1):
    d = alist[i+1] - alist[i]
  else:
    d = k - ( alist[i] - alist[0] )
  if maxd < d:
    maxd = d
print(k - maxd)

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

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

A_split.pop(A_split.index(max(A_split)))
print(sum(A_split))

Solution (Python):
import sys
read = sys.stdin.buffer.read

K, N, *A = map(int, read().split())
A += [A[0] + K]
gap = max(y-x for x,y in zip(A, A[1:]))
print(K - gap)

Solution (Python):
import sys
input = sys.stdin.readline

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

if __name__ == "__main__":
    main()

Solution (Python):
k,n= [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

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

tmp = k - a[n-1] + a[0] 
if tmp > mx:
    mx= tmp

print(k-mx)

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

A_shift = np.roll(A, -1)
A_diff = A_shift - A
ans = k - np.max(A_diff[:-1])
print(str(ans))

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

Solution (Python):
A=input()
B=input()
AA=A.split()
K=int(AA[0])
N=int(AA[1])
d=(B.split())
dis=[]
for i in range(N-1):
    dis.append(int(d[i+1])-int(d[i]))

dis.append(int(d[0])+(K-int(d[N-1])))

print(K-max(dis))

Solution (Python):
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[0] - A_list[i]
        distance.append(diff)
        
    else:
        diff = A_list[i+1] - A_list[i]
        distance.append(diff)
        

print(sum(distance) - max(distance))

Solution (Python):

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    
    
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right


if __name__ == "__main__":
    k,n = list((map(int, input().split(' '))))
    a = list(map(int, input().split(' ')))
    
    result = 0
    for i in range(1,n):
        length = a[i]-a[i-1]
        result = max(length, result)
        
    length = a[0] + (k-a[n-1])
    result = max(result, length)
    print(k-result)

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

Solution (Python):
k, n = map(int, input().split())
mid = k / 2
a = [int(i) for i in input().split()]
max_diff = a[0]  + (k - a[-1])
for i in range(n - 1):
  diff = a[i + 1] - a[i]
  if diff > max_diff:
    max_diff = diff
print(k - max_diff)

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

Solution (Python):
first = input()
first = first.split()

K = int(first[0])
N = int(first[1])

second = input()
second = second.split()

distance = []
diff = 0

for i in range(0, N):
    if i == N-1:
        diff0 = 0
        diff1 = 0
        diff0 = int(second[0])
        diff1 = K - int(second[i])
        diff = diff0 + diff1
        distance.append(diff)
    else:
        diff = int(second[i+1]) - int(second[i])
        distance.append(diff)
        
MaxIndex = distance.index(max(distance))
distance.pop(MaxIndex)
Max = sum(distance)
print(Max)

Solution (Python):
k,n = list(map(int,input().split()))
arr = list(map(int,input().split()))
max_d = k-arr[-1] + arr[0]
for i in range(n-1):
  max_d = max(max_d,arr[i+1]-arr[i])
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 - a[-1] + a[0])
diff.sort()
print(k - diff[-1])

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

distance = [A_list[i] - A_list[i - 1] for i in range(len(A_list))]
distance[0] = A_list[0] + K - A_list[N - 1]
index = distance.index(max(distance))

print(sum(distance) - distance[index])

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

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[0] - a[-1])
print(k - max(d))

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

max = k + a[0] - a[n-1]
prev = a[0]
for i in a[1:]:
    tmp = i - prev
    if tmp > max:
        max = tmp

    prev = i

print(k-max)

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

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

print(k-max)

Solution (Python):
k,n=map(int,input().split())
A=list(map(int,input().split()))
B=[]
B.append(k-A[n-1]+A[0])
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()))
a = sorted(a)
diff = [0]*N
for i in range(N):
    diff[i] = a[i] - a[i-1]
    if i == 0: diff[i] += K

print(K-max(diff))

Solution (Python):
[k,n] = list(map(int, input().split()))
 
li = list(map(int, input().split()))
 
distances = []
for i in range(n):
    if i == 0:
        distances.append(li[n-1] - li[i])
    else:
        distances.append(k - (li[i] - li[i-1]))
 
print(min(distances))

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

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

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

Solution (Python):
a,b = map(int,input().split())
aaa = [int(i) for i in input().split()]

aaa.append(aaa[0] + a)

result = [c1 - c0 for c0,c1 in zip(aaa,aaa[1:])]

print(a - max(result))

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

Solution (Python):
k,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]

if a[0]==0:
    a.append(k)
else:
    a.append(k+a[0])
l=[]
for i in range(len(a)-1):
    s = a[i+1]-a[i]
    if s<0:
        s=-s
    l.append(s)
max_value=max(l)

print(k-max_value)

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

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

Solution (Python):
import sys

k, n = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
a.sort()

max_len = 0
total = 0
for i in range(n):
    if a[i] < a[i-1]:
        l = a[i] + k - a[i-1]
    else:
        l = a[i] - a[i-1]
    max_len = max(max_len, l)
    total += l

print(total-max_len)

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

distance = []
for i in range(len(A)-1):
    distance.append(A[i]+K-A[i+1])
distance.append(max(A)-min(A))
print(min(distance))

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

distance = []
for i in range(len(A)-1):
    distance.append(A[i]+K-A[i+1])
distance.append(max(A)-min(A))
print(min(distance))

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

A.append(K+A[0])

ans=0
maxdist=0
for i,a in enumerate(A):
    if i == len(A)-1:
        break
    dist = A[i+1]-a   
    ans += dist
    maxdist=max(maxdist,dist)
print(ans-maxdist)

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

A.append(K+A[0])

maxdist,maxindx = 0,0
for i,a in enumerate(A):
    dist = -a
    if i+1 == len(A):
        dist += K
    else:
        dist += A[i+1]
    if dist > maxdist:
        maxdist = dist
        maxindx = i
ans = 0
num=0
for x,a in enumerate(A):
    if num==len(A)-2:
        break
    if x == maxindx:
        continue
    num+=1
    if x+1 == len(A):
        ans += K-a
    else:
        ans += A[x+1]-a
print(ans)

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

Solution (Python):
n,k=map(int,input().split())
x=[int(q) for q in input().split()]

d=[]

for i in range(1,len(x)):
    d.append(x[i]-x[i-1])
    
d.append(min(x[-1]-x[0], n-x[-1]+x[0]))
print(n-max(d))

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

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

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

Solution (Python):
K, N = map(int, input().split())
A = sorted([int(a) for a in input().split()])
D = [abs(A[i]-A[i+1]) if i != N-1 else abs(A[i]-K-A[0]) for i in range(N)]
D.remove(max(D))
print(sum(D))

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=(int(x) for x in input().split())
A = list(map(int,input().split()))
check = K- A[N-1] + A[0]

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

num = K - check
print(num)

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

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

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

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

print(min(distances))

Solution (Python):
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[0] - A[-1])
dist.sort()
print(sum(dist[:n-1]))

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

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

Solution (Python):
import numpy as np
K, N = map(int, input().split())
A = list(map(int, input().split()))
diff = np.diff(A)
diff = np.append(diff, A[0] + K - A[-1])
minimum = K - max(diff)
print(minimum)

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

q_dist = 0

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

print(K - q_dist)

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

relative = []
relative.append(distance[0])
for i in range(int(N)-1):
    relative.append(distance[i+1] - distance[i])
relative[0] += (int(K) -  distance[-1])
print(sum(relative) - max(relative))

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

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

print(K - max(D))

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

input_lines = [int(i) for i in input().split()]

max_range = input_lines[0]+(K-input_lines[-1])
for i in range(N):
    if i!=N-1:
        torange = input_lines[i+1]-input_lines[i]
        if torange>max_range:
            max_range = torange
print(K-max_range)

Solution (Python):
k, n = input().split()
a_list_org = list(map(int,input().split()))
a_list = []
for a in a_list_org:
    if a == a_list_org[0]:
        pre = a
        a_list.append(int(k) - a_list_org[-1] + a)
    else:
        a_list.append(a - pre)
        pre = a
a_list.remove(max(a_list))
print(sum(a_list))

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

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

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

Solution (Python):
def main(k, n, A):
    distances = [A[0] + k-A[-1]]

    for i in range(1, n):
        distances.append(A[i] - A[i-1])

    print(k - max(distances))


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

    main(k, n, A)

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

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

x = 10 ** 6

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

print(x)

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

Solution (Python):

k,n=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
i=a[0]
l=0 
for j in a[1:]:
  if l < j-i: 
    l = j-i
  i=j
if l < a[0]+k-a[-1]:
  l=a[0]+k-a[-1]
print(k-l)

Solution (Python):
kn_list = input().split(" ")
K = int(kn_list[0])
N = int(kn_list[1])
a_list = [int(num) for num in input().split(" ")]

max_distance = 0
for i in range(N-1):
    distance = a_list[i+1] - a_list[i]
    if max_distance < distance:
        max_distance = distance
if max_distance < K - (a_list[N-1]-a_list[0]):
    max_distance = K - (a_list[N-1]-a_list[0])
print(K - max_distance)

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


a = list(map(int,input().split()))	
maxdis = a[0]+K-a[-1]
alldis = a[0]+K-a[-1]
a.sort()
for i in range(1,N):
    alldis += a[i] - a[i-1]
    maxdis = max(maxdis,a[i]-a[i-1])
print(alldis-maxdis)

Solution (Python):
import numpy as np
k, n = map(int, input().split())

A = [int(a) for a in input().split()]
Abar = [0] + A
A.append(A[0]+k)
A = np.array(A)
Abar = np.array(Abar)
dis = A - Abar
print(k - max(dis))

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

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

maxV = max(maxV, K - A[N-1]+A[0])
print(ans - maxV)

Solution (Python):
"""時計回り／反時計回りのどちらで行くかを考えると終わるのでは
"""
K, N = (int(x) for x in input().split(' '))
A = [int(x) for x in input().split(' ')]

G = []
for i in range(len(A)-1):
  G += [A[i+1]-A[i]]

G += [(K - (A[N-1] - A[0]))]

print(sum(G)-max(G))