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

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

Solution (Python):
def main():
  k, N = (int(x) for x in input().split())
  min_LIST = []
  LIST = []
  LIST = list(map(int,input().split()))
  x = 0

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(x <= min_LIST[i]):
      x = min_LIST[i]

  if(N != 2):
    min_LIST.append(LIST[0] + (k - LIST[-1]))
    if(x <= (LIST[0] + (k - LIST[-1]))):
      x = (LIST[0] + (k - LIST[-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()

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

Solution (Python):
def myAnswer(K:int,N:int,A:list) -> int:
   distance=[]
   pre = A.pop(0)
   MIN = pre
   for a in A:
      distance.append(a - pre)
      pre = a
   distance.append(MIN + K - A[-1])
   distance.sort()
   return sum(distance[:N - 1])

def modelAnswer():
   tmp=1
def main():
   K,N = map(int,input().split())
   A = list(map(int,input().split()))
   print(myAnswer(K,N,A[:]))


if __name__ == '__main__':
   main()

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

Solution (Python):
def main():

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

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



if __name__ =='__main__':
    main()

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())
a=list(map(int,input().split()))
dis=[]

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

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

Solution (Python):
a, b = map(int, input().split())
homes = input().split()
homes_number = [int(s) for s in homes]
long = []
for i in range(b-1):
    x = homes_number[i+1] - homes_number[i]
    long.append(x)
y = a - homes_number[b-1] + homes_number[0]
long.append(y)
print(a-max(long))

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

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

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(lambda x: x + k, a))
a.extend(b)
min = float('inf')
for i in range(n):
    if a[i + n - 1] -  a[i] < min:
        min = a[i + n - 1] - a[i]
print(min)

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

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

print(k - max(t))

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

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

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

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

if(max < (a[0] + (k-a[n-1]))):
    max = (a[0] + (k-a[n-1]))

print(k-max)

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

x = k + A[0] - A[n-1]

for i in range(1,n):
    y = A[i]-A[i-1]
    if y > x:
        x = y

print(k-x)

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

Solution (Python):
k,n = map(int,input().split())
a = list(map(int,input().split()))
a += [a[0]+k]
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()))
maxDis = 0
for x in range(len(A)):
    if x != len(A) - 1:
        maxDis = max(maxDis, A[x + 1] - A[x])
    else:
        maxDis = max(maxDis, A[0] - A[x] + K)
print(K - maxDis)

Solution (Python):
import sys
read = sys.stdin.buffer.read
 
K, N, *A = map(int, read().split())
A.append(A[0] + K)
gap = max(y - x for x, y in zip(A, A[1:]))
print(K - gap)

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

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
 
K, N, *A = map(int, read().split())
A += [A[0] + K]
gap = max(y-x for x,y in zip(A, A[1:]))
print(K - gap)

Solution (Python):
import sys
read = sys.stdin.buffer.read
 
K, N, *A = map(int, read().split())
 
max_distance = K + (A[0] - A[-1])
 
for i in range(1, N):
  max_distance = max(max_distance, A[i] - A[i - 1])
  
print(K - max_distance)

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

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

for i in range(1,n-1):
  tmp = min(k-(a[i]-a[i-1]), k-(a[i+1]-a[i]))
  mi = min(tmp,mi)

print(mi)

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

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

a.append(a[0]+k)

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

print(k-dist)

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



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

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

x_list = A_list[:-1]

y_list = A_list[1:]

diff_list = list(map(lambda x,y: y - x, x_list, y_list))
diff_list.append(int(K-A_list[-1] + A_list[0]))

print(K - max(diff_list))

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

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

print(k - max_d)

Solution (Python):
p,q= input().split()
a,b =(int(p), int(q))
f = map(int,input().split())
f_list = list(f)
out_dis =f_list[0] + a -f_list[b-1]
dis_list=[out_dis]
for i in range(b-1):
    dis = f_list[i+1]-f_list[i]
    dis_list.append(dis)

print(int((sum(dis_list)-max(dis_list))))

Solution (Python):

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

x = max(l)
print(k-x)

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

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

print(ans)

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

Solution (Python):
K,n = map(int,input().split(' '))
A = list(map(int, input().split(' ')))
A.sort()
maxD = K-(A[-1]-A[0])
i = 0
while i < n-1:
    if A[i+1]-A[i] > maxD:
        maxD = A[i+1]-A[i]
    i += 1
print(K-maxD)

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

    value_list = []
    value_list.append(K - ie_list[-1] + ie_list[0])
    count = 0
    while count != N - 1:
        value_list.append(ie_list[count + 1] - ie_list[count])
        count += 1
    print(K - max(value_list))

resolve()

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

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

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

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


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


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


ans = k - distance_max
print(ans)

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

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

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

distence_list = [x for x in [ A[i+1] - A[i] for i in range(len(A)-1)]]

m = max(distence_list)

x = K - A[-1] + A[0]

if m > x:
  print(K-m)
else:
  print(A[-1] - A[0])

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

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=(int(x) for x in input().split())

a=list(map(int,input().split()))
d0=0
dmax=0
for i in range(n):
    d=a[i]-d0
    d0=a[i]
    if d>=dmax:
        dmax=d
d1=k-a[n-1]+a[0]
if d1>=dmax:
    print(str(k-d1))
else:
    print(str(k-dmax))

Solution (Python):







K, N = map(int, input().split())
A = [int(x) for x in input().split()]
l = 0
A.append(A[0] + K)
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()))
max = 0

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

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

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

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

print(k-max_dis)

Solution (Python):
k,n=map(int,input().split())
al=list(map(int,input().split()))
d=[a_-a for a,a_ in zip(al, al[1:])]+[k-al[-1]+al[0]]
print(sum(sorted(d)[:-1]))

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)



def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


def in_ints():
    return (map(int, input().split()))


k, n  = in_ints()
a = in_list()

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

    mx = max(mx, abs(a[i]- next))
    ans  += abs(a[i]- next)


print(ans - mx)

Solution (Python):
a, b = map(int, input().split())
r = list(map(int, input().split()))
ans = 0
num_m = 0

for i in range(1, len(r)):
    k = r[i] - r[i-1]
    ans += k
    if k > num_m:
        num_m = k
k = r[0] + a - r[-1]
ans += k
if k > num_m:
    num_m = k

print(ans - num_m)

Solution (Python):
input_KN = list(map(int, input().split()))
K = input_KN[0] 
N = input_KN[1] 
ienokyori= list(map(int, input().split()))
ienokyori.append(K)
if(ienokyori[0]==0):
    max_ienokyori = 0 
else:
    max_ienokyori = ienokyori[0]+abs(ienokyori[-1]-ienokyori[-2])
for i in range(N):
    if(max_ienokyori < abs(ienokyori[i]-ienokyori[i+1])):
        max_ienokyori = abs(ienokyori[i]-ienokyori[i+1])

print(K-max_ienokyori)

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







diffs = []
for i in range(n):
  if i+1 <= n-1:
    diffs.append(data[i+1] - data[i])
  
  if i == n-1:
    diffs.append(data[0] + k - data[i])

print(sum(diffs) - max(diffs))

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

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

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

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

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

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])
ans = k - m
print(ans)

Solution (Python):

n, k = map(int, input().split())
houses = [int(x) for x in input().split()]
houses.sort()

diffList = []

for index, pos in enumerate(houses):
    diffList.append( (pos - houses[index-1]) % n)

m = max(diffList)

print(n - max(diffList))

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

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

print(k - ans)

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

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

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

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

print(k-longest)

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

A = []
for i in input().split():
  A.append(int(i))

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

print(K-mergin[-1])

Solution (Python):
date0=input().rstrip().split(" ")
date1=input().rstrip().split(" ")
list0=[]
list1=[]
for i in range(int(date0[1])):
  list0.append(int(date1[i]))
list0=sorted(list0)
for j in range(int(date0[1])-1):
  list1.append(list0[j+1]-list0[j])
list1.append(int(date0[0])-list0[int(date0[1])-1]+list0[0])
list1=sorted(list1,reverse=True)
print(int(date0[0])-list1[0])

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

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

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

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

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

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

Solution (Python):
from collections import deque
K, N = map(int, input().split())
A = deque(map(int, input().split()))
A.appendleft(K - A[-1] + A[0])
sa_max = A[0]
for i in range(1, N):
    sa = A[i + 1] - A[i]
    if sa_max < sa:
        sa_max = sa
print(A[-1] + A[0] - (sa_max + A[1]))

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

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

print(k - max(a_sub))

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

a = list(int(i) for i in input().split())
s = []
for i in range(n-1):
   s.append(a[i+1] - a[i])

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

print(k - max(s))

Solution (Python):
round,house_num = map(int,input().split())

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

min = distance[-1] - distance[0]

for i in range(house_num - 1):
  if round-(distance[i+1]-distance[i]) < min:
    min = round-(distance[i+1]-distance[i])
print(min)

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

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

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

distance.append(A[0] + K - A[-1])
most = max(distance)
output = K - most
print(output)

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

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

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

if __name__ == "__main__":
    main()

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

import numpy as np
A=np.array(A)

ans=max(np.diff(A).max(), K-A[-1]+A[0])
if ans<=K/2:
    print(K-ans)
else:
    print(ans)

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

Solution (Python):
[K,N]=[int(item) for item in input().split()]
array = [int(item) for item in input().split()]
max_=0
for i in range(N-1):
    dist=abs(array[i] - array[i + 1])
    if dist > max_:
        max_=dist

dist=array[0] + (K-array[N-1])
if dist > max_:
    max_=dist
print(K-max_)

Solution (Python):
k,n=map(int,input().split())
li=list(map(int,input().split()))
def l(m):
    x=li[m+1]-li[m]
    return(x)
list_l=[l(m) for m in range(n-1)]
list_l.append(li[0]+k-li[n-1])
list_l.sort()
list_l.reverse()
print(sum(list_l)-list_l[0])

Solution (Python):
a,b=map(int,input().split())
c=sorted(map(int,input().split()))
x=int(1)
dif=[]
while x<b:
    dif.append(c[x]-c[x-1])
    x=x+1
dif.append(a-max(c)+min(c))
print(a-max(dif))

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

print(K - max(diff))

Solution (Python):

k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
a = [i - a[0] for i in a]
d = []
for i in range(len(a)):
    if i == len(a)-1:
        d.append(k - a[i])
    else:
        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()))

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

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

a.append(kn[0]+a[0])
dist = []
for i in range(1, kn[1]+1):
    dist.append(a[i]-a[i-1])

dist[dist.index(max(dist))] = 0

print(sum(dist))

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
longest = 0
prev = a[0]
for h in a[1:]:
    dif = h - prev
    prev = h
    if dif > longest:
        longest = dif
b = k - a[-1] + a[0]
if longest <= b:
    longest = b
print(k - longest)

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

max = 0
i = 0
while i < n-1:
  if max < a[i+1] - a[i]:
    max = a[i+1] - a[i]
  i+=1

if max < (a[0] + k) - a[n-1]:
  max = (a[0] + k) - a[n-1]

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,a[i+1]-a[i])
print(k-d)

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 = list(map(int,input().split()))
re_a = []
for x in a_list:
    re_a.append(x-k)
a_list_add_re = re_a+a_list
result = []
x = n - 1
for i in range(len(a_list_add_re)-x):
    result.append(a_list_add_re[i+x] - a_list_add_re[i])
    
print(min(result))

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

for i in range(N):
    if i < N - 1:
        sa = A[i+1] - A[i]
    else:
        sa = A[0] + (K - A[N-1])
    gap.append(sa)
    i += 1
    
dist = max(gap)
ans = K - dist
print(ans)

Solution (Python):
K,N=map(int, input().split())
A=[int(i) for i in input().split()]
D=[0]*(N)
sum=0
for i in range(0, N-1):
  D[i]=A[i+1]-A[i]
  sum+=D[i]
D[N-1]=A[0]-A[N-1]+K
sum+=D[N-1]
dist=-1
for i in range(0, N):
  temp=sum-D[i]
  if(dist==-1):
    dist=temp
  else:
    dist=min(temp, dist)
print(dist)

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

lis = np.append(As, As[0] + K)
diff = np.diff(lis)
longest = diff.max()
print(K - longest)

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a = [a[-1] - k] + a + [k + a[0]]
    mt = 10 ** 10
    for i in range(1, len(a) - 1):
        mt = min(mt, k - abs(a[i] - a[i-1]), k - abs(a[i] - a[i+1]))
    print(mt)


if __name__ == '__main__':
    solve()

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

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

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


if __name__ == "__main__":
    main()

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

maximum=0
previous=0
for i in array:
    maximum=max(maximum,abs(previous-i))
    previous=i

maximum=max(maximum,k-array[-1]+array[0])

print(k-maximum)

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

d.remove(max(d))
print(sum(d))

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

A = list(map(int, input().split()))
A.append(A[0]+K)
dis = 0

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

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

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

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

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

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

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

a_list = a_list + list(map(lambda x: x + k, a_list))

mi_len = k
for i in range(len(a_list)-(n-1)):
    mov_len = a_list[i+(n-1)] - a_list[i]
    if mi_len > mov_len:
        mi_len = mov_len

print(mi_len)

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

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

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

Solution (Python):
k, n = map(int, input().split())
a = list(map(int, input().split()))
ma = -1
mi = -1
for i in range(n-1):
    if abs(a[i] - a[i+1]) > ma:
        ma = abs(a[i] - a[i+1])
        mi = i
flag = False
if abs(a[n-1]-k-a[0]) > ma:
    ma = abs(a[n-1]-k-a[0])
    mi = n-1
    flag = True
sum = 0
if flag:
    for i in range(0, n-1):
        sum += abs(a[i] - a[i+1])
else:
    for i in range(0, mi):
        sum += abs(a[i] - a[i+1])
    for i in range(mi+1, n-1):
        sum += abs(a[i] - a[i+1])
    sum += abs(a[n-1]-k-a[0])
print(sum)

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

Solution (Python):

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

Solution (Python):


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

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

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


def solve():
    k, n = MI()
    A = LI()

    path = [A[i] - A[int1(i)] for i in range(1, len(A))]
    l = A[-1] - A[0]
    path.append(min(l, k - l))
    

    ans = sum(path)
    if len(path) > 2:
        ans = ans - max(path)
    print(ans)


if __name__ == '__main__':
    solve()

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

min = k
for i in range(n):
  if A[i+n-1]>=A[i]: 
    d = A[i+n-1]-A[i]
  else:
    d = A[i+n-1]+k-A[i]
  if d<min: min = d
    
print(min)

Solution (Python):
import copy
from functools import reduce
k,n=map(int,input().split())
a=list(map(int,input().split()))
c=copy.copy(a)
c.insert(0,0)
a.insert(n,(k+a[0]))
b=list(map(lambda a,b:a-b,a,c))
d=reduce(lambda a,b:b if b>a else a,b)
print(k-d)

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

a = [ int (x) for x in input().split() ]

kyori_sa = []

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

print(k-max(kyori_sa))

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

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

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

mx = house[0] + (K - house[-1])

for i in range(N - 1):
	mx_sub = house[i + 1] - house[i]
	if mx_sub > mx:
		mx = mx_sub

print(K - mx)

Solution (Python):
firstInput=list(map(int,input().split()))
perimeter=firstInput[0]
numHouses=firstInput[1]
housesDistances=list(map(int,input().split()))
maxDistance=0
maxidx=1
totalDistance=0
for idx, house in enumerate(housesDistances[:-1]):
  distance=housesDistances[idx+1]-house
  totalDistance+=distance
  if distance>maxDistance:
      maxDistance= distance
      maxidx=idx+1

lastDistance= perimeter-housesDistances[-1]+housesDistances[0]
totalDistance+=lastDistance
if lastDistance>maxDistance:
  maxDistance=perimeter-housesDistances[-1]+housesDistances[0]
  maxidx=idx+1
print(totalDistance-maxDistance)