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

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

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

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


def main():
    print(solve())


if __name__ == '__main__':
    main()

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

print(K-max_dis)

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

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

del distance[distance.index(max(distance))]

print(sum(distance))

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

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

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

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

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

a = sorted(a)

d = -1

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

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

ans = k - d
print(ans)

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

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

print(sum(sorted(B)[:-1]))

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

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

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

An_list.append(An_list[0]+K)


distance=[0]*N

for i in range(0,N):
    distance[int(i)]=An_list[int(i+1)]-An_list[int(i)]

distance.sort()
del distance[-1]

print(sum(distance))

Solution (Python):
K, N = map(int,input().split())
Alist = ["_"]
Alist += list(map(int,input().split()))
maxway = 0
for i in range(1,N+1):
    if i == N:
        temp = (K-Alist[N]) + Alist[1]
    else:
        temp = Alist[i+1] - Alist[i]
    
    maxway = max(maxway,temp)

print(K-maxway)

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

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

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

ans = sum(l) - max(l)
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(ans,a[i+1] - a[i])
ans = max(ans,k-a[-1]+a[0])
print(k-ans)

Solution (Python):
import sys
read = sys.stdin.read
def main():
    k, n = map(int, input().split())
    a = tuple(map(int, input().split()))
    dis_max = (k - a[-1]) + a[0]
    for i1 in range(1, n):
        dis = a[i1] - a[i1-1]
        dis_max = max(dis_max, dis)
    print(k - dis_max)
if __name__ == '__main__':
    main()

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

M = 0
for i in range(-1, N-1):
  dis = abs(A[i] - A[i + 1])
  if 2*dis > K:
    dis = K - dis
  
  if dis > M:
    M = dis
    
print(K-M)

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

a.append(a[0] + k)
max_dist = 0

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

print(k-max_dist)

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

A = [j for j in map(int, input().split())]

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

print(K-max(max(d), K-A[N-1]+A[0]))

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

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

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

Solution (Python):
import sys
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    m = 0
    for i in range(n):
        if i<n-1:
            m = max(m,a[i+1]-a[i])
        else:
            m = max(m,k-a[i]+a[0])
    print(k-m)
    return

if __name__=='__main__':
    Main()

Solution (Python):
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    dist = []
    for i in range(n):
        if i<n-1:
            dist.append(a[i+1]-a[i])
        else:
            dist.append(k-a[i]+a[0])
    x = max(dist)
    print(k-x)
    return

if __name__=='__main__':
    Main()

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

longest = k - (A[-1] - A[0])

for i in range(n-1):
    if A[i+1] - A[i] > longest:
        longest = A[i+1] -A[i]
print(k-longest)

Solution (Python):

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

ans = 10**18
for start_i in range(n):

    if start_i == 0:
        clock = al[-1] - al[0]
    else:
        clock = k - al[start_i] + al[start_i-1]

    if start_i == n-1:
        c_clock = al[-1] - al[0]
    else:
        goal = start_i+1
        c_clock = al[start_i] + (k-al[goal]) 

    ans = min(ans, clock, c_clock)
    
    

print(ans)

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

for i in range(n):
  if i == 0:
    sa = a[i + 1] - a[i]
  elif i == (n - 1):
    if sa < (k + a[0]) - a[i]:
      sa = (k + a[0]) - a[i]
  else:
    if sa < a[i + 1] - a[i]:
      sa = a[i + 1] - a[i]

min = k - sa
print(min)

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

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

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(1,N):
    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()))


mi=-1
m=-1
for i in range(N):
    if i==N-1:
        if m <= K+A[0]-A[i]:
            m=K+A[0]-A[i]
            mi=i
        
    else:
        if m <= A[i+1]-A[i]:
            m=A[i+1]-A[i]
            mi=i
        
if mi==N-1:
    print(A[-1]-A[0])
else:
    print(K-A[mi+1]+A[mi])

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

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

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

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

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

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

for i in range(1,n):
    dp[i] = abs(a[i]-a[i-1])

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

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


tmp_kyori = k - abs(a_lst[-1]-a_lst[0])
for i in range(len(a_lst)-1):
    sa_kyori = a_lst[i+1] - a_lst[i] 
    if sa_kyori > tmp_kyori:
        tmp_kyori = sa_kyori

print(k-tmp_kyori)

Solution (Python):

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

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

diff.sort()
ans = K - diff[-1]
print(ans)

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

Solution (Python):



def main():
    k, n = map(int, input().split())
    l = list(map(int,input().split()))

    l.insert(0, 0)
    l.append(k)
    dist_list = []
    for i in range(n+1):
        dist = l[i+1]-l[i]
        dist_list.append(dist)
    
    dist_list[0] = dist_list[0]+dist_list[-1]
    del dist_list[-1]
    print(sum(dist_list)-max(dist_list))
    

if __name__ == '__main__':
    main()

Solution (Python):

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

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

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


ans = k - max(dist)

print(ans)

Solution (Python):




from __future__ import division, print_function 

import sys
import os
from collections import Counter, deque, defaultdict
import itertools
import math
import io


"""Uncomment modules according to your need"""













if sys.version_info[0] < 3:
    input = raw_input
    range = xrange

    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip



"""value of mod"""
MOD = 998244353
mod = 10**9 + 7

"""Uncomment next 4 lines if doing huge recursion"""




def prepare_factorial(mod=mod):
    """ returns two lists, factorial and inverse factorial modulo argument by default 10**9 +7 """
    
    fact = [1]
    for i in range(1, 200005):
        fact.append((fact[-1] * i) % mod)

    ifact = [0] * 200005
    ifact[200004] = pow(fact[200004], mod - 2, mod)
    for i in range(200004, 0, -1):
        ifact[i - 1] = (i * ifact[i]) % mod

    return fact, ifact

def modinv(n, p):
    """ returns N inverse modulo p """
    return pow(n, p - 2, p)

def ncr(n, r,  fact, ifact):
    """ takes 4 arguments: n , r and factorial and inverse factorial lists"""
    t = (fact[n] * (ifact[r]*ifact[n-r]) % MOD)% MOD
    return t


def get_n(Sum):    
    """this function returns the maximum n for which Summation(n) <= Sum"""
    ans = (-1 + sqrt(1 + 8*Sum))//2
    return ans

def sieve(n):
    """ returns a list of prime numbers till n """
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r



def divs(n, start=1):
    """ returns a list of all divisors till n """
    divisors = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors



def divn(n, primes):
    """ returns the number of divisors, two arguments n and the sieve till n """
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t



def lrfind(d, x, default=-1):
    """ Takes 2 arguments an iterable and an element. returns a tuple (firstoccurence,lastoccurence) -1 if not found """
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return default, default
    else:
        return left, right

def gcd(x, y): 
    """ returns greatest common divisor of x and y """
    while y:
        x, y = y, x % y
    return x

def check_sorted(a):
    ''' returns True/False '''
    for i in range(len(a)-1):
        if a[i]>a[i+1]:
            return False
    return True
def ceil(n, k=1): return n // k + (n % k != 0) 
def input(): return sys.stdin.readline().strip()
def ii(): return int(input()) 
def mi(): return map(int, input().split()) 
def li(): return list(map(int, input().split())) 
def lw(): return input().split() 
def lcm(a, b): return abs(a * b) // gcd(a, b) 
def prr(a, sep=' ', end='\n'): print(sep.join(map(str, a)), end=end) 
def dd(): return defaultdict(int) 
def ddl(): return defaultdict(list) 
def write(s): return sys.stdout.write(s)













def main():
    
    k,n=mi()
    a=li()
    a.append(a[0])
    diff=[]
    for i in range(1,n+1):
        c=abs(a[i]-a[i-1])
        d=k-c
        diff.append(min(c,d))
    print(k-max(diff))









""" -------- Python 2 and 3 footer by Pajenegod and c1729 ---------"""

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange

import os, sys
from io import IOBase, BytesIO

BUFSIZE = 8192

class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill(): pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill();
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)


class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s: self.buffer.write(s.encode('ascii'))
            self.read = lambda: self.buffer.read().decode('ascii')
            self.readline = lambda: self.buffer.readline().decode('ascii')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')



""" main function"""

if __name__ == '__main__':
    main()

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 = list(map(int, input().split()))
houses = list(map(int, input().split()))
distance = 0
for i in range(n):
  if i == n-1:
    tmp = abs(houses[0] - houses[i])
  else:
    tmp = abs(houses[i] - houses[i+1])
  tmp = min(tmp, k - tmp)
  if tmp > distance:
    distance = tmp
print(k - distance)

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

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

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

li = []

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

print(sum(li)- max(li))

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(N):
    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()))
A.append(A[0]+K)
m=0
n=0
for i in range(N):
  if A[i+1]-A[i]>=m:
    m=A[i+1]-A[i]
    n=i+1
if n==N:
  print(A[N-1]-A[0])
else:
  print(K-m)

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

a = list(map(int, input().split()))
li = []

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


print(sum(li) - max(li))

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

a = list(map(int, input().split()))
li = []

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



print(sum(li) - max(li))

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

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

print(min(k-counter,a[n-1]-a[0]))

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

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

As = sorted(A)
As.append(As[0]+K)

gap = []
for x in range(N):
    gap.append(As[x+1] - As[x])

print(K-max(gap))

Solution (Python):
def resolve():
    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    B = [max(A)-min(A)]
    for i in range(N-1):
        B.append(A[i+1]-A[i])
    for j in B:
        if j > K/2:
            B.remove(j)
            B.append(K-j)
    print(K-max(B))
resolve()

Solution (Python):
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = []
l = 0

for i in range(m):
    if l+2 <= m:
        b.append(int(abs(a[l] - a[l+1])))
        l += 1
    elif l+2>m:
        b.append(int(n-a[l]+a[0]))

b.pop(b.index(max(b)))
print(sum(b))

Solution (Python):

import sys
input = lambda: sys.stdin.readline().strip()
k, n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.append(k + a[0])
S = sum(abs(a[i] - a[i - 1]) for i in range(1, len(a)))
print(min(S - abs(a[i] - a[i - 1]) for i in range(1, len(a))))

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

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

num = 0

for i in range(n):
    if num < abs(a[i] - a[i-1]):
        if not i:
            num = k - a[i-1] + a[i]
        else:
            num = a[i] - a[i-1]
        s = i
        e = i-1

if not s:
    print(a[e] - a[s])
else:
    print(k - a[s] + a[e])

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

print(k - max(B))

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

li = []

for i in range(n -1):
    li.append(a[i + 1] - a[i])

li.append(abs(a[0] + (k - a[-1])))
print(sum(li) - max(li))

Solution (Python):

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

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

for i in range(N-1):
    l.append(lis[i+1]-lis[i])

l.append(lis[0] + (K-lis[-1]))
l.remove(max(l))
print(sum(l))

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

a = list(map(int, input().split()))
b = []

a.append(k + a[0])

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

m = max(b)

print(k-m)

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

maxDistance = 0
for k,v in enumerate(A) :
   maxDistance = max(maxDistance, K - A[k] + A[0]) if ( k >= len(A)-1 ) else max(maxDistance, A[k+1] - A[k]) 

print(K - maxDistance)

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 T in range(0,N-1):
    Dist = A[T+1]-A[T]
    MaxD = max(MaxD,Dist)
print(K-MaxD)

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

b = sorted(b)
print(sum(b[:n-1]))

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

Solution (Python):

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

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

D=[]
for i in range(len(A)-1):
    d=A[i+1]-A[i]
    D.append(d)

print(K-max(D))

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

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

d = [None] * n
sum = 0
for i in range(n):
    if i == n-1:
        d[n-1] = k - a[n-1] + a[0]
    else:
        d[i] = a[i+1] - a[i]
    sum += d[i]
print(sum - max(d))

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

d = [None] * n
sum = 0
for i in range(n):
    if i == n-1:
        d[n-1] = k - a[n-1] + a[0]
    else:
        d[i] = a[i+1] - a[i]
    sum += d[i]
ans = 2 * 10**11
for i in range(n):
    if sum - d[i] < ans:
        ans = sum - d[i]
print(ans)

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

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

m = max(d)
print(sum(d) - m)

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

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


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



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

D1 = sorted(D1)
D2 = sorted(D2)

T1 = sum(D1[:-1])
T2 = sum(D2[:-1])

print(min([T1, T2]))

Solution (Python):

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

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

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

for i in range(n - 1):
    d = a[i + 1] - a[i]
    if d > d_max:
        d_max = d

d = (k - a[-1]) + a[0]
if d > d_max:
    d_max = d

print(k - d_max)

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

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

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

print(k - d_max)

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

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

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

print(k - d_max)

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

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

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

dis = 0

for i,j in zip(range(0,N),range(-1,N-1)):
    if i==0:
        temp = K-A[j]+A[i]
    else:
        temp = abs(A[i]-A[j])
    if temp > dis:
        dis = temp
print(K-dis)

Solution (Python):
import sys

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

def calcr(k, n, start, a):
    if start == 0:
        return a[n - 1] - a[0]
    else:
        return k - a[start] + a[start - 1]

def calcl(k, n, start, a):
    if start == n - 1:
        return a[n - 1] - a[0]
    return a[start] + k - a[start + 1]

mindist = calcr(k, n, 0, a)
for i in range(n):
    mindist = min(mindist, calcr(k, n, i, a))
    mindist = min(mindist, calcl(k, n, i, a))

print(mindist)

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

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

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

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

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

res = 10**14
for i in range(N, 2*N):
  res = min(res, cumsum[i]-cumsum[i-N+1])
print(res)

Solution (Python):
k,n=map(int,input().split())
a=list(map(int,input().split()))
b=k+1
for m in range(len(a)):
  a.append(a[m]-k)
a.sort()
for l in range(int(len(a)/2)):
  if b > -a[l]+a[n+l-1]:
    b=a[n+l-1]-a[l]
print(b)

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

dist = K-(A[N-1]-A[0])

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

print(abs(K-dist))

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

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

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

Solution (Python):
import numpy as np
K, N = map(int, input().split())

route = list(map(int, input().split()))
diff = np.diff(np.array(route)).tolist()
diff.append(route[0]+K-route[-1])
diff.remove(max(diff))
print(sum(diff))

Solution (Python):
X = input().split()

K = int(X[0])
N = int(X[1])

A = input().split()

i = 0
j = 1
max_distance = 0

while j < N:
    a = int(A[j-1])
    b = int(A[j])
    distance = b - a
    if distance > max_distance:
        max_distance = distance
    j += 1

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

if last_distance > max_distance:
    max_distance = last_distance

shortest_distance = K - max_distance
print(shortest_distance)

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

    
print(K-max(l))

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

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

print(k-acc)

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

shiftA = A[1:] + [K+A[0]]

subA = [shiftA[i] - A[i] for i in range(N)]
maxA = max(subA)
subA.remove(maxA)

print(sum(subA))

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

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

gap.append(k-a[-1])
m = max(max(gap),gap[0]+gap[-1])
print(k-m)

Solution (Python):


import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
K,N = LI()
A = LI()
path_len = [0] * N
maxpath_ind = 0
maxpath_num = 0


for i in range(N):
  if i+1==N:
    tmp = K-A[i]+A[0]
  else:
    tmp = A[i+1] - A[i]
  path_len[i] = tmp
  if tmp > maxpath_num:
    maxpath_num = tmp
    maxpath_ind = i

sum = 0
for i in range(N):
  if i != maxpath_ind:
    sum+=path_len[i]

print(sum)

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

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

max_d = 0
for i in range(n):
    if i == 0:
        d = A[i] + (k - A[-1])
    else:
        d = A[i] - A[i-1]

    if d > max_d:
        max_d = d

print(k-max_d)

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