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):
if __name__ == '__main__':
    k, n = map(int, input().split())
    a = [int(i) for i in input().split()]
    max = 0
    max = a[0] + (k-a[n-1])
    bf = 0

    for i  in a:
        now = i -bf
        if now > max:
            max = now
        bf = i

    print(k-max)

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


submit()

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

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


K, N = inpl()
A = inpl()
A.append(K + A[0])

ans = K
for a, na in zip(A, A[1:]):
    ans = min(ans, K - (na - a))

print(ans)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.q = l
        heapq.heapify(self.q)
        return

    def push(self, n):
        heapq.heappush(self.q, n)
        return

    def pop(self):
        return heapq.heappop(self.q)


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    
    K, N = Scanner.map_int()
    A = Scanner.map_int()
    A.append(0)
    A.append(K)
    A.sort()
    l = [A[i + 1] - A[i] for i in range(len(A)-1)]
    ans = min(K - max(l), A[-2] - A[1])
    print(ans)


if __name__ == "__main__":
    main()

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

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
k, n = map(int, input().split())

a = list(map(int, input().split()))
sub = [abs(k - a[-1] + a[0])]

for i in range(len(a) - 1):
    tmp = abs(a[i + 1] - a[i])
    sub.append(tmp)
print(sum(sub) - max(sub))

Solution (Python):


import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(0)
	k, n = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	mind = 10**9
	for i in range(n):
		if i == 0:
			mind = min(mind, k - abs(a[i] - a[i+1]), k - abs(a[i] + k - a[-1]))
			continue
		if i == n - 1:
			mind = min(mind, k - abs(k - a[i] + a[0]), k - abs(a[i] - a[i-1]))
			continue
		mind = min(mind, k - abs(a[i] - a[i+1]), k - abs(a[i] - a[i-1]))
	print(mind)


main()

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

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

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

k, n = list(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 = list(map(int,input().split()))
maxlen = 0
for i in range(1,n):
	maxlen = max(a[i] - a[i-1],maxlen) 

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


print(k - maxlen)

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

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

print(K - ans_sub)

Solution (Python):


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

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

print(K - max(B))

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

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    

    def dump(*args):
        sys.stderr.write(str(args))

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


if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

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

B = [A[0] + K - A[-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())
dat = list(map(int, input().split()))
d = []
c = dat[0]
for i in range(n):
    d.append(dat[i] - c)
    c = dat[i]
d.append(k - dat[-1] + dat[0])
d.sort(reverse=True)

print(sum(d[1:]))

Solution (Python):



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

    A.sort()

    x = A[-1] - A[0]
    x = min(x, K - A[1]+A[0])

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

    print(x)


    



if __name__ == "__main__":
    resolve()

Solution (Python):
K, N = map(int, input().split())
Alist = sorted(list(map(int, input().split())))
minus = 0
old = Alist[0]
for a in Alist[1:]:
    minus = max(minus, a - old)
    old = a
minus = max(minus, (K - a) + Alist[0])
print(K - minus)

Solution (Python):
K, N = map(int, input().split())
A = 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[N - 1] - A[0])
print(ans)

Solution (Python):


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

max_length = A[-1]-A[0]

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

print(max_length)

Solution (Python):

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

a.sort()
ans1 = a[-1] - a[0]
ans2 = 10 ** 10
for i in range(n - 1):
    
    tmp = a[i] + (k - a[i + 1])
    if tmp < ans2:
        ans2 = tmp
print(min(ans1, ans2))

Solution (Python):
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
setrecursionlimit(1000000)


INF = int(1e10)

def input():
    return stdin.readline()[:-1]

def main():
    from builtins import int, map
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()

    
    
    A.append(K + A[0])

    
    dist = A[-1] - A[0]

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


if __name__ == '__main__':
    main()

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

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

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

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

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

r = A[N - 1] - A[0]
ans = min(r, ans)

print(ans)

Solution (Python):
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

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

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

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

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

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

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(A[0]+(K-A[N-1]))
m = max(d)
print(K-m)

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

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

Solution (Python):
from collections import Counter, defaultdict, deque

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

minDist = K + A[0] - A[-1]
for x, y in zip(A, A[1:]):
    minDist = max(minDist, y - x)
print(K - minDist)

Solution (Python):

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

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

max_dist = max(max_dist, K - A_list[N - 1] + A_list[0])
print(K - max_dist)

Solution (Python):
K, N = [int(i) for i in input().split()]
A = sorted([int(i) for i in input().split()])
res = [A[-1]-A[0]]

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

print(min(res))

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

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

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

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

print(k-max(sa))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

K, N = MAP()
A = LIST()
diff = abs(K-A[-1]+A[0])
d = abs(K-A[-1]+A[0])
if N == 2:
	print(min(d, A[1]-A[0]))
	exit()
for i in range(1, N):
	d += abs(A[i]-A[i-1])
	diff = max(diff, abs(A[i]-A[i-1]))
print(d-diff)

Solution (Python):
from sys import stdin
K, N = (int(i) for i in input().split())
A = [int(x) for x in stdin.readline().rstrip().split()]


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

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

print(K-max(dif))

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








print(k-l)

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

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

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

print(K - maxd)

Solution (Python):


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

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


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

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

print(K - max_dif)

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

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

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

print(ans)

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

Solution (Python):

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
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 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 = float('inf')
mod = 10 ** 9 + 7

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

c = []
for b in B:
    c.append(K-b)
print(min(c))

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

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

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

Solution (Python):
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(ans,A[i+1]-A[i])
print(k-ans)

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

K, N = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
diffs = []
prev = 0
for a in A:
    diffs.append(a - prev)
    prev = a
last = K - A[-1] + A[0]
diffs.append(last)

print(K - max(diffs))

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

dist = []

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

answer = max(dist)

print(k - answer)

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

if d < k - a[-1] + a[0]:
    d = k - a[-1] + a[0]

print(k - d)

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

Solution (Python):
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
k,n=map(int,input().split())

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

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

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

print(k-c)

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

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

sa=0
i=1
while i <n:
    if sa<a[i]-a[i-1]:
        sa=a[i]-a[i-1]
    i+=1

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


print(k-sa)

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 = [int(a) for a in input().split()]
A.append(A[0] + K)
print(K - max([A[i+1] - A[i] for i in range(N)]))

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

Solution (Python):

import sys



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



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

if __name__ == '__main__':
    main()

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

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

ans = Alist[0] + (K - Alist[N-1])

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

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

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

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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(K, N, A):
    m = 0
    for i in range(N):
        d = A[i] - A[i-1]
        if d < 0:
            d += K
        m = max(m, d)
    return K - m


def main():
    K, N = read_int_n()
    A = read_int_n()
    print(slv(K, N, A))


if __name__ == '__main__':
    main()

Solution (Python):
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29



def main():
    K,N = isRaw()
    As = isRaw()
    ans = K-As[0]-(K-As[-1])
    for aidx in range(len(As)-1):
        ans = min(ans,K-(As[aidx+1]-As[aidx]))
    return ans

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

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

Solution (Python):

"""
Created on Sun Jan 19 20:52:57 2020

@author: over-
"""

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

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

L.append(A[0]+K-A[-1])
M=max(L)
ans=K-M

print(ans)

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[0] - a[n-1]

print(sum(b) - max(b))

Solution (Python):
import math
import copy
from copy import deepcopy
import sys
import fractions

from functools import reduce

import decimal
import heapq
import collections
import itertools
from operator import mul

sys.setrecursionlimit(100001)







def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList




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

max_dis = 0
reigai = d[0]+(k-d[n-1])
max_dis = max(max_dis, reigai)

for i in range(1, n, 1):
    tmp = d[i]-d[i-1]
    max_dis = max(max_dis, tmp)

print(k-max_dis)

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

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

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])
print(min(k-m,a[n-1]-a[0]))

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

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

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

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math

import itertools



import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default




def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


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




if __name__ == "__main__":

    main()

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

mn=0
for i in range(n):
    if ls[i+1]-ls[i]>mn:
        mn = ls[i+1]-ls[i]

print(k-mn)

Solution (Python):


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

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

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

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

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

print(K - l)

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

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

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

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

for i in range(N - 1):
  B[i] = A[i + 1] - A[i]
  
B[N - 1] = 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=[0 for i in range(N)]
for i in range(N):
    if i ==0:
        b[i] = a[0] +(K-a[N-1])
    else:
        b[i] = a[i]-a[i-1]


minb = max(b)
print(K-minb)

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

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

max_dis=0
tot_dis=0
posi=a[0]
for i in range(1,len(a)):
    tmp=a[i]-posi
    if max_dis < tmp:
        max_dis=tmp
    posi=a[i]
    tot_dis+=tmp
tmp=a[0]+(k-a[-1])
if max_dis < tmp:
    max_dis = tmp
tot_dis += tmp

print(tot_dis-max_dis)

Solution (Python):

import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = 10 ** 18
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

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

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

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

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()))

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

print(K - d)