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 numpy as np

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

B = []
for i in range(1, len(A)):
    B.append(A[i] - A[i-1])

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

max_dist = np.max(B)

print(K - max_dist)

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(len(A)-1)]
B.append(K-A[-1] + A[0])


print(K - max(B))

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

A.sort()
A += [K+A[0]]

delta = [A[i+1]-A[i] for i in range(N)]
print(K-max(delta))

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

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

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

print(K-max(L))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]




k,n=mii()
arr0=limii()
p=k+arr0[0]
arr=arr0+[p]

ans=0
arr.sort()
for i in range(n):
    ans=max(ans,arr[i+1]-arr[i])

print(k-ans)

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


if __name__ == '__main__':
    main()

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

distance = [A[i+1]-A[i] for i in range(len(A)-1)]
distance = [K-A[-1]+A[0]] + distance

min_distance = max(distance)
print(K-min_distance)

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

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

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

print(k-ma)

Solution (Python):


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



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

ans = K - max_distance
print(ans)

Solution (Python):


MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

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

m = K-A[len(A)-1] + A[0]

index = 0

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

ans = K - m
print(ans)

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

span = []

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

print(K-max(span))

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

df = [None] * n

for i in range(n - 1):
    df[i] = an[i + 1] - an[i]

df[n - 1] = k - an[n - 1] + an[0]

print(sum(df) - max(df))

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

Solution (Python):
K, N = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
maximum = 0
for i in range(N):
  if i == 0:
    continue
  elif maximum < A[i]-A[i-1]:
    maximum = A[i]-A[i-1]
if maximum < A[0]+(K-A[N-1]):
  maximum = A[0]+(K-A[N-1])

print(K-maximum)

Solution (Python):

K, N = [int(s) for s in input().split()]
A_list = [int(s) for s in input().split()]

d_list = []
for i, A in enumerate(A_list):
    if i == N - 1:
        d = K - A_list[i] + A_list[0]
    else:
        d = A_list[i + 1] - A_list[i]
    d_list.append(d)

d_sum_min = K - max(d_list)
print(d_sum_min)

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

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

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

print(K-longest)

Solution (Python):
mod = 10**9 + 7

def iip(listed = False):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod



def main():
    K, N = iip()
    A = iip()
    A.sort()

    dis = []
    cur = -(K - A[-1])
    for i in A:
        d = i - cur
        cur = i
        dis.append(d)

    md = max(dis)
    print(K-md)


    pass

if __name__ == "__main__":
    main()

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    D = []
    for i in range(1,len(A)):
        D.append(abs(A[i]-A[i-1]))
    D.append(A[0]+K-A[-1])
    print(sum(D)-max(D))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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

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



if __name__ == "__main__":
    c()

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

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

print(sum(dist) - max(dist))

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

list_Y = list(map(int, input().split()))
addY = list_Y[0] + M
list_Y.append(addY)

max_Y = 0 

for i in range(N):
  kyori = list_Y[i+1] - list_Y[i]
  if kyori >= max_Y:
    max_Y = kyori
    
print(M - max_Y)

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

Solution (Python):
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools


a = [int(s) for s in stdin.readline().rstrip().split()]
K = a[0]
N = a[1]
A = [int(s) for s in stdin.readline().rstrip().split()]

diff = []
for el in range(len(A)):
    if el == len(A) - 1:
        diff.append(K-A[el]+A[0])
        break
    diff.append(A[el+1]-A[el])

print(K-max(diff))

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

lens = []
max_len = 0
for i in range(len(A)):
  if i == len(A) - 1:
    dist = K - A[i] + A[0]
  else:
    dist = A[i+1] - A[i]
  lens.append(dist)
print(sum(lens) - max(lens))

Solution (Python):
k,n = map(int, input().split())
a = list(map(int, input().split()))
max_diff = -float("inf")
ind = 0
for i in range(n):
    if i == n-1:
        x = (k-a[i]) + a[0]
        if x > max_diff:
            max_diff = x
            ind = i
    else:
        x = a[i+1] - a[i]
        if x > max_diff:
            max_diff = x
            ind = i
dist = 0
for i in range(n):
    if i == ind:
        continue
    if i == n-1:
        dist += (k-a[i])+a[0]
    else:
        dist += a[i+1] - a[i]
print(dist)

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

Solution (Python):
import numpy as np

k, n = map(int, input().split())
A = list(map(int, input().split()))
A = np.array(A + [k + A[0]])
d = A[1:] - A[:-1]
print(np.sum(d) - np.max(d))

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

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

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

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

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

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

maxLen = 0
A.sort()
for i in range(len(A)-1):
    l = A[i+1] - A[i]
    if l > maxLen:
        maxLen = l 
l = (A[0]+K) - A[i+1]
if l > maxLen:
    maxLen = l 

print(K-maxLen)

Solution (Python):
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect




from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
input = sys.stdin.readline

sys.setrecursionlimit(2147483647)

def main():
  k, n = map(int, input().split())
  A = list(map(int, input().split()))
  A.sort()
  result = 0
  for i in range(n-1):
    result = max(result, A[i+1]-A[i])
  ans = max(result, (k+A[0])-A[n-1])
  print(k-ans)

if __name__ == '__main__':
  main()

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

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

print(k - maximum)

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

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):
    long_ = a[i+1] - a[i]
    if long_ >= long:
        long = long_
        
print(k - long)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



K,N = LI()
A = LI()

A.sort()

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

if K-(A[N-1]-A[0]) > x:
    x = K-(A[N-1]-A[0])

print(K-x)

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

Solution (Python):
import sys
from pprint import pprint

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

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

print(K - max(d))

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

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


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



print(k-max_l)

Solution (Python):
import numpy as np
K, N = (int(i) for i in input().split())  
A = [int(i) for i in input().split()] 

A.append(A[0] + K)
lis = sorted(list(np.diff(A)))
print(sum(lis[:-1]))

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

x = np.array(a)
a1 = x[1:] - x[:-1]

ans = list(a1)
ans.append((k-a[-1])+a[0])

print(int(k-max(ans)))

Solution (Python):
import numpy as np

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

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

a_l = np.array(a_l)

b_l = np.empty(a_l.shape[0])

b_l[1:] = a_l[:-1]

b_l[0] = a_l[-1]

c_l = a_l - b_l

c_l[0] = K - a_l[-1] + a_l[0]

print(int(K - np.max(c_l)))

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

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

ans = K - max_d
print(ans)

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

arr.sort()

distances = [(arr[(i + 1) % n] - arr[i]) % k for i in range(len(arr))]

print(k - max(distances))

Solution (Python):




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


interhouse = [0] * n


for i in range(n-1):
    interhouse[i] = a[i+1] - a[i]

interhouse[n-1] = k - a[n-1] + a[0]

ans = k - max(interhouse)


print(ans)

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

Solution (Python):
def input_multiple_number():
    return map(int, input().split())

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


K ,N = input_multiple_number()
listA = input_multiple_number_as_list()




diffy=[0]*(len(listA))

bif = 0
for d in range(len(diffy)):
	diffy[d] = listA[d]-bif
	bif = listA[d]
diffy[0] += (K-listA[-1])

maxx = 0
for a in diffy:
	maxx = max(a,maxx)

print(K-maxx)

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    mx = 0
    for i in range(len(A) - 1):
        if A[i + 1] - A[i] > mx:
            mx = A[i + 1] - A[i]
    if A[0] + K - A[-1] > mx:
        mx = A[0] + K - A[-1]
    print(K - mx)




def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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

ld = []
ld.append(k - la[-1] + la[0])
for i in range(1, n):
    ld.append(la[i] - la[i-1])

ld.sort()

print(k - ld[-1])

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

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

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

ans = sum(d) - max(d)

print(ans)

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


between_house = list()
between_house = [A_list[i+1]-A_list[i] for i in range(N-1)]
between_house.append((K - A_list[-1])+A_list[0])

print(K-max(between_house))

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

maxdis = 0
for i in range(n):
  if i == 0:
    continue
  if As[i] - As[i-1] > maxdis:
    maxdis = As[i] - As[i-1]
if As[0] + k - As[n-1] > maxdis:
  maxdis = As[0] + k - As[n-1]
print(k - maxdis)

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

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

ans=k-max(b)
print(ans)

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    max = K - A[N-1] + A[0]
    for i in range(N-1):
        p = A[i+1] - A[i]
        if p > max:
            max = p
    print(K-max)

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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(K-A[-1] + A[0])
ans = sum(B) - max(B)
print(ans)

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

k = inputs1[0]
n = inputs1[1]

b = [0 for i in range(n)]

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

b[n-1] = k - a[n-1] + a[0]

m = max(b)

print(k-m)

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

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

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

D = max(B)
print(K-D)

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

ans = X[0] + (k - X[n-1])
for i in range(n-1):
    _a = X[i+1] - X[i]
    ans = max(ans, _a)

print(k - ans)

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

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

dA = [A[i+1]-A[i] for i in range(n-1)]
dA += [k- A[-1]+A[0]]

print(sum(dA) - max(dA))

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

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

print(min(k-x,a[n-1]-a[0]))

Solution (Python):
k,n=map(int, raw_input().split())
houses=map(int, raw_input().split())
maxd=0
l=-1
r=-1
for i in xrange(1, n):
    maxd=max(maxd, houses[i]-houses[i-1])

maxd=max(maxd, k-houses[-1]+houses[0])

print k-maxd

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    d = 0
    for i in range(0, N):
        if i + 1 == N:
            next = K - A[i] + A[0]
        else:
            next = A[i + 1] - A[i]
        d = max(d, next)
    print(K - d)
        
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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

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

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

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

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

k,n = map(int,input().split())
ai = [int(i) for i in input().split()]

sukima = ai[0] + k - ai[n-1]

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

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

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

for i in range(1,N):
  B.append(A[i]-A[i-1])
B.append(C)
ans=K-max(B)
print(ans)

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

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

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

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

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

print(K-dis)

Solution (Python):

if __name__ == '__main__':
    k, n = map(int, input().split())

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

    for i in range(n):
        A.append(A[i] + k)

    
    ans = 10000000000
    for i in range(n):
        
        if (ans > A[i + n - 1] - A[i]):
            ans = A[i + n - 1] - A[i]

    print(ans)

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

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

print(K-a)

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

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

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

s2 = []
for i in range(N-1):
  s2.append(s[i+1] - s[i])
s2.append(s[0]+L - s[N-1])

s2 = sorted(s2)

print(sum(s2[:-1]))

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

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

diff = [0] * n
for i in range(len(a) - 1):
    diff[i] = abs(a[i] - a[i+1])
diff[-1] = abs(a[-1] - k) + a[0]

print(k - max(diff))

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

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

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

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

D = 0

for idx, a in enumerate(A):
    d = K + a - A[idx-1] if idx == 0 else a - A[idx-1]
    if D < d:
        D = d
print(K-D)

Solution (Python):
d = input().split(" ")
k = int(d[0])
n = int(d[1])
a = input().split(" ")
for i in range(n):
	a[i] = int(a[i])
a.sort()
p = 0
for i in range(n-1):
	c = a[i+1] - a[i]
	if c > p:
		p = c
e = a[0] + k - a[n-1]
if e > p:
	p = e
print(k - p)

Solution (Python):
import sys

max_dist = -1

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

for i in range(n):
	if i == n - 1:
		max_dist = max(max_dist, p + a[0] - a[i])
	else:
		max_dist = max(max_dist, a[i + 1] - a[i])

print(p - max_dist)

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    max_len = 0
    for i in range(len(A)):
        if i == 0:
            max_len = A[0] + K - A[N-1]
        else:
            if A[i] - A[i-1] > max_len:
                max_len = A[i] - A[i-1]
    print(K - max_len)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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

def main():
    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)


if __name__ == '__main__':
    main()

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

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

Solution (Python):
inp = list(map(lambda x: int(x), input().split(" ")))
K = inp[0]
N = inp[1]
inp = list(map(lambda x: int(x), input().split(" ")))

diffs = []

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

print(sum(diffs)-max(diffs))

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

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

max_distance = 0

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


distance = A[0]+(K-A[N-1])
max_distance = max(distance,max_distance)

print(K-max_distance)

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

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

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

b=sorted(b)
print(sum(b[:n-1]))

Solution (Python):

"""
Created on Sat Mar 28 21:05:47 2020

@author: H_Hoshigi
"""

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

A_list.append(A_list[0]+K)


dA_list = [A_list[i+1]-A_list[i] for i in range(N)]

print(K-max(dA_list))

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

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

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

a = [0 for i in range(N)]
amax = 0
for i in range(N):
    a[i] = A[i] - A[i-1]
    if a[i] < 0:
        a[i] += K
    if a[i] > amax:
        amax = a[i]

print(K-amax)

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

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

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

Solution (Python):
l,n = map(int,input().split())
input_list = list(map(int, input().split()))
maxlens = l-input_list[-1]+input_list[0]

for i in range(1,n):
  	maxlens = max(maxlens,input_list[i]-input_list[i-1])

print(l-maxlens)

Solution (Python):
import sys
import math
from collections import defaultdict
k,n=map(int,sys.stdin.readline().split())
arr=list(map(int,sys.stdin.readline().split()))
dis=-1
for i in range(n-1):
    d=arr[i+1]-arr[i]
    dis=max(dis,d)
d=(k-arr[-1]+arr[0])
dis=max(dis,d)
print(k-dis)

Solution (Python):
import sys








import math



readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))

    ans = INF

    temp1 = a[-1] - a[0]
    temp2 = k - (a[1] - a[0])
    ans = min(ans, temp1, temp2)

    for i in range(1, n - 1):
        temp1 = k - (a[i + 1] - a[i])
        temp2 = k - (a[i] - a[i - 1])
        ans = min(ans, temp1, temp2)

    temp1 = k - (a[-1] - a[-2])
    temp2 = a[-1] - a[0]
    ans = min(ans, temp1, temp2)

    print(ans)


if __name__ == '__main__':
    main()

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

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

if __name__ == '__main__':
    resolve()

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
d = list(map(lambda x: x[1]-x[0], zip(a, a[1:])))
first_and_last = a[0] + (k - a[-1])
ignore = max(d+[first_and_last])
ans = k - ignore
print(ans)

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


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

print(sum(d)-max(d))

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
dist = []
for i in range(K):
    if i == K-1:
        d = a[0] - 0 + N - a[i]
    else:
        d = a[i+1] - a[i]
    dist.append(d)
dist.sort()
dist.pop(-1)
print(sum(dist))