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
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

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

if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
import sys

stdin = sys.stdin

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

K, n = na()
a = na()

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

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

arr = list(map(int,input().split()))
d = [0 for i in range(n)]

for i in range(n-1):
    d[i] = arr[i+1]-arr[i]

d[-1] = k-arr[-1]+arr[0]


print(k-max(d))

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

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

m = 0
for i in range(n):
    if i != n-1:
        b = a[i+1]-a[i]
    else:
        b = a[0]+k - a[i]
    m = max(m, b)

ans = k-m
print(ans)

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

print(K-MAX)

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

B = [k+A[0]-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())
A = sorted(list(map(int, input().split())))
difA = []
for i in range(N - 1):
    if i == 0:
        difA.append(A[i] + K - A[-1])
    else:
        difA.append(A[i + 1] - A[i])
print(K - max(difA))

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

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

for i, j in zip(a, a[1:]):
    res = max(res, j - i)

print(k - res)

Solution (Python):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(K: int, N: int, A: "List[int]"):
    print(K - max((A[i] - A[i - 1]) % K for i in range(N)))


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


if __name__ == '__main__':
    main()

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

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    k,n = LI()
    a = LI()
    a += [a[0]+k]
    ans = k
    for i in range(n):
        m = a[i+1]-a[i]
        s = k-m
        if s < ans:
            ans = s
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


K, N = MI()
A = LMI()

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

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

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


if __name__ == '__main__':
    main()

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

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

diff = [a1 - a0 for a0, a1 in zip(aaa, aaa[1:])]
print(k - max(diff))