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):
def main():
    k, n = [int(i) for i in input().split()]
    pos = [int(i) for i in input().split()]
    res = k - pos[-1] + pos[0]
    for i, j in zip(pos[:-1], pos[1:]):
        if res < j -i:
            res = j-i
    print(k - res)


if __name__ == '__main__':
    main()

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

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

D = []

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

D = sorted(D)

m = max(D)

print(K - m)

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

Solution (Python):

import sys
import math
import numpy as np
import itertools

k,n = (int(i) for i in input().split())  


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

b = sorted(a)

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

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

print(k-maxd)

Solution (Python):
def C():
    K, N = map(int, input().split())
    A = [int(n) for n in input().split()]
    minimum_distance = A[-1] - A[0]
    for i in range(1, N):
        minimum_distance = min(minimum_distance, K - A[-i] + A[-i-1])
    print(minimum_distance)
            
C()

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

A+=[A[0]]
B=[None]*N
for i in range(0, N):
    if i<=N-2:
        B[i]=A[i+1]-A[i]
    if i==N-1:
        B[i]=A[i+1]+K-A[i]

print(sum(B)-max(B))

Solution (Python):
from operator import add
from functools import reduce

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

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

print(k - max(dists))

Solution (Python):


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

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

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

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

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

print(K - max_distance)

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

if __name__ == '__main__':
    main()

Solution (Python):


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

A0 = A[1:] + [A[0] + K]
dis = [a0 - a for (a, a0) in zip(A, A0)]
ans = K - max(dis)

print(ans)

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

a_before = a[0]
for i in a[1:]:
    tmp = i - a_before
    a_t.append(tmp)
    a_before = i
print(k-max(a_t))

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

alist.sort()


answer=K
for i in range(N):
  dist=(alist[(N-1+i)%N]-alist[i])%K
  
  answer=min(answer,dist)
  
print(answer)

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

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

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

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

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

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

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

print(K-max(diff))

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

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

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

print( K - d )

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

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, k-A[n-1]+A[0])
print(k-temp)

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

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

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

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

for i in range(len(a)):
    d = a[i] - a[i-1]
    if i == 0:
        d += k
    distances.append(d)
print(sum(distances) - max(distances))

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


arr = []

for i in range(N-1):
  a = List[i]
  b = List[i+1]
  arr.append(int(b-a))
  
arr.append(int(List[0]-0 + K - List[N-1]))

a = max(arr)
print(int(K-a))

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

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

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

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

B = [None] * (N - 1)
for i in range(N - 1):
    B[i] = A[i + 1] - A[i]

shuu = K
tmp = shuu - sum(B)
print(shuu - max(max(B), tmp))

Solution (Python):
K, N = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
M = 0
for i in range(N-1):
  t = l[i+1] - l[i]
  M = max(M, t)
t = K - l[-1] + l[0]
M = max(M, t)
print(K - M)

Solution (Python):
import sys,math,copy,queue,itertools
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
K,N = LI()
A = LI()
A = A + [A[0]+K]
ans = 0
for i in range(N):
    ans = max(ans,A[i+1]-A[i])
print (K - ans)

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

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

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

print(K - mx)

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

D = [j - i for i, j in zip(A, A[1:])] + [k-A[-1]+A[0]]

md = max(D)
ans = k - md

print(ans)

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

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

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

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

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

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

print(K - max_d)

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

da[n-1] = k - (a[n-1] - a[0])

da.sort()

print(sum(da[:n-1]))

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

m = 0
for i in range(n):
    t = abs(a[i] - a[i-1])
    m = max(m, min(t, abs(k-t)))

print(k-m)

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

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

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

print(sum(B)-max(B))

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

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

print(K - ans)

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

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

b = [0]*n

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

print(k-max(b))

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

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

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

for i in range(n-1):
    B.append(A[i+1]-A[i])
    
print(sum(B)-max(B))

Solution (Python):
def resolve():

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

    A.append(K+A[0])

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



resolve()

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

maxer=0
sum=0

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

print(sum-maxer)

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

Solution (Python):
import sys








def main():
    print(solve())


def solve():
    K, N, = map(int, input().split())

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

    pre = 0
    for i, a in enumerate(As):
        As[i] = a - pre
        pre = a

    As[0] = As[0] + (K - sum(As))

    return sum(As) - max(As)


if __name__ == '__main__':
    main()

Solution (Python):
K,N=map(int,input().split())
la=list(map(int,input().split()))
la.sort()
maxd=0
ans=0
for i in range(N):
    tmpd=(la[i]-la[i-1])%K
    ans+=tmpd
    if tmpd>maxd:
        maxd=tmpd
print(ans-maxd)

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

mx = 0
print(sum(diff) - max(diff))

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

dlst = []

n = AA[-1] - K
for a in AA:
    dlst.append(a - n)
    n = a
    
print(K-max(dlst))

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

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

print(ans)

Solution (Python):
K, N = map(int, input().split())
arr = list(map(int, input().split()))
tmp = arr[-1] - K
max_len = 0
for i in arr:
    max_len = max(max_len, i - tmp)
    tmp = i
print(K - max_len)

Solution (Python):


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

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

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

    elif i == N-1:
        dists.append((K-A[i]) + A[i-1])
        dists.append(A[i] - A[0])

    else:
        dists.append((K - A[i]) + A[i-1])
        dists.append(A[i] + (K - A[i+1]))
    
print(min(dists))

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

Solution (Python):
k,n=map(int,input().split())
l=list(map(int,input().split()))
d=0;j=l[0]
p=l[1:];p.append(j)
for s,f in zip(l,p):
    d=max(d,(f-s) if f>=s else k+f-s)
print(k-d)

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

aa_diff = [min(k - abs(aa[-1]-aa[0]), abs(aa[-1]-aa[0]))]
for a1, a2 in zip(aa[1:], aa[:-1]):
    aa_diff.append(min(k - abs(a1 - a2), abs(a1 - a2)))
print(k-max(aa_diff))

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

ans = K + 100
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()))
m = 0
for i in range(1,n):
  m = max(a[i]-a[i-1],m)
m = max((k+a[0])-a[n-1],m)
print(k-m)

Solution (Python):
def solve(k, n, a_list):
    res = a_list[n - 1] - a_list[0]
    a_list_s = list(sorted(a_list))
    for i in range(n - 1):
        res_a = k - (a_list_s[i + 1] - a_list_s[i])
        if res_a < res:
            res = res_a
    return res


def main():
    k, n = map(int, input().split())
    a_list = list(map(int, input().split()))
    res = solve(k, n, a_list)
    print(res)


def test():
    assert solve(20, 3, [5, 10, 15]) == 10
    assert solve(20, 3, [0, 5, 15]) == 10


if __name__ == "__main__":
    test()
    main()

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

if A[0]+K-A[-1]>=a:
    print(ans)
else:
    print(ans+A[0]+K-A[-1]-a)

Solution (Python):
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]

K, N = mi()

A = li()

m = 0

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

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

print(K-m)

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

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

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

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

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

print(s - mx)

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

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

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

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



print(k - max(D))

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

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

print(k-mx)

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


if __name__ == '__main__':
    resolve()

Solution (Python):
K, N = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
max_diff = 0
for i in range(N):
    diff = (A[i] - A[(i-1)%N]) % K
    if diff > max_diff:
        max_diff = diff
print(K - max_diff)

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

dist = [0]*n

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

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

ans = sum(dist) - max(dist)

print(ans)

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

Solution (Python):

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

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

print(k - l)

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

a = a + [ai + k for ai in a]

ans = float('inf')
for i in range(n):
    ans = min(ans, a[i+n-1] - a[i])

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

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

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 = 0
for i in range(N-1):
  a = max(a,A[i+1]-A[i])
a = max(a,A[0]+K-A[N-1])
ans = K - a
print(ans)

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

a.sort()
for x in a[:]:
    a.append(x + k)

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

print(len_min)

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

A=list(map(int,input().split()))
B=[]

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

print(k-max(B))

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

Solution (Python):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 

sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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


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

rA.sort()
rA[-1] = 0
print(sum(rA))

Solution (Python):

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

distance = [houses[i+1] - houses[i] 
            if i != N-1 else abs(houses[0] - (houses[N-1] - K))
            for i in range(N)]

print(K-max(distance))

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

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
pre = a[-1] - k
x = 0
for ai in a:
    x = max(x, ai - pre)
    pre = ai
print(k - x)

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

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(sum(dist)-max(dist))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

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

pre = l[0]
MAX = pre+K - l[-1]
for i in range(1,N):
    k = l[i]
    d = k - pre
    MAX = max(MAX,d)
    pre = k
print(K-MAX)

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

d_max = 0
tmp = 0

for a in A:
    d = a - tmp
    d_max = max(d_max, d)
    tmp = a

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

print(K - d_max)

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

Solution (Python):
from sys import stdin


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

As = list(map(int, stdin.readline().rstrip().split()))

kyori = []

for i in range(1, n):
    kyori.append(As[i] - As[i-1])
kyori.append(As[0]+k-As[-1])
kyori.append(As[-1]-As[-2])

print(k - max(kyori))

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

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


print(sum(B)-max(B))

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

Solution (Python):

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

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

Solution (Python):

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

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

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

Solution (Python):


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

    res = 10**20
    for i in range(1, N-1):
        res = min(K-(a[i+1]-a[i]), K-(a[i]-a[i-1]), res)
    res = min(K-(a[1]-a[0]), res, a[N-1]-a[0])
    res = min(res, K-(a[N-1]-a[N-2]))
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
def II():return int(input())
def MI():return map(int,input().split())
def LI():return list(MI())
def ok():print("Yes")
def ng():print("No");exit()

k,n=MI()
a=LI()

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

Solution (Python):
K, N = map(int,input().split())
A = [int(_) for _ in input().split()]
B = [0]*N

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

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

t = max(B)

print(K-t)

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

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

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

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

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

print(K - max_len)

Solution (Python):
K,N=(int(x) for x in input().split())
As =[int(x) for x in input().split()]
Asub =[As[x+1]-As[x] for x in range(N-1)]
Asub.append(As[0]+K-As[N-1])
print(K-max(Asub))

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

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

dist = [arr[i+1] - arr[i] for i in range(n-1)]
dist += [arr[0] + (k-arr[-1])]
print(k - max(dist))

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def main():
    K,N=mis()
    A=lmis()
    f = A[0] - (A[-1] - K)
    for i in range(1, N):
        f = max(f, A[i]-A[i-1])
    print(K - f)



main()

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

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

print(k - b)

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

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

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

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

dis = [0]*N
for i in range(N-1):
    dis[i] = A[i+1] - A[i]

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

print(K-max(dis))

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

ans = 10**9
for k in range(1,N+1):
    ans = min(K-(A[k]-A[k-1]),ans)
print(ans)

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

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

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

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 = 2 * k

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

    print(ans)

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

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