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):
N, K = map(int, input().split())
A = list(map(int, input().split()))

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

print(N-l)

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

A14=K-A[N-1]+A[0]
sa=[A14]

for i in range(N-1):
  sa.append(A[i+1]-A[i])
sa.sort()
ans=0
for i in range(N-1):
  ans+=sa[i]
print(ans)

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

ans = 0
maxi = 0

for i in range(1, len(A)):
    ans += abs(A[i]-A[i-1])
    if maxi < abs(A[i]-A[i-1]): maxi = abs(A[i]-A[i-1])
    
if A[0]==0:
    if maxi < N-A[-1]: maxi = N-A[-1]
    ans += N-A[-1]
else :
    if maxi < N-A[-1]+A[0]: maxi = N-A[-1]+A[0]
    ans += N-A[-1]+A[0]
    
print(ans-maxi)

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

Solution (Python):
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))
B = np.roll(A, -1)
delta = B - A
delta = list(delta % K)
saidai = max(delta)

delta.remove(saidai)

print(sum(delta))

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

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

for i in range(1, n):
    y = a[i]
    x = a[i-1]
    d.append(y-x)
d.append(k - (a[n-1] - a[0]))
d.sort(reverse = True)
print(k-d[0])

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

diff = [None] * N

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

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

max_diff = max(diff)
answer = sum(diff) - max(diff)
print(answer)

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

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

can = k - max(c)
print(min((a[-1] - a[0]), can))

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

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


if __name__ == "__main__":
    solve()

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

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

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

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

ans = sum(d) - max(d)

print(ans)

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

max=list[0]+k-list[n-1]
for i in range(n-1):
    if max < list[i+1]-list[i]:
        max=list[i+1]-list[i]

print(k-max)

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

dis = 0
for i in range(len(a)-1):
    if dis < a[i+1]-a[i]:
        dis = a[i+1]-a[i]

if dis < a[0]+k-a[len(a)-1]:
    dis = a[0] + k-a[len(a)-1]

print(k-dis)

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

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

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

print(K-max(list))

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

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

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

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

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

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

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

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 == N-1:
        B[i] = K - A[i] + A[0]
    else:
        B[i] = A[i+1] - A[i]

C = sum(B) - max(B)

print(C)

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

Solution (Python):
K,N=map(int,input().split())
*A,=map(int,input().split())
A.append(A[0]+K)
ans=float('inf')
for i in range(N):
  a,b=A[i],A[i+1]
  ans=min(ans, K-(b-a))
print(ans)

Solution (Python):
K,N=map(int,input().split())
*A,=map(int,input().split())
ans=float('inf')
for i in range(N-1):
  a,b=A[i],A[i+1]
  b=abs(K-b)
  ans=min(ans, a+b)
print(min(ans,A[N-1]-A[0]))

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


max_d = 0
max_index = 0
for i in range(n-1):
    if A[i+1] - A[i] > max_d:
        max_d = A[i+1]-A[i]
        max_index = i

if k-A[n-1]+A[0] > max_d:
    max_d = k-A[n-1]+A[0]
    max_index = n-1

min_distance = 0

if(max_index != n-1):
    min_distance = A[max_index]+k-A[max_index+1]
else:
    min_distance = A[n-1]-A[0]

print(min_distance)

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

print(k-max(glist))

Solution (Python):

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

ans =[]

for i in range(N-1):
    ans.append(A[i+1]-A[i])
ans.append(abs(A[0] + (K - A[-1])))
print(sum(ans) - max(ans))

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

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

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

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

print(min(A[-1]-A[0], ans))

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

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


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

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

print(k-max_len)

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

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

A.append(K+A[0])

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

print(K - maxl)

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

print(ans)

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

D = [0]*N

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

Solution (Python):

import re
import math
from collections import defaultdict
import itertools
from copy import deepcopy
import random
from heapq import heappop,heappush
import time
import os
import queue
import sys
import datetime
from functools import lru_cache

readline=sys.stdin.readline
sys.setrecursionlimit(2000000)

alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
def hani(x,min_,max_):
    ret=x
    if x<min_:ret=min_
    if x>max_:ret=max_
    return ret
def seifu(x):
    return x//abs(x)


def main():
    K,N=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    mmax=0
    for a in range(0,N):
        mmax=max(mmax,abs(A[(a+1)%N]+K*((a+1)//N)-A[a]))
    print(K-mmax)

main()

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

Solution (Python):
k, n = map(int,input().split())
a = [int(x) for x in 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[n-1]
 
print(k - max(b))

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

print(k - max(b))

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

print(k-c)

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

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

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


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

print(k - max_v)

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

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

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

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))

def resolve():
    K,N=pin()
    A=lispin()
    temp=K
    for i in range(0,N):
        temp=min(temp,max(abs(A[i-1]-A[i]),K-abs(A[i]-A[i-1])))
    print(temp)
        

resolve()

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

sub_x_list = x_list.copy()[1:]
sub_x_list.append(K + x_list[0])

for i in range(N):
    sub_x_list[i] -= x_list[i]
    
max_i = max(sub_x_list)
print(K - max_i)

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

sub_A_list = A_list.copy()[1:]
sub_A_list.append(K + A_list[0])

for i in range(N):
    sub_A_list[i] -= A_list[i]

maxi = max(sub_A_list)
print(K - maxi)

Solution (Python):
from sys import stdin


def main():
    k, n = map(int, stdin.readline().split())
    a = list(map(int, stdin.readline().split()))
    b = [0] * n
    for i in range(n):
        if 0 <= i < n - 1:
            b[i] = a[i + 1] - a[i]
        else:
            b[i] = k - a[i] + a[0]
    print(k - max(b))


if __name__ == "__main__":
    main()

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

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

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
R=[]
for i in range(N):
    if i == N-1:
        R.append(abs(A[i]-(K+A[0])))
    else:
        R.append(abs(A[i]-A[i+1]))
R.sort(key=int)
print(K-R.pop(-1))

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

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

right = a[n-1]-a[0]
left = 0

min = right

for x in range(n-1):
    left = (k-a[x+1])+a[x]
    if left <= right and left < min:
        min = left

print(str(min))

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

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

    a_diff = [a[i] - a[i-1] for i in range(1, len(a))]
    a_diff.append((k - a[-1]) + a[0])
    a_diff = sorted(a_diff, reverse=True)

    print(sum(a_diff[1:]))


resolve()

Solution (Python):

"""
Created on Wed May  6 16:35:32 2020

@author: shinba
"""

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

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

print(K-max(B))

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

Solution (Python):


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

total = 0
mx = 0

for i in range(N):
    if i == N - 1:
        dist = K - A[i] + A[0]
    else:
        dist = A[i + 1] - A[i]
    if mx < dist:
        mx = dist
    total += dist

print(total - mx)

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

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

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

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

print(K - max(dist))

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

l = [0]*n
l[-1] = k+a[0]-a[-1]
for i in range(n-1):
  l[i] = a[i+1] - a[i]
print(sum(sorted(l)[:-1]))

Solution (Python):
def bubbleSort(array):
    sortCompleted = False
    while sortCompleted != True:
        sortCompleted = True
        for i in range(len(array)-1):
            if array[i] > array[i+1]:
                array[i], array[i+1] = array[i+1], array[i]
                sortCompleted = False

def getMaxDistance(array):
    maxDistance = 0
    for i in range(len(array)-1):
        distance = abs(array[i] - array[i+1])
        if maxDistance < distance:
            maxDistance = distance
    return maxDistance

def function(K, An): 
    bubbleSort(An)
    An.append(K+An[0])
    maxDistance = getMaxDistance(An)
    minDistance = K - maxDistance
    return str(minDistance)

if __name__ == '__main__':
    K, N = map(int, input().split())
    An = list(map(int,input().split()))
    print(function(K, An))

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

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

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

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

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

Solution (Python):



k, n = [int(i) for i in input().split()]
distances = [int(d) for d in input().split()]
sections = [distances[i+1] - distances[i] for i in range(n-1)]
sections.append(distances[0] - (distances[-1] - k))

print(k - max(sections))

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

a += [a[0] + k]
maxPath = max(b - a for a,b in zip(a,a[1:]))
print(k - maxPath)

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

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

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

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

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

print(K-a)

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

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

print(k - max(b))

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

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

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

interval = []
ans = 1e+9
for i in range(len(a_list)):
    if i == 0:
        distance = a_list[-1] - a_list[0]
    else:
        interval = a_list[i] - a_list[i-1]
        distance = k - interval
    ans = min(ans, distance)
print(ans)

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

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


sorted_distance = sorted(distance, reverse=True)
print(sum(distance) - sorted_distance[0])

Solution (Python):
import numpy as np

k, n = map(int, input().split())
A = np.array(list(map(int, input().split())))
B = A[1:]
dist = B - A[:-1]
dist = np.append(dist, k-A[-1]+A[0])
print(np.sum(dist) - np.max(dist))

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

B = [A[-1] - k] + A[:n]

Diff = sorted(list(map(lambda x, y: abs(x-y), A, B)))
print(sum(Diff[:-1]))

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

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

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


resolve()

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):
    if A[i+1] - A[i] > M:
        M = A[i+1] - A[i]
print(k-M)

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

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

print(K - m)

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

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

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

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

print(K-l)

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    k,n = LI()
    a = LI()
    r = a[0] - a[-1] + k
    for i in range(n-1):
        r = max(r, a[i+1] - a[i])

    return k - r


print(main())

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

sum = 0
long_distance = 0
distance = 0
for i in range(n):
    if i == n-1:
        distance = k - As[i] + As[0]
    else:
        distance = As[i+1] - As[i]
    sum += distance
    if long_distance < distance:
        long_distance = distance

print(sum-long_distance)

Solution (Python):
def check(a):
  if a < 0:
    return a * -1
  else:
    return a

def main():
  K, N = map(int, input().split())
  As = list(map(int, input().split()))
  r = []
  r.append(K-As[-1]+As[0])
  for x in range(N-1, -1, -1):
    solve = As[x] - As[x-1]
    r.append(check(solve))
  ans = sorted(r)
  ans.remove(ans[-1])
  print(K-ans[-1])

if __name__ == "__main__":
  main()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  
def Is(): return map(int, input().split())  
def LI(): return list(map(int, input().split()))  
def TI(): return tuple(map(int, input().split()))  
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  
def LS(): return list(input())  
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

k, n = Is()
A = LI()

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

print(k-m)

Solution (Python):

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

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

print(k-max(d))

Solution (Python):



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

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

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    k, n = MI()
    A = LI()
    for i in range(n):
        A.append(k+A[i])
    
    
    S = [0] * (2 * n)

    ans = INF
    for i in range(1, 2 * n):
        
        
        S[i] = S[i-1] + A[i] - A[i-1]
        if i >= (n - 1):
            
            ans = min(ans, S[i] - S[i-n+1])

    
    print(ans)


    
    






if __name__ == '__main__':
    solve()

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

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

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

X=max(x,k-a[n-1]+a[0])
print(k-X)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

K, N = LI()
A = LI()

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

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

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

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


disList = []
for i in range(n):
    if i == n-1:
        dis = k - a_[i] + a_[0]
        disList.append(dis)
    else:
        dis = a_[i+1] - a_[i]
        disList.append(dis)
max = max(disList)
print(k - max)

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


disList = []
for i in range(n):
    if i == n-1:
        dis = k - a_[i] + a_[0]
        disList.append(dis)
    else:
        dis = a_[i+1] - a_[i]
        disList.append(dis)
max = max(disList)
disList.remove(max)

print(sum(disList))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
k,n = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]

a.sort()
a.append(a[0]+k) 

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

print(k-d)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
k,n = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]

a.sort()

d = a[0]+(k-a[n-1])
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()))

d = [None]*n

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

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

print(k - max(d))

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

memo = []

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

memo.append(K - A[-1] + A[0])
ans = sum(memo)
ans -= max(memo)
print(ans)