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 = list(map(int, input().split()))
arry = list(map(int, input().split()))
t_arry = []
for i in range(0,len(arry)-1):
    t_arry.append(arry[i+1]-arry[i])

dist = arry[0] + k - arry[len(arry)-1]
t_arry.append(dist)

print(sum(t_arry) - max(t_arry))

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

A.append(K + A[0])

distance = 0
for x in range(len(A) - 1):
    distance = max(distance, A[x + 1] - A[x])

print(K - distance)

Solution (Python):
first = str(input()).split(' ')
second = str(input()).split(' ')

k = int(first[0]) 
n = int(first[1]) 
A = [int(i) for i in second]

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

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

a = sorted(a)
miner = a[-1] - a[0]
for i in range(1, n):
    miner = min(miner, abs(a[- 1  + i] + k - a[i]))

print(miner)

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

Solution (Python):
K, N = map(int, input().split())
A = sorted(map(int, input().split()))
D = [0] * 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):
l = list(map(int, input().split()))
l2 = list(map(int, input().split()))
m = abs(l[0]-l2[-1]+l2[0])
for i in range(1, len(l2)):
    m = max(m,abs(l2[i]-l2[i-1]))
print(l[0]-m)

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

now = k - l[n-1] + l[0]

for i in range(n-1):
  now = max(l[i+1] - l[i], now)
  
print(k - now)

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

A = sorted(A)
max_l = 0

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

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

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


print(K-max_diff)

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

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

Solution (Python):

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

As = [A - As[0] for A in As]
As_diffs = [As[i+1] - As[i] for i in range(len(As)-1)]
As_diffs += [K - As[-1]]
As_fiff_max = max(As_diffs)


print(K - As_fiff_max)

Solution (Python):
import sys
k, n = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))
L=[]
for i in range(0, n-1):
    L.append(abs(A[i+1]-A[i]))
L.append(k-A[-1]+ A[0])

print(sum(L)-max(L))

Solution (Python):

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

sum_dist = 0
dist = [0] * n

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

dist[n-1] = a[0] + k - a[n-1]
sum_dist += dist[n-1]

result = sum_dist - max(dist)

print(result)

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

dis = [0] * n

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

dis[n-1] = min(abs(a[n-1] - a[0]), abs((a[0]+k)-a[n-1]))

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

Solution (Python):
k, n, *A = map(int, open(0).read().split())
B = [a1-a0 for a0, a1 in zip(A, A[1:])] + [k-A[-1]+A[0]]
print(sum(B) - max(B))

Solution (Python):
import numpy as np

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

a_list = np.array(a_list)

dif = a_list[1:] - a_list[:-1]
dif_max = np.max(dif)

print(k-dif_max)

Solution (Python):
KN = list(map(int,input().split()))
A = list(map(int,input().split()))
maxi = A[0] - A[KN[1]-1] + KN[0]
result = 0

for i in range(KN[1]-1):
    if maxi < A[i+1]-A[i]:
        maxi = A[i+1]-A[i]

print(KN[0]-maxi)

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

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
from fractions import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)


K, N = inp_list()
A = inp_list()

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

print(K - max_dist)

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


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

print(k-max(dist))

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

print(K-A_max)

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

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

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

ans = float("inf")
for i in range(N-1):
    
    if i == 0:
        ans = min(ans, A[N-1]-A[i])
    else:
        ans = min(ans, (K-A[i])+A[i-1])

    
    if i == 0:
        ans = min(ans, A[i]+(K-A[i+1]))
    else:
        ans = min(ans, A[i]+(K-A[i+1]))



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

ans = min(ans, K-A[N-1]+A[N-2])
print(ans)

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

Solution (Python):
from collections import deque

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

B = deque(A)
b = B.pop()
B.appendleft(b - K)

print(K - max(a - b for a, b in zip(A, B)))

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

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

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

import numpy as np
diff = np.array(A[1:] + [K + A[0]]) - np.array(A)
print(K - diff.max())

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


max = 0
for i in range(N-1):
    if As[i+1] - As[i] > max:
        max = As[i+1] - As[i]

if (As[0]+K) - As[N-1] > max:
    max = (As[0]+K) - As[N-1]

print(K-max)

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

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

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

s = k - c

print (s)

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

max_diff = 0

for i in range(len(A)):
    if i == 0:
        max_diff = abs(K - A[-1]) + A[i]
    elif i == len(A) -1:
        break
    else:
        if max_diff < A[i+1] - A[i]:
            max_diff = A[i+1] - A[i]

print(K - max_diff)

Solution (Python):


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

Solution (Python):
'''
Created on 2020/03/28

@author: fanta
'''
N,K = map(int,input().split())
A = [int(x) for x in input().split()]
B = []

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

print(N - max(B))

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

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

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

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

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

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

print(sum(total)-max(total))

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

mi = 0

for n in range(N):
    j = n
    k = (n + 1) % N
    i = (A[k] - A[j] + K) % K
    if i > mi:
        mi = i

print(K-mi)

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

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


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
total, N = map(int, input().split())
nums = list(map(int, input().split()))
if N == 1:
    print(0)
else:
    ans = total - (total - nums[-1] + nums[0])
    prev = nums[0]
    for num in nums[1:]:
        ans = min(total-(num - prev), ans)
        prev = num
    print(ans)

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

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

for i in range(1, n):
    list_ans.append(list_A[i]-list_A[i-1])

print(k-max(list_ans))

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(abs(a[0] + k - a[-1]))
b.sort()

print(sum(b[:-1]))

Solution (Python):
K, N = map(int, input().split())
li1 = list(map(int, input().split()))
i = 0
li2 = []
while i <= N-2:
  li2.append(li1[i+1] - li1[i])
  i += 1
li2.append(li1[0]+K - li1[N-1])
print(K - max(li2))

Solution (Python):
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

k,n = miinps()
a = lmiinps()

l = -1

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

if a[0] == 0:
    l = max(l,k-a[n-1])
else:
    l = max(l,(k-a[n-1])+a[0])

print(k-l)

Solution (Python):
import math
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))
mod = 10**9 + 7 
sys.setrecursionlimit(1000010)


K,N = nm()
A  = nl()

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

if A[0]+K - A[N-1] > maxL:
    maxL = A[0]+K - A[N-1]

print(K - maxL )

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

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

Solution (Python):
k, n = list(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(k-a[-1]+a[0])

print(k - max(b))

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

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

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

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

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(len(a)-1):
    if i == len(a):
        break
    else:
        if d < a[i+1] - a[i]:
            d = a[i+1] - a[i]
print (k-d)

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

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

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

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

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

relative_distance = []
relative_distance.append(A[0] + K - A[-1])

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

print(sum(relative_distance) - max(relative_distance))

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

dList = [0] * n
dMax = 0
for i in range(n-1):
  dList[i] = aList[i+1] - aList[i]
  if dMax < dList[i]:
    dMax = dList[i]

dList[n-1] = aList[0] + k - aList[n-1]
if dMax < dList[n-1]:
  dMax = dList[n-1]

dSum = sum(dList) - dMax
print(dSum)

Solution (Python):
import numpy as np

K, N = map(int, input().split())
an = [int(x) for x in input().split()]
arr = np.array(an)

dist = arr[1:] - arr[:-1]
dist = list(dist)
dist.append(K - an[N-1] + an[0])

print(K - max(dist))

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

max = a[0] + k - a[n-1]
for i in range(n-1):
    kyori = a[i+1] - a[i]
    if kyori > max:
        max = kyori

print(k-max)

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

Solution (Python):



import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    K, N = i2nn()
    A = i2nn()
    A.append(K + A[0])
    max_d = 0
    for i in range(N):
        d = A[i + 1] - A[i]
        max_d = max(max_d, d)
    print(K - max_d)
    return

main()

Solution (Python):
def getIntList(inp):
	return [int(_) for _ in inp]


from sys import stdin
import sys
sys.setrecursionlimit(10**8)

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs

def main(inputs):
	k,n=getIntList(inputs[0])
	aList=getIntList(inputs[1])
	
	d=k-aList[n-1]+aList[0]
	di=n-1

	for ai in range(n-1):
		dd=aList[ai+1]-aList[ai]

		if dd>d:
			di=ai
			d=dd
	
	print(k-d)


if __name__=="__main__":
	inputs=getInputs()

	main(inputs)

Solution (Python):
k, n = map(int, input().split())
a_array = map(int, input().split())
first_a = a_array.__next__()
forward_a = first_a
max_length = 0
for i in range(n - 1):
    a = a_array.__next__()
    max_length, forward_a = max(max_length, a - forward_a), a
print(k-max(max_length, k - a + first_a))

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
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 [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  k,n=LI()
  l=LI()
  a=copy.deepcopy(l)

  for x in a:
    l.append(k+x)

  ans=inf
  for i in range(n):
    ans=min(ans,l[i+n-1]-l[i])

  return ans


print(main())

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

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

print(k-max(lst))

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

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()))
A.append(A[0]+K)

max_dist = 0
for i in range(N):
    cur_dist = A[i+1] - A[i]
    if max_dist < cur_dist:
        max_dist = cur_dist

print(str(K - max_dist))

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

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

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

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

min_dis = sum(A[:N])
for idx, a in enumerate(A):
    if idx < N-1:
        diff_list.append(A[idx+1]-a)
    else:
        diff_list.append(A[0]+K-A[-1])

print(K - max(diff_list))

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

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

if __name__ == '__main__':
    solve()

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

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

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

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

for i in range(N-1):
  m = max(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()]
d = 0
for i in range(N):
    tmp = abs(A[i] - A[i-1])
    if tmp > K / 2:
        tmp = K - tmp
    if tmp > d:
        d = tmp
print(K - d)

Solution (Python):
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
INF = 1 << 60
MOD = 10 ** 9 + 7









k, n = map(int, input().split())
a = list(map(int, input().split()))
max_gap = 0
start = 0
for i in range(n):
    if i == 0:
        if k - a[-1] + a[0] > max_gap:
            start = i
            max_gap = k - a[-1] + a[0]
    elif a[i] - a[i - 1] > max_gap:
        start = i
        max_gap = a[i] - a[i - 1]


ans = k - max_gap
print(ans)

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

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

max = 0
skip = 0
for i in range(N-1):
    if a[i+1]-a[i] > max: 
        max=a[i+1]-a[i]
    if i == N-2: 
        if a[0] + K -a[i+1] > max: 
            max = a[0] + K -a[i+1]
print(K-max)

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

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

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

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

m = 10 ** 12
for i in range(1,len(a)-1):
    r = k - abs(a[i] - a[i-1])
    l = k - abs(a[i] - a[i+1])
    if min(r,l) <= m:
        m = min(r,l)

r = abs(a[-1] - a[0])
l = k - abs(a[1] - a[0])
if min(r,l) <= m:
    m = min(r,l)

r = k - abs(a[-1] - a[-2])
l = abs(a[-1] - a[0])
if min(r,l) <= m:
    m = min(r,l)
    
print(m)

Solution (Python):


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

     l = sorted(l)
     diff_max = 0
     for i in range(len(l)-1):
          diff = l[i+1] - l[i]
          if diff_max < diff:
               diff_max = diff
     
     ans = k - diff_max
     last = l[-1] - l[0]
     if last < ans:
          ans = last
     print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):

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

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

print(k - max(dist))

Solution (Python):


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

A=list(map(int,input().split()))
B=list(map(lambda x:x+K,A))

A+=B

ans=10**9
for i in range(N):
    ans=min(ans,A[i+N-1]-A[i])
    

print(ans)

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

Solution (Python):
import numpy as np
import heapq

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

As = np.asarray(input().split(), dtype=np.int32)

H = []

for i in range(len(As)):
    if i == 0:
        heapq.heappush(H, As[-1]-k-As[0])
    else:
        heapq.heappush(H, As[i-1] - As[i])

print(k + H[0])

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

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

max_d = A[0] + (K-A[-1])
for n in range(1,N):
  _ = A[n-1]
  __ = A[n]
  max_d = max(__-_, max_d)

ans = K-max_d
print(ans)

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

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

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

print(k-temp)

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

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

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

Solution (Python):




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

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


if __name__ == "__main__":
    main()

Solution (Python):
k, n = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]



def gen(a):
    for i, d in enumerate(a):
        if i < n - 1:
            yield a[i + 1] - d
        elif i == n - 1:
            yield k + a[0] - d


max_d = max(gen(a))
print(k - max_d)

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

Solution (Python):
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
l = []

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

print(sum(l) - max(l))

Solution (Python):
k, n = map(int,input().split())
A = list(map(int,input().split()))
ans = 10**9
ans = min(ans, A[-1] - A[0])

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

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

Solution (Python):
def main():
    import sys
    input=sys.stdin.readline
    k,n=map(int,input().split())
    A=list(map(int,input().split()))
    m=0
    for i in range(n-1):
        m=max(m,A[i+1]-A[i])
    m=max(m,A[0]+k-A[-1])
    print(k-m)
if __name__ == '__main__':
    main()

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

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

tmp = 0
for i in range(n-1):
    if abs(a_list[i] - a_list[i+1]) > tmp:
        tmp = abs(a_list[i] - a_list[i+1])
        max_index = i

if (a_list[0] + (k-a_list[-1])) > tmp:
    tmp = a_list[0] + (k-a_list[-1])
    max_index = n-1

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

Solution (Python):
import sys

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

max_diff = A[1] - A[0]

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

diff = A[0] + (K - A[N - 1])
if diff > max_diff:
    max_diff = diff

ans = K - max_diff

print(ans)

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




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

gapmax = 0

for i, gap in enumerate(gaps):
    
    gapmax = max(gapmax, gap)
print(K - gapmax)

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

Solution (Python):
import numpy as np
k,n=map(int,input().split())
A = np.array(list(map(int,input().split())))
B = np.append(A[1:],A[0]+k)-A

print(np.sum(B)-np.max(B))

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

Solution (Python):


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

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

    print(k - max(b))

if __name__ == '__main__':
    main()

Solution (Python):
m,n=tuple(map(int,input().split(" ")))
lis=list(map(int,input().split(" ")))
lis.append(lis[0]+m)
min=0
for i in range(n):
    if(lis[i+1]-lis[i]>min):
        min=lis[i+1]-lis[i]
print(m-min)

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

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

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

if a[0] + k - a[n-1] > ans:
  ans = a[0] + k - a[n-1]
  
print(k - ans)

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

s.remove(max(s))

print(sum(s))

Solution (Python):
def c160(k , n , al):

    ans = 0
    count = 0

    for i in range(n):
        if i == n - 1:
            count = max(count , (k - al[i]) + al[0])
        else:
            count = max(count , al[i + 1] - al[i])

    return k - count

def main():
    k , n = map(int , input().split())
    al = list(map(int , input().split()))
    print(c160(k , n , al))
if __name__ == '__main__':
    main()

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

Solution (Python):
import sys

input = sys.stdin.readline

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

Solution (Python):
k, n = map(int, input().split(' '))
a_list = list(map(int, input().split(' ')))
d_list = [a_list[i + 1] - a_list[i] for i in range(len(a_list) - 1)]
d_list.append(k + a_list[0] - a_list[-1])
sorted_d_list = sorted(d_list)
print(sum(sorted_d_list[:-1]))

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

	m = 0

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

	if K - A[N-1] + A[0] > m:
		m = K - A[N-1] + A[0]

	print(K-m)

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

print(k - max(box))