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

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

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

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

print(ans)

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

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

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

Solution (Python):




def findDistBetween(i,j):
    return abs(i - j)

def minDistfindDistBetweenLastAndFirstHouse(i,j):
    dist = findDistBetween(i, j)
    return K - dist 

K, N = input().split()
As = input().split()

K = int(K)
N = int(N)




A = [0] * len(As)
for i in range(len(As)):
    A[i] = int(As[i])
    

















overAllMin = float('inf')
Arev = list(reversed(A))
dist1 = 0 
dist2 = 0
prevHouse = -1
for i in range(N):
    
    

    dist1 = max(findDistBetween(A[i], prevHouse), dist1)
    prevHouse = A[i]
    

ma = max(dist1, minDistfindDistBetweenLastAndFirstHouse(A[0], A[-1]))
print (K - ma)

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

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

print(K-max(dist))

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

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):
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k * 2)

for i in range(k - 1):
    distance[i] = array[i+1] - array[i]

distance[k-1] = n-array[k-1] + array[0]

print(n-max(distance))

Solution (Python):
from sys import stdin


inp = stdin.readline

n, k = [int(x) for x in inp().strip().split()]

array = [int(x) for x in inp().strip().split()]

distance = [0]*(k * 2)

for i in range(k - 1):
    distance[i] = array[i+1] - array[i]

distance[k-1] = n-array[k-1] + array[0]
distance[2*k-1] = n-array[k-1] + array[0]
for i in range(k - 1):
    distance[k+i] = array[i+1] - array[i]

minimum = 999999999

count = 0
total = 0
countLimit = k-1
for i in range(k * 2):
    if count < countLimit:
        total += distance[i]
        count += 1
    else:
        if minimum > total:
            minimum = total
        total += distance[i]
        total -= distance[i-countLimit]

print(minimum)

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

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

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

n_list=input().split(" ")

for i in range(len(n_list)):
  n_list[i]=int(n_list[i])
ans=n_list[len(n_list)-1]-n_list[0]
for i in range(1,len(n_list)):
  ans=min(ans, k-n_list[i]+n_list[i-1])
print(ans)

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

Solution (Python):
import collections

local = False
if local:
    file = open("C.txt", "r")

def inp():
    if local:
        return file.readline().rstrip()
    else:
        return input().rstrip()

K,N = [int(_) for _ in inp().split()]
arr = [int(_) for _ in inp().split()]

temp = 0
max_dist = -1

for a in arr:
    cur_dist = a-temp

    max_dist = max(max_dist, cur_dist)

    temp = a

max_dist = max(max_dist, arr[0] + K-temp)

print(str(K-max_dist))

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


an = list(map(int, input().split()))
max_interval = 0
for i in range(N):
    if i != N-1:
        max_interval = max(an[i+1] - an[i], max_interval)
    else:
        max_interval = max(an[0] + length - an[i], max_interval)
print(length - max_interval)

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

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(N-1):
    dist.append(A[i+1] - A[i])

print(K - max(dist))

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

print(ans)

Solution (Python):
k, n = map(int, input().split())
dist = list(map(int, input().split()))
maximum = 0
total = 0
for i in range(1, n):
    diff = dist[i] - dist[i - 1]
    total += diff
    maximum = max(diff, maximum)

diff = k - dist[n - 1] + dist[0]
maximum = max(diff, maximum)

print(total + diff - maximum)

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

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

print(K - ans)

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

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

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

k = []
for c in range(N):
    if c == N - 1:
        kk = K - sum(k)
    else:
        kk = A[c + 1] - A[c]
    k.append(kk)
print(sum(k) - max(k))

Solution (Python):
KN=list(map(int,input().strip().split()))
K=KN[0]
N=KN[1]
A_list=list(map(int,input().strip().split()))

Dis_list=[A_list[n]-A_list[n-1] for n in range(1,N)]
Dis_list.append(K-(A_list[N-1]-A_list[0]))

min_distance=K-max(Dis_list)

print(min_distance)

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


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


ans = max(ans, An[0] - (An[-1]-K))

print(K-ans)

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

diffs = [-1] * (n)
for i in range(n-1):
    diffs[i] = positions[i+1] - positions[i]
diffs[n-1] = positions[0] - positions[-1] + k

print(k - max(diffs))

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

dist = [0] * N

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

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

ans = K - max(dist)

print(ans)

Solution (Python):

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

Solution (Python):

KN = input().split(' ')
K = int(KN[0]) ; N = int(KN[1])
Ai = input().split(' ')


AnoSa = []

for k in range(N-1):
    AnoSa.append(int(Ai[k + 1]) - int(Ai[k]))
if K - max(AnoSa) > int(Ai[N-1]) - int(Ai[0]):
    print(int(Ai[N-1]) - int(Ai[0]))
else:
    print(K - max(AnoSa))

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

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

mindis=0

for i in range(len(a)):
  cw=0 
  ccw=0

  if i==0:
    cw=a[len(a)-1]-a[0]
    ccw=k-(a[i+1]-a[i])

  elif i==len(a)-1:
    cw=k-(a[i]-a[i-1])
    ccw=a[i]-a[0]


  else:
    cw=k-(a[i+1]-a[i])
    ccw=k-(a[i]-a[i-1])

  

  if mindis==0:
    if ccw>cw:
      mindis=cw
    else:
      mindis=ccw

  if mindis>cw:
    mindis=cw
  if mindis>ccw:
    mindis=ccw

print(mindis)

Solution (Python):
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
revA = [a - K for a in A[1:]]
S = revA + A

dis = []
for i in range(N):
  st = S[i]
  ed = S[i + N - 1]
  dis.append(max(st, ed) - min(st, ed))
  
print(min(dis))

Solution (Python):
k, n = input("").split()
k = int(k)
n = int(n)
Loc = list(map(int, input("").split()))
temp = Loc[-1]-Loc[0]
for i in range(n-1):
    x1 = Loc[i-1] + k - Loc[i]
    x2 = Loc[i] + k - Loc[i+1]
    temp = min(temp, x1, x2)
temp = min(temp,Loc[-1]-Loc[0], Loc[-2]+k-Loc[-1])

print(temp)

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

d= k - a[-1] + a[0] 
dist.append(d)
dist.sort()
print(sum(dist[:-1]))

Solution (Python):
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(20, [5, 10, 15]), 10)

    def test_2(self):
        self.assertEqual(think(20, [0, 5, 15]), 10)


def solve():
    k, a = read()
    result = think(k, a)
    write(result)


def read():
    k, n = read_int(2)
    return k, read_int(n)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(k, a):
    distance = []
    for i in range(len(a) - 1):
        distance.append(a[i + 1] - a[i])
    distance.append(k - a[-1] + a[0])
    return sum(distance) - max(distance)


def write(result):
    print(result)


if __name__ == '__main__':
    
    solve()

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

a.append(a[0]+k)

for i in range(n):
    ans.append(a[i+1]-a[i])
print(k-max(ans))

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

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

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

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


C = []

for i,v in enumerate(A):
  if i <= len(A) - 2:
    C.append(A[i+1] - A[i])
  else: 
    C.append(k - A[i] + A[0])

maxwidth = max(C)
print(k-maxwidth)

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

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

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

As = map(int, raw_input().split())

a = N
for i in range(len(As)):
  start = As[i]
  stop = As[i-1]
  a_t = stop-start
  if a_t <= 0: a_t += N
  if a_t < a: a = a_t

print a

Solution (Python):
arr=input().split()
k=int(arr[0])
n=int(arr[1])
arr=input().split()
x_i=[int(arr[i]) for i in range(n)]
sa=0
for i in range(1,n,1):
  sa=x_i[i]-x_i[i-1] if sa<x_i[i]-x_i[i-1] else sa
sa=x_i[0]+k-x_i[n-1] if sa<x_i[0]+k-x_i[n-1] else sa
print(k-sa)

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

b = a
c = []
for i,v in enumerate(b):
    if i <= len(b)-2:
        c.append(b[i+1] - b[i])
    else: 
        c.append(k-b[i]+b[0])

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

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

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

Solution (Python):


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys


def input() -> str:
    return sys.stdin.readline().rstrip("\r\n")


def main():
    k, n = map(int, input().split())
    houses = [int(s) for s in input().split()]
    houses = houses + [houses[0] + k]
    distance = 0
    for i in range(1, len(houses)):
        distance = max(distance, houses[i] - houses[i-1])
    print(k - distance)


if __name__ == "__main__":

    main()

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

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

dist = 0

dist = l - lis[n-1] + lis[0]
for kk in range(n-1):
    	if dist < lis[kk + 1] - lis[kk]:
    			dist = lis[kk + 1] - lis[kk]

print(l-dist)

Solution (Python):



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

    k, n = map(int, input().split())
    a = list(map(int, input().split()))
    ans = a[-1] - a[0]

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

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

len = -float("INF")

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

print(k-len)

Solution (Python):
K,N=map(int,input().split())
l=list(map(int,input().split()))
m=0
for j in range(1,len(l)):
	m=max(m,l[j]-l[j-1])
m=max(m,K-(l[len(l)-1]-l[0]))
print(K-m)

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

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

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

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

dis_list = [0 for i in range(n)]
for i in range(1, n):
    dis_list[i] = List[i] - List[i - 1]

dis_list[0] = k - List[-1] + List[0]

print(int(k -max(dis_list)))

Solution (Python):
n = input().split(" ")
input = input().split(" ")
dist = []

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

dist.sort()
dist.reverse()

if dist[0] >= int(input[0]) + (int(n[0]) - int(input[int(n[1]) - 1])):
    print(int(n[0]) - dist[0])
else:
    print(int(input[int(n[1]) - 1]) - int(input[0]))

Solution (Python):


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



a.append(a[0] + k)


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



print(k - max(dist))

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

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

print(k-max(dif_list))

Solution (Python):
K, N = map(int,input().split())
A = list(map(int,input().split()))
list = []
x = K-A[len(A)-1] + A[0]
list.append(x)
for i in range(1,len(A)):
    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()))
b = [0]*(n-1)
p = a[-1]
for i in range(n-1):
    b[i] = a[i+1] - a[i]
b.append(k-p+a[0])
ans = k- max(b)
print(ans)

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]

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

tmp = A[0]
tmp += (K - A[-1])

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

ans.sort(reverse=True)

print(sum(ans[1:]))

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

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

Solution (Python):
def start(k, n, a):
    max_distance=0
    for i in range(n-1):
        if(a[i+1]-a[i]>max_distance):
            max_distance = a[i+1]-a[i]
    if(k-a[-1]+a[0]>max_distance):
        max_distance = k-a[-1]+a[0]
    print(k-max_distance)

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

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

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

result = 10000000
for i in range(N):
    temp = A[i+N-1] - A[i]
    if temp < result:
        result = temp
print(result)

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

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

    print(k - max_length)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

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

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

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

min = a[-1] - a[0]

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

print(min)

Solution (Python):
import copy
import math
import string
import heapq
import bisect
import sys
sys.setrecursionlimit(10**7)

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

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

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

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

prev = a[0]

for v in a[1:]:
    ans = min(ans, k-(v-prev))
    prev = v
ans = min(ans, k-b)
    
print(ans)

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

prev = a[0]

for i, v in enumerate(a):
    if i == 0:
        ans = min(ans, k-b)
    ans = min(ans, k-(v-prev))
    prev = v
    
print(ans)

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

prev = a[0]

for i, v in enumerate(a):
    if i == 0:
        ans = min(ans, k-b)
    ans = min(ans, k-abs(v-prev))
    prev = v
    
print(ans)

Solution (Python):
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[b[0]+(a[0]-b[a[1]-1])]

for i in range(a[1]-1):
  c.append(b[i+1]-b[i])
print(sum(c)-max(c))

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
ans = k
b = a[0] + (k-a[-1])
prev = a[0]
for i, v in enumerate(a):
    x = 0
    if i == 0:
        x = max(abs(a[i+1]-v), b)
    elif i == len(a)-1:
        x = max(b, abs(v-prev))
    else:
        x = max(abs(a[i+1]-v), abs(v-prev))
    ans = min(ans, k-x)
    prev = v
print(ans)

Solution (Python):
k, n = map(int,input().split())
xs = list(map(int,input().split()))
xs = xs + [x+k for x in xs]

ans = float('inf')
for i in range(n):
    delta = xs[i+n-1] - xs[i]
    ans = min(delta, ans)

print(ans)

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

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

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

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

max_distance = 0
max_distance_idx = 0
for i in range(0, N-1):
  dist = A_list[i+1]-A_list[i]
  if max_distance < dist:
    max_distance = dist
    max_distance_idx = i

if max_distance <= (K-A_list[N-1]) + (A_list[0]):
  print(A_list[N-1] - A_list[0])
else:
  print((A_list[max_distance_idx]) + (K-A_list[max_distance_idx+1]))

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

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

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

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

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

kyori = []
for i in range(n - 1):
    kyori.append(abs(A[i] - A[i + 1]))

kyori.append(A[0] + (k-A[-1]))

ans = k - max(kyori)
print(ans)

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

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

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

Solution (Python):
K,N = map(int,input().split())
len = []
a = [int(x) for x in input().split()]
for i in range(N):
  if i == N-1:
    if a[0] == 0:
      len.append(K - a[i])
    else:
      len.append((K - a[i]) + a[0])
  else:
    len.append(a[i+1] - a[i])
len.sort(reverse=True)
print(K-len[0])

Solution (Python):
a,b = map(int,input().split())
c = list(map(int, input().split()))
d = []
for i in range(b):
    if i != b-1:
        d.append(c[i+1]-c[i])
    else:
        d.append(a-c[i]+c[0])
print(sum(d) - max(d))

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

a += [a[0]+k]

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

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

Solution (Python):
k, n = map(int, input().split())
h_list = list(map(int, input().split()))
distance = []
tmp = h_list[0]
for i in range(len(h_list)):
    a = h_list[i] - tmp
    distance.append(a)
    tmp = h_list[i]
x = (k - h_list[len(h_list)-1] )+ h_list[0]
distance.append(x)
sorted_d_list = sorted(distance)
sorted_d_list.pop()
print(sum(sorted_d_list))

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

B = [ A[i+1]-A[i] for i in range(n-1) ] + [A[0]+k-A[-1]]
print(sum(B)-max(B))

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

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

listB = []

sum = max(listA) - min(listA)
listB.append(sum)


for i in range(1,N):
  out = listA[i] - listA[i-1]
  out = K - out
  listB.append(out)


print(min(listB))

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

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

for i in range(1, n - 1):
    ref = k - (a[i + 1] - a[i])
    if ref < ans:
        ans = ref

print(ans)

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

A.append(A[0]+K)
L = []

for i in range(N):
  a = A[i+1]-A[i]
  L.append(a)
ans = K-max(L)

print(ans)

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

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=[]

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

b.append(a[0]+K-a[N-1])

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

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

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

print(sum(distance) - max(distance))

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

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

Solution (Python):
k,n = 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])

longest = 0
for j in range(0,n-1):
  if dist[j] >= longest:
    longest = dist[j]

short_cont_1 = k-longest
short_cont_2 = a[n-1]-a[0]
if short_cont_1>=short_cont_2:
  print(short_cont_2)
else:
  print(short_cont_1)

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

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

Solution (Python):
K, N = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
A = list(map(lambda x: x-K, A)) + A
minimum = -1
i = 0
for i in range(len(A)-N+1):
    cost = abs(A[i] - A[i+N-1])
    if minimum == -1 or cost < minimum:
        minimum = cost
print(minimum)

Solution (Python):






from sys import stdin,setrecursionlimit










setrecursionlimit(10**6+1)
input=stdin.readline

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

print(sum(absdis)-max(absdis))

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

if N == 2:
    print(min(A[1] - A[0], K + A[0] - A[1]))

max_distance = 0

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

    print(min(K - max_distance, A[N - 1] - A[0]))

Solution (Python):
def main():
    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])
    print(K-max(diff))

if __name__ == '__main__':
    main()

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(N):
  maxD = max(maxD, A[n+1] - A[n])
print(K-maxD)

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

if N == 2:
    print(min(A[1] - A[0] , K + A[0] - A[1]))

max_distance = 0

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

    print(min(K-max_distance,A[N-1]-A[0]))

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

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

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

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

a.append(k + a[0])

section = []

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

b_max = max(section)
ans = k - b_max

print(ans)