Problem:
 Score : 300 points    Problem Statement There is a circular pond with a perimeter of K meters, and N houses around them.  The i -th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.  When traveling between these houses, you can only go around the pond.  Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.      Constraints  2 \leq K \leq 10^6  2 \leq N \leq 2 \times 10^5  0 \leq A_1 < ... < A_N < K  All values in input are integers.         Input Input is given from Standard Input in the following format:  K  N  A_1  A_2  ...  A_N       Output Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.        Sample Input 1 20 3
5 10 15      Sample Output 1 10  If you start at the 1 -st house and go to the 2 -nd and 3 -rd houses in this order, the total distance traveled will be 10 .       Sample Input 2 20 3
0 5 15      Sample Output 2 10  If you start at the 2 -nd house and go to the 1 -st and 3 -rd houses in this order, the total distance traveled will be 10 .

Solution (Python):
import numpy as np


def solve():
    dists = np.zeros(n, dtype=int)
    for i in range(n):
        dists[i] = abs(a[(i+1)%n]-a[i%n])
        dists[i] = min(dists[i], k-dists[i])
    ans = k - np.max(dists)
    print(ans)
    return




k, n = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
solve()

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

check_l =[]
for i in range(len(a)-1):
	check_l.append(a[i+1]-a[i])

check_l.append(k+a[0]-a[n-1])
print(k-max(check_l))

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

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

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

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

Solution (Python):
import numpy as np
k,n=map(int,input().split())
a=list(map(int,input().split()))
a=np.array(a)
a1=abs(a[1:]-a[:-1])
a2=list(a1)
a2.append(abs(a[0]+k-a[-1]))
print(sum(a2)-max(a2))

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

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

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

Solution (Python):

if __name__ == "__main__":
    k, n = map(int, input().split())
    k = int(k)
    n = int(n)
    a = list(map(int, input().split()))
    ans = float('inf')

    for start in range(n):
        tmp = 0
        if start == 0:
            tmp = a[-1] - a[start]
        else:
            tmp = k - (a[start] - a[start - 1])
        if ans > tmp:
            ans = tmp

    print(ans)

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

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


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

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

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


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

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

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

tmp = 0

for i in range(1, len(a)):
    if a[i]-a[i-1] >=tmp:
        tmp = a[i] - a[i-1]

if a[0]+k-a[len(a)-1] > tmp:
    tmp = a[0]+k-a[len(a)-1]

print(k-tmp)

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

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

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

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()))
ma=0
for i in range(n-1):
    ma=max(ma,a[i+1]-a[i])
print(k-max(ma,a[0]+k-a[n-1]))

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

Solution (Python):
import numpy as np
k,n = map(int,input().split())
a = np.array(input().split(),dtype=int)
max = 0
for i in range(n):
    if max < (a[i]-a[i-1])%k:
        max = (a[i]-a[i-1])%k

print(k-max)

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

Solution (Python):
import numpy as np
k,n = map(int,input().split())
a = np.array(list(map(int,input().split())))
dif = a[1:] - a[:-1]
ans = k - max(k + a[0] - a[-1],dif.max())
print(ans)

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

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

A.append(K + A[0])

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

print(K - max(distance))

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

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

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

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


def max_distance_homes(homes):
    max_distance = 0
    for i in range(len(homes) - 1):
        distance = homes[i + 1] - homes[i]
        if max_distance < distance:
            max_distance = distance
    distance = k + homes[0] - homes[-1]
    if max_distance < distance:
        max_distance = distance
    return max_distance


print(k - max_distance_homes(home_positions))

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

b = []

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

b.append(k - sum(b))
print(k - max(b))

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

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

A = sorted(A)

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

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

print(K-load)

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




difn = []
difn.append(k-a[n-1]+a[0])

for i in range(1,n):
    difa = a[i]-a[i-1]
    difn.append(difa)
    
difn.sort(reverse = True)

print(k-difn[0])

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

B = []

for i in range(1, n):
    B.append(aa[i] - aa[i - 1])

B.append(k - sum(B))
print(k - max(B))

Solution (Python):
input_=[]
for i in range(2):
    input_.append(input())
    
K,N = list(map(int,input_[0].split()))
L = list(map(int,input_[1].split()))

dist=[0]*N

d = -1    
dist[d]=K + L[d+1]-L[d]
d_max = dist[d]

for d in range(N-1):
    dist[d] = L[d+1]-L[d]
    if d_max < dist[d]:
        d_max = dist[d]

ans=sum(dist)-d_max
print(ans)

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

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

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

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

Solution (Python):
K,N = 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(sum(d)-max(d))

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

Solution (Python):
a,b=map(int,input().split())
List=list(map(int,input().split()))
c=[]
time=0
for j in range(b-1):
  c.append(List[time+1]-List[time])
  time=time+1
c.append(List[0]+a-List[b-1])
print(a-max(c))

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

print(k-max(d))

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

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

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


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

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


print(K - max_len)

Solution (Python):

a = input()
b = input()
a_list = list(map(int, a.split()))
b_list = list(map(int, b.split()))
edges = []
for num in range(len(b_list)):
  if num == 0:
    edges.append(b_list[num])
  else:
    edges.append(b_list[num] - b_list[num - 1])

edges[0] = a_list[0] - b_list[-1] + b_list[0] 
edges[edges.index(max(edges))] = 0
print(sum(edges))

Solution (Python):
K,N=map(int,input().split())
A=list(map(int,input().split()))
M = 0
i = 0
while i<N-1:
    if M<(A[i+1]-A[i]):
        M=(A[i+1]-A[i])
    i+=1

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

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

D = [0] * K

for i in range(K-1):
    D[i] = S[i+1] - S[i]

D[-1] = (N - S[-1]) +S[0]
print(sum(D) - max(D))

Solution (Python):
k, n = map(int, input().split())
house_list = list(map(int, input().split()))
for i in range(n):
  house_list.append(house_list[i] - k)
tmp_list = sorted(house_list)
ans_list = []
for j in range(n + 1):
  ans_list.append(tmp_list[j + n - 1] - tmp_list[j])
print(min(ans_list))

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

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

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

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

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

print(K-m)

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

def check(K,N,A):
    a_dsn =[]
    sum = 0

    for a in range(len(A)-1):
        a_dsn.append(A[a+1] - A[a])
    
    a_dsn.append(A[0]+K-A[N-1])
    a_dsn.sort()
    
    
    for a in range(len(a_dsn)-1):
        sum += a_dsn[a]

    print(sum)
    



K,N,A = nyu()
check(K,N,A)

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

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

print(total)

Solution (Python):
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()))

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

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

Solution (Python):
k,n,*a=map(int,open(0).read().split())
a+=[k+a[0]]
print(k-max(y-x for x,y in zip(a,a[1:])))

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

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


print(K - max(space))

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

sum_num = 0
max_num = 0
tmp = K - A[-1] + A[0]
max_num = tmp
sum_num += tmp
diff.append(tmp)
for i in range(N - 1):
    tmp = A[i + 1] - A[i]
    diff.append(tmp)
    if tmp >= max_num:
        max_num =tmp
    sum_num += tmp

ans = sum_num - max_num
print(ans)

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

sum_num = 0
max_num = 0
max_num = K - A[-1] + A[0]
sum_num += K - A[-1] + A[0]
diff.append(K - A[-1] + A[0])
for i in range(N - 1):
    tmp = A[i + 1] - A[i]
    diff.append(tmp)
    if tmp >= max_num:
        max_num =tmp
    sum_num += tmp

ans = sum_num - max_num
print(ans)

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

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

B=[]

s=0

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

print(sum(B)-max(B))

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

l.append(K+l[0])
l.insert(0, 0)

lsub = list()
for i in range (1, N+2):
    lsub.append(l[i] - l[i-1])

print(K - max(lsub))

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

long=0

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

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

print(k-long)

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

l = []
for i in range(1, N+1):
    l.append(abs(A[i-1] - A[i]))
l.pop(l.index(max(l)))
print(sum(l))

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    K, N = list(map(int,_in[0].split(' ')))  
    A_arr = list(map(int,_in[1].split(' ')))  
    
    A_arr_diff = []
    for i in range(len(A_arr[1:])):
        A_arr_diff.append(A_arr[i+1]-A_arr[i])
    A_arr_diff.append(K-A_arr[-1]+A_arr[0])
    cnt = K - max(A_arr_diff)
    
    print(cnt)

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

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

diff=[]
for i in range(len(a_list)-1):
    diff.append(a_list[i+1]-a_list[i])
diff.append(k-(a_list[n-1]-a_list[0]))
diff.sort(reverse=True)
print(k-diff[0])

Solution (Python):
import sys

k, n, *a = map(int, sys.stdin.read().split())
a += [a[0] + k]

def main():
    d = [a[i+1] - a[i] for i in range(n)]
    print(k - max(d))

if __name__ ==  '__main__':
    main()

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


def max_distance_homes(homes):
    max_distance = 0
    for i in range(len(homes) - 1):
        distance = homes[i + 1] - homes[i]
        if max_distance < distance:
            max_distance = distance
    distance = min(homes[-1] - homes[0], k + homes[0] - homes[-1])
    if max_distance < distance:
        max_distance = distance
    return max_distance


print(k - max_distance_homes(home_positions))

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

def pairwise_distance(a, b, K):
  return b - a 
  
memo = {frozenset({i, i+1}): pairwise_distance(A[i], A[i+1], K) 
        for i in range(0, len(A) - 1)}

last_pair_distance = K - pairwise_distance(A[0], A[len(A) - 1], K)
if last_pair_distance > K / 2:
  memo[frozenset({0, len(A) - 1})] = K - last_pair_distance
else:
  memo[frozenset({0, len(A) - 1})] = last_pair_distance

min_dist = K - max(memo.values())
print(min_dist)

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

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

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

print(k - max(l))

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

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

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

dis = K - dis
print(dis)

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

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

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

for i in range(n):
    if kyori < a[i] - a[i-1]:
        kyori = a[i] - a[i-1]
print(k-kyori)

Solution (Python):
K,N = map(int,input().split())
num_list = list(map(int, input().split())) 
num_list.sort()
num_list_sort=num_list
maxa_b=0
for i in range(len(num_list_sort)):
  a=num_list_sort[i]
  if(i==len(num_list_sort)-1):
    b=num_list_sort[0]
  else:
    b=num_list_sort[i+1]

  a_b=min(max(a,b)-min(a,b),min(a,b)+K-max(a,b))
  if(maxa_b<=a_b):
    maxa_b=a_b
print(K-maxa_b)

Solution (Python):
k, n = map(int, input().split())
array = list(map(int,input().split()))
max_length = 0
sum_length = 0
for i in range(n - 1):
  length = array[i + 1] - array[i]
  sum_length += length
  if length > max_length:
    max_length = length
last_length = k - array[n - 1] + array[0]
sum_length += last_length
max_length = max(max_length, last_length)
print(sum_length - max_length)

Solution (Python):
X = input().split()
K = int(X[0])
N = int(X[1])
Y = input().split()
Z = []
for i in range(N):
    Z.append(int(Y[i]))
result = Z[0]+ K - Z[N-1]

for i in range(N):
    if result < Z[i] - Z[i-1]:
        result = Z[i] - Z[i -1]
    i = i + 1
print(K - result)

Solution (Python):
import numpy as np



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


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

distance.append(k - a[n-1] + a[0])


dis_max = np.max(distance)


ans = k - dis_max
print(ans)

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

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

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

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

BBB = []

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

print(K-max(BBB))

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

print(K - max(m, K - (lst[-1] - a)))

Solution (Python):
i = list(map(int, input().split()))
j = list(map(int, input().split()))
max = 0


for k in range(i[1]-1):
  buf1 = j[k+1]
  buf2 = j[k]
  if max < buf1 - buf2:
    max =  buf1 - buf2
if max < i[0] + j[0] - j[i[1]-1]:
  max =  i[0] + j[0] - j[i[1]-1]
max = i[0] - max
print(max)

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

Solution (Python):

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

Solution (Python):

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

print(k-d)

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

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

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

Solution (Python):
import os

def main():
	k,n = map(int, input().split())
	a_list = list(map(int, input().split()))
	min_total = 1000000
	for i in range(n):
		if i == 0:
			a_total = a_list[i-1] - a_list[i]
			b_total = a_list[i] + k - a_list[i+1]	
		else:
			if i == n-1:
				a_total = k - a_list[i] + a_list[i-1]
				b_total = a_list[i] - a_list[0]
			else:
				a_total = k - a_list[i] + a_list[i-1]
				b_total = a_list[i] + k - a_list[i+1]

		if (a_total < min_total) or (b_total < min_total):
			if a_total <= b_total:
				min_total = a_total
			else:
				min_total = b_total
	print(min_total)

if __name__ == "__main__":
	main()

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

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

result = 0
matagi = K - An[N - 1] + An[0]
if max(dist) >= matagi:
    result = sum(dist) + matagi - max(dist)
else:
    result = sum(dist)

print(result)

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

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

Solution (Python):

import sys
input = sys.stdin.buffer.readline


from collections import defaultdict

con = 10 ** 9 + 7

def getlist():
	return list(map(int, input().split()))


def main():
	K, N = getlist()
	A = getlist()
	dif = 0
	for i in range(N - 1):
		dif = max(dif, A[i + 1] - A[i])

	dif = max(dif, A[0] + K - A[-1])

	print(K - dif)


if __name__ == '__main__':
	main()

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

Solution (Python):





K,N = list(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()))

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

ans = k - x
print(ans)

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

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

max_space = 0

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

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

A.append(K+A[0])

max_range = 0

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

print(K - max_range)

Solution (Python):

url = "https://atcoder.jp//contests/abc160/tasks/abc160_c"

def main():
    lake, house = list(map(int, input().split()))
    dis = list(map(int, input().split()))
    ans = []
    for i in range(1, len(dis)):
        ans.append(dis[i] - dis[i - 1])
    ans.append(lake - dis[-1] + dis[0])
    print(sum(ans) - max(ans))

if __name__ == '__main__':
    main()

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

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

Solution (Python):
import numpy as np

k, n = map(int, input().split())
a = np.array([int(i) for i in input().split()])




d = np.diff(a)
diff_k = k - a[-1] + a[0]

if diff_k >= max(d):
    s = sum(d)
else:
    s = sum(d) - max(d) + diff_k

print(s)

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

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

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

c = max(b)
b.remove(c)

print(sum(b))

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

for i in range(k-1):
  s=l[i+1]-l[i]
  if s>max:
    max=s

s=n-l[-1]+l[0]
if s>max:
  max=s
print(n-max)

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

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

print(k-longest)

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

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

a.append(k+a[0])

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())) 
a += [a[0]+k]
b = []
for i in range(1,n+1):
    b.append(a[i]-a[i-1])
print(sum(b)-max(b))

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

su_list =[k-a_list[-1]+a_list[0]]

for i in range(n-1):
    su_list.append(a_list[i+1]-a_list[i])

print(k-max(su_list))

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

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

print(k-max(distance))

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

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


print(sum(diff) - max(diff))

Solution (Python):
k, _ = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
b = [ai + k - a[len(a) - 1] if i == 0 else ai - a[i - 1]  for i, ai in enumerate(a)]
print(k - max(b))

Solution (Python):
k, _ = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
b = [ai + k - a[len(a) - 1] if i == 0 else ai - a[i - 1]  for i, ai in enumerate(a)]
print(k - max(b))

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

A_list.append(a[0] + (k - a[n-1]))
print(k - max(A_list))

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

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

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

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

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

Solution (Python):
k_n = list(map(int,input().split()))
a = list(map(int,input().split()))
dis = []
total_dis = 0
max_dis = 0
for i in range(k_n[1]):
  if(i < (k_n[1]-1)):
    dis.append(a[i+1] - a[i])
  else:
    dis.append(k_n[0] - a[i] + a[0])
  
  if(max_dis <= dis[i]):
    max_dis = dis[i]
    
  total_dis+= dis[i]

total_dis -= max_dis
print(total_dis)

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

Solution (Python):
k,N = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
max = 0
for i in range(N-1):
  if (a[i+1]-a[i]) >= max:
    max = a[i+1]-a[i]
if max <  k -(a[N-1]-a[0]):
  max = k -(a[N-1]-a[0])
min = k-max
print(min)

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

Solution (Python):
perimeter,n = map(int, input().split())
v =list(map(int, input().split()))
diff = []
v.reverse()
for i in range(n-1):
    diff.append(v[i]-v[i+1])
diff.append(perimeter-(v[0]- v[n-1]))
print(perimeter - max(diff))

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

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

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

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

A = [int(a) for a in A];

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


B [0] = (K - A[len(A) -1]) + A[0];

max_B = max(B);
ans = K - max(B);

print(ans);