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= list(map(int, input().split()))
a = list(map(int, input().split()))
max = 0
for i in range(len(a)):
  if i==len(a)-1:
    diff = k-a[i]+a[0]
  else:
    diff = abs(a[i]-a[i+1])
  if max < diff:
    max = diff
print(k-max)

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

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

distance_list.sort()

interval_list = []

a = 0
for i in  range(n):
    interval_list.append(distance_list[i] - a)
    a = distance_list[i]

b = k - distance_list[n-1]

interval_list[0] = interval_list[0] + b

interval_list.sort()

ans = 0
for i in range(n-1):
    ans = ans + interval_list[i]

print(ans)

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

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

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

e = [0 for _ in range(n)]
for i in range(1, n):
    e[i] = a[i] - a[i-1]
e[0] = k + a[0] - a[-1]
print(k - sorted(e, reverse=True)[0])

Solution (Python):
from sys import stdin

def main():

    input = stdin.readline

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

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

    max_blank = 0

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

    print(K - max_blank)


if __name__ == "__main__":
    main()

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

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

def main():
    K,N = map(int,input().split())
    a = list(map(int,input().split()))
    
    a = [a[-1]-K] + a + [a[0]+K]
    
    ans = K
    
    for i in range(N):
        use = max(a[i+1]-a[i],a[i+2]-a[i+1])
        ans = min(ans,K-use)
    
    print(ans)
    
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):
    tmp = A[i+1] - A[i]
    if tmp > max_:
        max_ = tmp
if max_ > A[0]+(K-A[-1]):
    print(K-(max_))
else:
    print(K-(A[0]+(K-A[-1])))

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(1,n):
  d=max(d,a[i]-a[i-1])
print(k-d)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)  
to_index = lambda x: int(x) - 1  
print_list_in_2D = lambda x: print(*x, sep="\n")  


def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())


def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

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


def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

K, N = MII_split()
A_list = LII_split()

max = 0
for i in range(len(A_list)):
    if i + 1 < len(A_list):
        dist = abs(A_list[i] - A_list[i + 1])
    else:
        dist = abs(K - A_list[i] + A_list[0])
    if dist > max:
        max = dist

print(K - max)

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

Solution (Python):
n,k = map(int,input().split())
l = sorted(map(int,input().split()))
x=n*2
for i in range(k):
  x=min(x,n-(l[(i+1)%k]-l[i])%n)
print(x)

Solution (Python):

"""
Created on Sat Mar 28 21:11:28 2020

@author: Kanaru Sato
"""

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

for i in range(n-1):
    d = a[i+1]-a[i]
    if d > k/2:
        d = k-d
    dl.append(d)

d = a[n-1]-a[0]
if d > k/2:
    d = k-d
dl.append(d)

ans = k-max(dl)

print(ans)

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

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])
c=max(b)
print(k-c)

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

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

maxdist = distlist[0] + K - distlist[N-1]
for i in range(0, N-1):
  dist2houses = distlist[i+1] - distlist[i]
  if (dist2houses > maxdist):
    maxdist = dist2houses

print(K - maxdist)

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

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

Solution (Python):
n = list(map(int,input().split()))
m = list(map(int,input().split()))
l = [0,0]
for i,j in enumerate(m):
    if i == 0:
        l[0] = i
        l[1] = j-0+n[0]-m[n[1]-1]
    else:
        if l[1] < m[i]-m[i-1]:
            l[0] = i
            l[1] = m[i]-m[i-1]
print(n[0]-l[1])

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

ans = a[n-1] - a[0]

for i in range(len(a)):
  a[n - 1 - i] -= k
  new = -(a[n - 1 - i]) + a[n - 2 - i]
  
  if new < ans:
    ans = new
print(ans)

Solution (Python):
inps = input().split()
k = int(inps[0])
n = int(inps[1])
ap = input().split()
ap = [int(s) for s in ap]
a = []
for i in range(n):
    if i == 0:
        a.append(ap[0] + k - ap[n - 1])
    else:
        a.append(ap[i] - ap[i-1])
print(k - max(a))

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

sorted(A)
A.append(A[0]+K)
ls = []

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

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

k, n = LI()
A = LI()
ret = k - A[n - 1] + A[0]
for i in range(n - 1):
    ret = max(ret, A[i + 1] - A[i])

print(k - ret)

Solution (Python):

import math
import itertools
import statistics


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

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

dif = 0
dif_index = -1
for i in range(n-1):
    if A[i+1]-A[i] >= dif:
        dif = A[i+1]-A[i]
        dif_index = i+1

if A[0] > 0:
    if A[0]+(k-A[-1]) >= dif:
       dif =  A[0]+(k-A[-1])

else :
    if k-A[-1] >= dif:
        dif = k-A[-1] 
        dif_index = 0

print(k-dif)

Solution (Python):

K, N = map(int, input().split())
S = input().split()
A = [0 for i in range(N)]
for i in range(N):
    A[i] = int(S[i])
max = 0
for i in range(N):
    if i != N-1:
        D = A[i+1]-A[i]
    else:
        D = K-A[i]+A[0]
    if max < D:
        max = D
print(K-max)

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

kyori = 0
if A[0] == 0:
    kyori = k-A[-1]
else:
    kyori = k-(A[-1]-A[0])

for i in range(n-1):
    kyori = max(kyori, A[i+1]-A[i])
print(k-kyori)

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

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

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

tmp = max(tmp, gyaku)

print(K - tmp)

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

for n, A in enumerate(A_list):
    if (n != (N - 1)) and (n != 0):
        D  = max(abs(A - A_list[n - 1]), abs(A - A_list[n + 1]))
    elif n == (N - 1):
        D = abs(A - A_list[n - 1])
    else:
        D = max(abs(A +(K - A_list[n - 1])), abs(A - A_list[n + 1]))
    if M < D:
        M = D


print(K - M)

Solution (Python):
import numpy as np


def main():
    k, n = [int(v) for v in input().split(' ')]
    a = np.array([int(v) for v in input().split(' ')])
    d = np.zeros(a.shape)
    d[1:] = a[1:] - a[:-1]
    d[0] = k - a[-1] + a[0]
    ans = k - np.max(d)
    print(int(ans))


if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):



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


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

lmax = 0

for a in range(N):
  if a != N - 1:
    if A[a + 1] - A[a] > lmax:
      lmax = A[a + 1] - A[a]
  else:
    if A[0] + K - A[a] > lmax:
      lmax = A[0] + K - A[a]

print(K - lmax)

Solution (Python):
k,n=map(int,raw_input().split())
a=map(int,raw_input().split())
s=0
m=a[1]-a[0]
for i in range(1,n):
	d=a[i]-a[i-1]
	s+=	d
	m=max(m,d)

m=max(m,k-a[-1]+a[0])
s+=(k-a[-1])+a[0]

print s-m

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

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

A.sort()
max_distance = A[0] + K-A[-1]

old = None
for a in A:
    if not old:
        old = a
        continue
    distance = a - old
    if distance > max_distance:
        max_distance = distance
    old = a

answer = K - max_distance
print(answer)

Solution (Python):

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

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


maxv = -float('inf')

for i in range(len(arr) - 1):
    maxv = max(maxv, arr[i + 1] - arr[i])

maxv = max(maxv, k - arr[-1] + arr[0])

print (k - maxv)

Solution (Python):
from math import pi
K, N = map(int, input().split())
A = list(map(int, input().split()))
D = [0 for i in range(N)]
D[0] = abs(A[0] + (K - A[-1]))
for i in range(1, N):
  D[i] = abs(A[i] - A[i-1])
print(sum(D) - max(D))

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

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

print(sum(c[1:]))

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

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

print(k - max(b))

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

distance = [k] * n

for i in range(n - 1):
  distance[i] -= houses[i + 1] - houses[i]

distance[n - 1] -= k - houses[n - 1] + houses[0]

print(min(distance))

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

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

ans = -1
for i in range(len(input_list)-1):
  ans = max(ans, input_list[i+1] - input_list[i])

ans = max(ans, input_list[0] + K - input_list[-1])
print(K - ans)

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

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

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

D = max(D, K + A[0] - A[-1])
print(K - D)

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

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

Solution (Python):
k, n = map(int,input().split())
a = list(map(int,input().split()))
min_length = a[n-1]-a[0]
for ind in range(n-1):
    cur_length = k -a[ind+1] + a[ind]
    if cur_length < min_length:
        min_length = cur_length
print(min_length)

Solution (Python):

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

print(sum(diff_A)-max(diff_A))

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

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

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

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

tot = A[0] + k - A[-1]
length = tot
for i in range(1, n):
    now = A[i] - A[i-1]
    tot += now
    length = max(length, now)

ans = tot - length
print(ans)

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

A.sort()

max_dis = A[0] + K - A[-1]
max_disidx = 0

for i in range(1, N):
    distance = A[i] - A[i-1]
    if max_dis < distance:
        max_dis = distance
        max_disidx = i

print(K-max_dis)

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

def calc_add(N):
    return N + k

A_2 = list(map(calc_add, A_1))

A_total = A_1 + A_2

min_d = k

i = 0
while i < n:
    if A_total[i + n -1] - A_total[i] <= min_d:
        min_d = A_total[i + n -1] - A_total[i]
    
    i += 1

print(min_d)

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

D=[]
for i in range(len(A)):
    if i!=len(A)-1:
        d=A[i+1]-A[i]
    else:
        d=K-A[i]+A[0]
    D.append(d)

D.sort()
D.pop()


print(sum(D))

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

arr=[]

for i in range(n-1):
    arr.append(l[i+1]-l[i])

arr.append(l[0]+k-l[n-1])

m=max(arr)
print(k-m)

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

c=a+b
ans=10**9
for i in range(n+1):
  ans=min(ans,c[i+n-1]-c[i])
print(ans)

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

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

total = k

minimum = 10000000000000
for i in range(n):
    if i == 0:
        dist = total - ((k - as_[-1]) + as_[0])
    else:
        dist = total - (as_[i] - as_[i - 1])

    minimum = min(minimum, dist)

print(minimum)

Solution (Python):
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
maxx = 0
key = 0
for i in range(0, n):
  if i == n-1:
    key = (k + a[0]) - a[i]
  else:
    key = a[i+1] - a[i]
  if maxx < key:
    maxx = key
print(k - maxx)

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

max_a = 0
before_a = A[0]
for i in range(1, N):
    if A[i]-before_a > max_a:
        max_a = A[i]-before_a
    before_a = A[i]

if (K-A[N-1]+A[0]) > max_a:
    print(A[N-1]-A[0])
else:
    print(K-max_a)

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

	else:
		delta.append(a[i]-a[i-1])
print(sum(delta)-max(delta))

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

print(k - ans)

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

print(K-max(C))

Solution (Python):
import sys
import math
import numpy

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

sorted(A)

total = K

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


print(total)

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

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

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

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

print(K-maxlen)

Solution (Python):
import sys,math
sys.setrecursionlimit(10**9)
from collections import defaultdict
 
IA = lambda: map(int,input().split())
IAS= lambda: map(str,input().split())


L, n=IA()
a=list(IA())
a.sort()
cha=[0 for i in range(n)]

cha[0]=a[0]+L-a[n-1]

summ=cha[0]
maxx=cha[0]

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

print(summ-maxx)

Solution (Python):
import sys

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

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

minimum_distance = K - max(distances)
print(minimum_distance)

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

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

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

print(sum(dis)- max(dis))

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

Solution (Python):
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') 
def gcd(a,b):return fractions.gcd(a,b) 
def lcm(a,b):return (a*b) // fractions.gcd(a,b) 
def iin(): return int(sys.stdin.readline()) 
def ifn(): return float(sys.stdin.readline()) 
def isn(): return sys.stdin.readline().split() 
def imn(): return map(int, sys.stdin.readline().split()) 
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) 
def fmn(): return map(float, sys.stdin.readline().split()) 
def iln(): return list(map(int, sys.stdin.readline().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def fln(): return list(map(float, sys.stdin.readline().split())) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) 
def comb(l, n): return itertools.combinations(l, n) 
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) 
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 
def m_add(a,b): return (a+b) % MOD
def print_list(l): print(*l, sep='\n')

def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

K,N = imn()
A = iln()

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

if max_index == 0:
    print(A[-1] - A[0])
else:
    print(A[-1]-A[0]-max+diff)

Solution (Python):



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



diff_list = [A_LIST[i] - A_LIST[i-1] if i != 0 else A_LIST[0] + (K-A_LIST[N-1]) for i in range(N)]
print(K - max(diff_list))

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

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

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

min_dist = K
for i, Ai in enumerate(A):
  clockwise = (A[(i - 1) % N] - Ai) % K
  counter_clockwise = (K - (A[(i + 1) % N] - Ai)) % K
  if clockwise < min_dist:
    min_dist = clockwise
  if counter_clockwise < min_dist:
    min_dist = counter_clockwise

print(min_dist)

Solution (Python):
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    ans = float('inf')
    for i in range(N):
        if A[i] > A[i-1]:
            tmp = (K + A[i-1]) - A[i]
        else:
            tmp = A[i-1] - A[i]
        ans = min(ans, tmp)

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

    print(ans)



if __name__ == "__main__":
    main()

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

a.sort()
ans = a[n-1]-a[0]
for i in range(n):
    tmp = a[i-1]-a[i]
    tmp%=k
    ans = min(ans,tmp)
print(ans)

Solution (Python):
k, n = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
a.append(k + a[0])
tmp = 0
maxi = 0
end = 0
for i, aa in enumerate(a):
    l = aa - tmp
    if l > maxi:
        maxi = l
        end = i
    tmp = aa
print(k - (a[end] - a[end - 1]))

Solution (Python):
str1 = input()
k = int(str1.split()[0])
n = int(str1.split()[1])
stra = input().split()
a = [int(x) for x in stra]
maxlen = k - a[n - 1] + a[0]
for i in range(1, n):
    if a[i] - a[i - 1] > maxlen:
        maxlen = a[i] - a[i - 1]
print(k - maxlen)

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

la = 0

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

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

K, N = i_l()
A = i_l()
A = A + [x + K for x in A]

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

print(min(ans))

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

distance = []

A.sort()

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

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

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

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
l = len(a)
dis = [None] * l
for i in range(n - 1):
    dis[i] = a[i + 1] - a[i]
dis[-1] = a[0] - a[-1] + k

ans = sum(dis) - max(dis)
print(ans)

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

max_a = 0
before_a = A[0]
for i in range(1, N):
    if A[i]-before_a > max_a:
        max_a = A[i]-before_a
    before_a = A[i]

if (K-A[N-1]+A[0]) > max_a:
    print(A[N-1]-A[0])
else:
    print(K-max_a)

Solution (Python):
K_meter, N_ken = map(int, input().split())
A = list(map(int, input().split()))

diff = []

for i in range(N_ken - 1):
    d = A[i + 1] - A[i]
    diff.append(d)

diff.append(K_meter - A[N_ken - 1] + A[0])

summ = sum(diff)

ans = summ - max(diff)

print(ans)

Solution (Python):
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = sorted(A)
C = [] * N
L = 0
for i in range(N):
    if i == 0:
        x = K - B[N - 1] + B[0]
    else:
        x = B[i - 1] - B[i]
    y = abs(x)
    if L < y:
        L = y
ans = K - L
print(ans)

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

b.append(k+a[0]-a[-1])
print(k-max(b))

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

Solution (Python):
def main():
    import numpy as np
    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    b = np.array(a + [a[-1]+k-a[-1]+a[0]])
    c = np.diff(b)
    ans = sum(c) - max(c)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
k,n = map(int,input().strip().split())
ls = list(map(int,input().strip().split()))
mini1 = abs(ls[-1]-ls[0])
if n>1:
    mini_cons = -1
    sind = -1
    for i in range(1,n):
        ans = ls[i]-ls[i-1]
        if ans>mini_cons:
            sind = i-1
            mini_cons = ans
    if sind!=-1:
        mini1 = min(ls[sind]+k-ls[sind+1],mini1)
print(mini1)

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

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

Solution (Python):
import numpy as np
k, n = map(int, input().split())
a = np.array(list(map(int, input().strip().split())))
d = np.zeros(n,dtype=np.int64)
d[1:] = a[1:] - a[:-1]
d[0] = a[0] + (k-a[-1])


m = 10**9
q = d[0:-1].sum()
for i in range(n):
    if i == 0:
        q = d[1:].sum()
    else:
        q = q + d[i-1] - d[i]

    if q < m:
        m = q

print(m)

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

distances = []
for i in range(N):
    if i == N-1:
        distances.append(array[0] + (K - array[i]))
    else :
        distances.append(abs(array[i] - array[i+1]))
        
print(K - max(distances))

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

max_diff = -1
for i in range(n-1):
    max_diff = max(max_diff, a_list[i+1] - a_list[i])
max_diff = max(max_diff, abs(k-a_list[-1] + a_list[0]))
print(k-max_diff)

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

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

mi = 10000000
for i in range(N):
  if i == 0:
    d = A[-1] - A[0]
  else:
    d = K - (A[i] - A[i - 1])
  mi = min(d, mi)
print(mi)

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

n, k = map(int, input().split())
a = list(map(int, sys.stdin.readline().split()))
ans = a[k-1] - a[0]
for i in range(1, k-1):
    if ans > n - (a[i+1] - a[i]):
        ans =  n - (a[i+1] - a[i])
print(ans)

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

Solution (Python):
import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

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

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

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

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

ans = sum(li_diff) - max(li_diff)
print(ans)

Solution (Python):


def solve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = max((A[i]-A[i-1])%K for i in range(N))
    res = K-B
    return str(res)

if __name__ == '__main__':
    print(solve())

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

Solution (Python):
k, n = (int(each) for each in input().split(' '))

a = [int(each) for each in input().split(' ')]
distance = []
for i in range(len(a)-1):
    distance.append(a[i+1]-a[i])

distance.append(a[0] + (k-a[-1]))

print(k - max(distance))