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 sys


def solve(K: int, N: int, A: "List[int]"):
    B = []
    for i in range(N-1):
        B.append(A[i+1]-A[i])
    B.append(A[0] + K - A[N-1])
    C = []
    for b in B:
        C.append(K-b)
    print(min(C))
    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):
def main():
    K,N = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    res = A[-1] - A[0]
    for i in range(N-1):
        res = min(res, K - A[i+1] + A[i])
    print(res)
main()

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

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

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

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    K, N = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    m = 0

    for a, b in zip(A, A[1:]):
        m = max(m, abs(a - b))

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

    print(K - m)



if __name__ == '__main__':
    main()

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

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

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

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

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

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

Solution (Python):
def main():
    K, N = map( int, input().split())
    A = list( map( int, input().split()))
    ans = K - A[0] - (K-A[N-1])
    for i in range(N-1):
        if K - (A[i+1] - A[i]) < ans:
            ans = K - (A[i+1] - A[i])
    print(ans)
if __name__ == '__main__':
    main()

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

print(sum_d - max_d)

Solution (Python):












mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

if __name__=='__main__':
    main()

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

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input=lambda: sys.stdin.readline().rstrip()
k,n=map(int,input().split())
A=[int(i) for i in input().split()]
ans=k-(k-A[-1]+A[0])
for i in range(n-1):
  ans=min(ans,k-A[i+1]+A[i])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
K, N = map(int, input().split())
a = list(map(int, input().split()))
d = [0] * N
for i in range(N): d[i] = abs(a[i - 1] - a[i])
d[0] = min(d[0], K - d[0])
print(sum(d) - max(d))

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

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

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()))
dist = [0]*n
for i in range(n-1):
  dist[i] = a[i+1]-a[i]
dist[-1] = k-a[-1]+a[0]
dist.sort()
dist.remove(dist[-1])
print(sum(dist))

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

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

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

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

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

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

print(k-max_diff)

Solution (Python):
import sys
input=sys.stdin.readline
k,n=map(int,input().split())
a_list=list(map(int,input().split()))
ans=10**9
for i in range(n):
    if i==0:
        t=a_list[n-1]-a_list[0]
        ans=t
    else:
        t=k-a_list[i]+a_list[i-1]
        ans=min(ans,t)
print(ans)

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

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

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

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

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

print(k - ans)

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

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

Solution (Python):

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

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

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

print(k-mx)

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

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

MAX=0
for i in range(len(A)):
    if MAX<A[i]-A[i-1]:
        MAX=A[i]-A[i-1]
print(K-MAX)

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

k,n,*a = [int(i) for i in read().split()]

d = max(j-i for i,j in zip(a,a[1:]))
d = max(d,k-a[-1]+a[0])

print(k-d)

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

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


if __name__ == "__main__":
    main()

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

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

ma = 0
for i in range(1, N):
    d = As[i] - As[i-1]
    ma = ma if ma > d else d

d = K - As[-1] + As[0]
ma = ma if ma > d else d
print(K - ma)

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

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

def check(s):
    for i in range(len(s)):
        if s[i] != s[len(s)-1-i]:
            return False
    return True

def main():
    k, n = rli()
    la = rli()
    la.sort()
    m = 0
    for i in range(n):
        l = la[i]
        r = la[(i+1)%n]
        if l > r:
            r += k
        m = max(m, r-l)
    print(k-m)


if __name__ == '__main__':
    main()

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

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

diff_max = 0

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

diff_max = max(diff_max, a_list[0] + (k - a_list[-1]))

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

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

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

A.append(K+A[0])

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

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

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

Solution (Python):
def main():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    t = 0
    p = A[0]
    for i in A[1:]:
        t = max(t, i - p)
        p = i
    t = max(t, A[0] + K - A[-1])
    return K - t
print(main())

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

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

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

A = [int(x) for x in input().split()]

Adelta = [A[i+1]-A[i] if i < (N-1) else A[0] + (K - A[N-1]) for i in range(N)]

AdeltaMax = max(Adelta)

print(K - AdeltaMax)

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

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

print(K - M)

Solution (Python):

import sys


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

B.sort()
ans = sum(B[:-1])
print(ans)

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

Solution (Python):

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

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

print(K-maxd)

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

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

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

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

print(k - max(b))

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

a.append(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()))

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

print(k - max(diff))

Solution (Python):


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


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

Solution (Python):
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    k,n=INTM()
    A=LIST()
    As=[]
    for i in range(n-1):
        As.append(A[i+1]-A[i])
    As.append(k+A[0]-A[n-1])
    print(k-max(As))
    
if __name__ == '__main__':
    do()

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


main()

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

diff_max = 0
for a, b in zip(A[:-1], A[1:]):
    diff_max = max(diff_max, b - a)

print(k - diff_max)

Solution (Python):


import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    k, n = na()
    a = na()
    lmax = a[0] + (k-a[-1])
    for ai, aj in zip(a[0:-1], a[1:]):
        lmax = max(lmax, (aj-ai))
    print(k-lmax)

if __name__ == "__main__":
    main()

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

B = [b-a for b,a in zip(A[1:],A[:-1])]

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

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

Solution (Python):

import sys
input = sys.stdin.readline

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

a.append(a[0] + k)
max_diff = 0
for a1, a2 in zip(a, a[1:]):
    max_diff = max(max_diff, a2 - a1)
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,=map(int,input().split())
a=list(map(int,input().split()))


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

print(k-max(b))

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

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

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    K, N = map(int, input().split())
    A = [int(a) for a in input().split()]
    longestL = A[0] + (K - A[N - 1])
    for i in range(N - 1):
        longestL = max(longestL, A[i+1] - A[i])
    print(K - longestL)


    return 0

if __name__ == "__main__":
    solve()

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

max_gap = 0

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

print(K - max_gap)

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


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

ans = K - (A[0] + K - A[-1])
for a, b in zip(A, A[1:]):
    ans = min(K - (b - a), ans)
print(ans)

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

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

print(k-saidai)

Solution (Python):
import numpy as np
K, N = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
B = A + K

A = np.concatenate((A, B)).tolist()
ans = 10 ** 18
for i in range(N):
    d = A[i + N - 1] - A[i]
    ans = min(d, ans)

print(ans)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

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

max_len = 0

for i in range(N):
    l = A[i] - A[i-1]
    if l < 0:
        l += K
    if(max_len < l):
        max_len = l

print(K - max_len)

Solution (Python):
import sys
import itertools
import bisect
from operator import itemgetter
 
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

K,N = IL()
a = IL()

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

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

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

ans = K - max(dlist)
print(ans)

Solution (Python):
def examA():
    S = SI()
    if S[2]==S[3] and S[4]==S[5]:
        print("Yes")
    else:
        print("No")
    return

def examB():
    X = I()
    ans = 0
    ans += (X//500) * 1000
    X %= 500
    ans += (X//5) * 5
    print(ans)
    return

def examC():
    K, N = LI()
    A = LI()
    L = [0]*N
    L[0] = K-A[-1] + A[0]
    for i in range(1,N):
        L[i] = A[i]-A[i-1]
    ans = sum(L) - max(L)
    
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examC()

"""

"""

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

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

print(K-max_num)

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

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

Solution (Python):
k,n=map(int,input().split())
A=list(map(int,input().split()))
ans=A[0]+k-A[n-1]
for i in range(n-1):
    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()))

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

print(k-mx)

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

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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,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 fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    
    K,N=MI()
    A=LI()
    dist = []
    for i,a in enumerate(A):
        if(i!=N-1):
            dist.append(A[i+1]-a)
        else:
            dist.append(A[0]+K-a)
    dist.sort()
    print(sum(dist[:-1]))
if __name__ == '__main__':
    main()

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

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

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

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

for i in range(1, N):
  L.append(A[i] - A[i - 1])

L.sort()

print(sum(L) - L[-1])

Solution (Python):

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

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

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

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

Solution (Python):
def main():
    K, N = map(int, input().split())
    *a, = map(int, input().split())
    a.append(K + a[0])

    b = [a1 - a0 for a1, a0 in zip(a[1:], a)]
    tot = sum(b)
    ret = tot - max(b)
    print(ret)


if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

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

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

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

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

maxdiff=K-(A[-1]-A[0])
for i in range(1,len(A)):
  if abs(A[i]-A[i-1])>maxdiff:
    maxdiff=abs(A[i]-A[i-1])
print(K-maxdiff)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    k,n=MI()
    aa=LI()
    mx=k-aa[-1]+aa[0]
    for a0,a1 in zip(aa,aa[1:]):
        mx=max(mx,a1-a0)
    print(k-mx)

main()

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    k,n = inpm()
    a = inpl()
    a.append(a[0]+k)
    MAX = 0
    for i in range(n):
        d = a[i+1]-a[i]
        MAX = max(MAX,d)
    print(k-MAX)
    
if __name__ == "__main__":
    main()

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

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

ans = k - d
print(ans)

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

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

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

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

k, n = MAP()
a = LIST()
d = [a[i + 1] - a[i] for i in range(n - 1)]
d.append(a[0] + k - a[-1])
print(k - max(d))

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):
k, n = map(int, input().split())
a = list(map(int, input().split()))

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

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Solution (Python):

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

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

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

for i in range(N-1):

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

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

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

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

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols
    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])
    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '
        
        
    return ret


MOD = 10**9 + 7

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  


K, N = read_ints()
A = read_ints()
import numpy as np
A_diff = list(np.diff(A))
A_diff.append(A[0] + K - A[-1])
print(K - max(A_diff))