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

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

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

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

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

print(K-max(diff))

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

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

maxd = 0

b = 0
for a in As:
    maxd = max(maxd, a - b)
    b = a
maxd = max(maxd, K - As[-1] + As[0])

print(K - maxd)

Solution (Python):


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

max_d = -float("inf")
total_d = 0
for i, a in enumerate(a_list):
    if i == 0:
        d = (K - a_list[-1]) + a_list[i]
    else:
        d = a_list[i] - a_list[i - 1]
    total_d += d
    max_d = max(max_d, d)
ans = total_d - max_d

print(ans)

Solution (Python):
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()


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

value=10**9
value=min(value,-(A[0]-A[-1]))
for i in range(1,N):
    value=min(value,K-(A[i]-A[i-1]))
    
print(value)

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

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

res = al[-1] - al[0]
for i in range(1,n):
    r = k - al[i] + al[i-1]
    res = min(res, r)

print(res)

Solution (Python):
K,N = map(int, input().split())
a = [int(c) for c in input().split()]
b = [a[(i+1)%N]-a[i] for i in range(N-1)]
b += [a[0]+K-a[N-1]]
ans = K-max(b)
print(ans)

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

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


dist = 0
for x, y in zip(A, A[1:]):
    dist = max(dist, y-x)

dist = max(dist, A[0]+k-A[-1])

print(k-dist)

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

Solution (Python):
x,y=list(map(int, input().split()))
a=list(map(int, input().split()))
ans=0
for i in range(y-1):
	if ans<abs(a[i]-a[i+1]):
		ans=abs(a[i]-a[i+1])

if (x+a[0])-a[y-1]>ans:
	ans=(x+a[0])-a[y-1]

print(x-ans)

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

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

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

print(k - p)

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

print(k-mdist)

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

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

print(ans)

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]
    if i == n-1:
        D[i] = A[0]+k-A[i]


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

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

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

k,n = get_nums_l()
aaa = get_nums_l()
for i in range(n):
    aaa.append(aaa[i] + k)

ans = 99999999999999999999
for i in range(n):
    c = aaa[i+n-1] - aaa[i]
    ans = min(ans, c)

print(ans)

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

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

K, N = MAP()
A = LIST()

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

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

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

print(k - diff)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

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

dist = 0
start = -1
for i in range(N - 1):
    a = A[i + 1]
    pre = A[i]
    if dist < a - pre:
        dist = a - pre
        start = i

if dist < A[0] + (K - A[N - 1]):
    dist = A[0] + (K - A[N - 1])
    start = N - 1

print(K - dist)

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

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

print(K-distance_max)

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

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

tmp = (K - A[-1]) + A[0]
maximum = max(tmp, maximum)

print (K - maximum)

Solution (Python):
import numpy as np

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

D = np.diff(A)

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

Solution (Python):

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

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

dist = As[0] + (K - As[-1])
for i in range(len(As) - 1):
    dist = max(dist, As[i+1] - As[i])

print(K - dist)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    K, N = LI()
    A = LI()
    ans = inf
    for i in range(len(A) - 1):
        dist = A[i+1] - A[i]
        ans = min(ans, K - dist)
    dist = A[0] + K - A[-1]
    ans = min(ans, K - dist)
    print(ans)

main()

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

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

Solution (Python):
import sys 
MOD = 10 ** 9 + 7
INF = 10**11
from heapq import heapify,heappush,heappop

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

if __name__=='__main__':
    main()

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

K, N = lr()
A = np.array(lr())
A = np.append(A, A[0] + K)
diff = np.diff(A)
answer = K - diff.max()
print(answer)

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.append(A[i+1] - A[i])

print(K - max(diff))

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

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

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

print(k - maxD)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

a,b = m()

ppp = l()



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

for i in range(b):
    if i != b-1:
        dp[i] = abs(ppp[i+1]-ppp[i])
    else:
        dp[i] = abs(a-ppp[-1]+ppp[0])

print(sum(dp)-max(dp))

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

dmax=0
a_old=-(k-a_list[-1])
for a in a_list:
    d=a-a_old
    if d > dmax:
        dmax=d

    a_old=a

print(int(k-dmax))

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

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

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

d=[(k-a[-1]+a[0])]
for i in range(1,len(a)):
    d.append(a[i]-a[i-1])

print(k-max(d))

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

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

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

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

delta_list = []

for a, b in zip(A[:-1], A[1:]):
    delta = b - a
    delta_list.append(delta)

delta_list.append(k - A[-1] + A[0])

candidates = [k-delta for delta in delta_list]
print(min(candidates))

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


ans = 10**12

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

print(ans)

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

Solution (Python):

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

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

print(K - max(diffs))

Solution (Python):
from sys import stdin, stdout 
from math import *
import itertools
import sys

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

d = max(d, k - a[-1] + a[0])
print(k - d)

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

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

import numpy as np

d = np.diff(a)
d = np.append(d, k - a[-1] + a[0])

print(np.sum(np.sort(d)[:-1]))

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

d = 0
for i in range(0,len(A)-1):
    diff = A[i+1]-A[i]
    if diff > d:
        d = diff
diff = k-A[-1]+A[0]
if diff > d:
    d = diff
    
print(k-d)

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

ans=10**8
B=[]
for i in range(1,n):
    B.append(A[i]-A[i-1])
B.append(k-A[-1]+A[0])
print(k-max(B))

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

d = []

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

print(K-max(d))

Solution (Python):
import sys

n, k, *a = map(int, sys.stdin.read().split())
a.append(n + a[0])

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

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if self.v is None:
            self.v = n
        else:
            self.v = self.f(self.v, n) 


def main():
    K, N = read_values()
    A = read_list()
    A += [K + A[0]]
    B = [A[i + 1] - A[i] for i in range(N)]
    print(K - max(B))


if __name__ == "__main__":
    main()

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

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

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

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

Solution (Python):
K,N= map(int,input().split())
A= list(map(int,input().split()))
A = A + [A[0]+K]
diff = [b-a for a,b in zip(A,A[1:])]
mA = max(diff)
print(K-mA)

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

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

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

if maxi<=a[0]+(k-a[-1]):
    print(max(a)-min(a))
else:
    print(k-maxi)

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
K, N = [int(x) for x in input().strip().split()]
A = [int(x) for x in input().strip().split()]
A2 = [0] * len(A)
for i, a in enumerate(A[1:], start=1):
    A2[i] = A[i] - A[i-1]
A2.append(A[0] + K - A[-1])
print(K - max(A2))

Solution (Python):
def main():

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

    for v in dist:
        ans = min(ans, K - v)
    return ans


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

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

Solution (Python):

import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    K,N = LMIIS()

    A = LMIIS()
    D = []
    pre = 0
    max_diff = A[0] + K - A[-1]
    for ai in A:
        max_diff = max(max_diff,ai-pre)
        pre = ai
    
    print(K-max_diff)

    

if __name__ == '__main__':
    main()

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

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

al = sorted(al)

ans = 0

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

ans = max(al[0] + (k - al[-1]) , ans)

print(k-ans)

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

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

B = []

for i in range(b):
    begin = A[i]
    
    if i == 0:
        end = A[-1+i]
    else:
        end = A[-1+i] + a
    B.append(end - begin)
print(min(B))

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

ans = 10**16

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

tmp = K-(K+A[0]-A[N-1])
ans = min(tmp,ans)

print(ans)

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

ans = As[0] + (K-As[-1])
for i in range(N-1):
    dist = As[i+1] - As[i]
    ans = max(ans, dist)

print(K-ans)

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

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

max_dist = 0
for i in range(n):
    nxt = (i+1) % n
    distance = a[nxt] - a[i]
    if distance < 0:
        distance += k
    max_dist = max(max_dist, distance)

print(k-max_dist)

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

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,boundary=1,search=[],replacement_of_found='.',mp_def={'

    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):return [[tb//(n**bt)%n for bt in range(k)] for tb in range(n**k)]
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
def show2d(g,h,w):
    for i in range(h):show(g[i*w:i*w+w])

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase

input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0
k,n=LI()
a=LI()
a+=[a[0]+k]
b=[a[i+1]-a[i] for i in range(n)]

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

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

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

print(k-maxi)

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

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

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

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

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

Solution (Python):



def minimize(k, n, bs):
    deltas = [bs[i + 1] - bs[i] for i in range(n - 1)]
    deltas.append(bs[0] - bs[-1] + k)
    return k - max(deltas)


def main():
    k, n = (int(z) for z in input().split())
    bs = [int(b) for b in input().split()]
    res = minimize(k, n, bs)
    print(res)


if __name__ == "__main__":
    main()

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

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    B = A+[A[i] + K for i in range(N)]
    answer = 10**9
    for i in range(N):
        answer = min(answer,B[i+N-1]-B[i])
    print(answer)
    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 = list(map(int,input().split()))
A = list(map(int,input().split()))
distances = [float('inf')]*N
distances[0] = A[0]+(K-A[-1])
for i in range(1,N):
    distances[i] = abs(A[i]-A[i-1])
print(K-max(distances))

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

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

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

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

A = [a % K for a in A]
A.sort()

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

print(K - max(b))

Solution (Python):


k,n = map(int, input().split())
A = list(map(int, input().split()))
x = []
for a in A:
    x.append(a%k)
x.sort()
t = 0
for i in range(len(x)-1):
    t = max(t, x[i+1]-x[i])
t = max(t, k-x[-1]+x[0])
print(k-t)

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

tmp = K-abs(A[0]-A[-1])

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

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

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

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

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

print(K-maxDis)

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

dif_list = []

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

print(k-max(dif_list))

Solution (Python):

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

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

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

d_list = []
prev = A_list[-1] - K
for a in A_list:
	d_list.append(a - prev)
	prev = a

print(sum(d_list) - max(d_list))

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

a = sorted(a)

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

print(k-mins)

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

Solution (Python):

from fractions import gcd
import math

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    k,n = mi()
    a = li()

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


main()

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])
ans=sum(B)-max(B)
print(ans)

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

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

ls = ls + [a+l for a in ls]

ans = l+1
for i in range(n):
  ans = min(ans,ls[i+n-1]-ls[i])
print(ans)

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

print(K-max(L))

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):
    dis = A[i-1]+K-A[i]
    ans = min(ans,dis)
print(ans)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    K, N = lr()
    A = lr()
    ans = 0
    for i in range(N-1):
        ans = max(A[i+1]-A[i], ans)
    ans = max(K-A[N-1]+A[0], ans)
    print(K-ans)
resolve()

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

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

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

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

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

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

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

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

print(main())