CodeEff/ECCO huggingface dataset:
# Original solution
K,N=list(map(int,input().split()))
A=list(int(i) for i in input().split())
A.append(A[0])
#print(A)

a=[]
for i in range(N-1):
  a.append(abs(A[i]-A[i+1]))
  #print(a)
a.append(abs(K-A[N-1]))
#print(a)

ans=1000
tmp=0
for i in range(N):
  tmp=sum(a)-a[i]
  if tmp<ans:  
    ans=tmp
    #print(tmp)
print(ans)


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

A.sort()
A.append(A[0]+K)
dif = 0
for i in range(1, N+1):
  dif = max(dif, A[i] - A[i-1])
  #print(dif)
print((K-dif))
# Original solution
import sys

K,N = list(map(int, sys.stdin.readline().strip().split()))
A = list(map(int, sys.stdin.readline().strip().split()))
# K = 20
# N = 3
# A =[0, 5, 15]
# A =[5, 10, 15]

distance = []
# 家間距離
for i,a in enumerate(A):
    if i == len(A) - 1:
        distance.append(K - A[i] + A[0])
    else:
        distance.append(A[i+1] - A[i])

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


# Optimized solution
K,N,*A = list(map(int, open(0).read().split()))

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

ans.append(A[0] + K - A[-1])
ans.sort()
ans.pop()

print((sum(ans)))
# Original solution
max,n = list(map(int,input().split()))
l = list(map(int,input().split()))

double = [ i + max for i in l]
ll = l + double

sum = 0
home = 0
ans = 9999999
for i in range(n):
    for j in range(n-1):
        sum += ll[i+j+1] - ll[i+j]
        #print('ll[i+j+1]%d  -  ll[i+j]%d= sum%d    i=%d  j=%d'%(ll[i+j+1],ll[i+j],sum,i,j))

    ans = sum if sum < ans else ans
    #ans = min(ans,sum)
    sum = 0
    
print(ans)

# Optimized solution
k,n = list(map(int,input().split()))
l = list(map(int,input().split()))

ll = l[:]

ll.append(k)
ll.pop(0)
s = []
x = (l[0] + k) - l[-1]


for i,j in zip(l,ll):
    s.append(j - i)

print((k - max(s+[x])))
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))

diff = A[0] + K - A[-1]
ans = A[-1] - A[0]
for i in range(1, N):
    dist = K - A[i] + A[i - 1]
    if dist < ans:
        ans = dist

print(ans)


# Optimized solution
import sys


def input():
    return sys.stdin.readline().strip()


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

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


main()
# Original solution
k, n = list(map(int, input().split()))
a = [int(i) for i in input().split()]
diff_a = [0] * n
diff_a[0] = (a[0] + k) - a[n - 1]
for i in range(1, n):
    diff_a[i] = a[i] - a[i - 1]
print((k - max(diff_a)))

# Optimized solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a += [k + a[0]]
print((k - max([a[i + 1] - a[i] for i in range(n)])))
# Original solution
K, N = list(map(int, input().split()))

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

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

for i in range(1,N+1):
    if i == N:
        tmp = K - A[i-1]
    else:
        tmp = A[i] - A[i-1]
    # print(i, tmp)
         
    if minV < tmp:
        minV = tmp
        minI = i

# print(minI)
ans = 0

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


# Optimized solution
K, N = list(map(int, input().split()))
A = [int(x) for x in input().split()]

maxV = 0
ans = 0
for i in range(N-1):
    tmp = A[i+1] - A[i]
    ans += tmp
    maxV = max(tmp, maxV)
ans += K - A[N-1]+A[0]
# print(ans)
maxV = max(maxV, K - A[N-1]+A[0])
print((ans - maxV))
# Original solution
t,n=list(map(int,input().split()))
x=list(map(int,input().split()))
xx=[i+t for i in x]
x=x+xx
dis=[]
for i in range(2*n-1):
    dis.append(x[i+1]-x[i])

ans=[]
for j in range(2*n-2):
    ans2=dis[j:j+n-1]
    ans.append(sum(ans2))
print((min(ans)))

# Optimized solution
t,n=list(map(int,input().split()))
x=list(map(int,input().split()))
x.insert(0,x[-1]-t)
dis=[x[i+1]-x[i] for i in range(n)]
print((sum(dis)-max(dis)))
# Original solution
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
l = k - a[n-1] + a[0]
t = 0

for i in range(n):
    p = 0
    j = i + 1

    while True:
        if j == n:
            j = 0

        if j == i:
            break

        if j == 0:
            p += l
        else:
            p += a[j] - a[j-1]

        j += 1

    if t == 0:
        t = p
    else:
        if t > p:
            t = p

print(t)

# Optimized solution
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
t = 10 ** 6

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

for i in range(len(a)-n+1):
    t = min(t,a[i+n-1] - a[i])

print(t)
# Original solution
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = 10 ** 8
for i in range(n-1):
    a.append(a[i]+k)
for i in range(n):
    ans = min(ans,a[i+n-1] - a[i])
print(ans)

# Optimized solution
k,n = list(map(int,input().split()))
a = list(map(int,input().split()))
for i in range(n-1):
    a.append(k+a[i])
ans = a[n-1]-a[0]
for i in range(n,n*2-1):
    ans = min(ans,a[i]-a[i-n+1])
print(ans)
# Original solution
# Problem C - Traveling Salesman around Lake

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

# initialization
min_cost = 10**8
a_len = len(a_list)

for i in range(len(a_list)):
    a = a_list[i]
    ushiro_i = 0
    mae_i = 0
    # 1つ後ろを取る
    if i-1<0:
        ushiro_i = a_len - 1
    else:
        ushiro_i = i - 1

    # 1つ前を取る
    if i+1>=a_len:
        mae_i = 0
    else:
        mae_i = i + 1

    # update
    kouho_1 = 0
    kouho_2 = 0
    if a_list[mae_i]-a_list[i]<0:
        kouho_1 = K - (a_list[mae_i] - a_list[i] + K)
    else:
        kouho_1 = K - (a_list[mae_i] - a_list[i])
    if a_list[i]-a_list[ushiro_i]<0:
        kouho_2 = K - (a_list[i] - a_list[ushiro_i] + K)
    else:
        kouho_2 = K - (a_list[i] - a_list[ushiro_i])
        
    min_cost = min(min_cost, kouho_1, kouho_2)

# output
print(min_cost)


# Optimized solution
# Problem C - Traveling Salesman around Lake

# input
K, N = list(map(int, input().split()))
A_list = list(map(int, input().split()))
list_len = len(A_list)

# initialization
min_ans = 10**18

# search
for i in range(list_len):
    mae_i = 0
    if i==list_len-1:
        mae_i = 0
    else:
        mae_i = i + 1

    kyori = 0
    if A_list[mae_i]-A_list[i]<0:
        kyori = K - (A_list[mae_i] - A_list[i] + K)
    else:
        kyori = K - (A_list[mae_i] - A_list[i])
    min_ans = min(min_ans, kyori)

# output
print(min_ans)
# Original solution
List = list(map(int, input().split()))
A = list(map(int, input().split()))

for i in range(0,List[1]):
    A.append(A[i]+List[0])

D = []
for i in range(0,List[1]):
    D.append(A[i+List[1]-1]-A[i])

print((min(D)))

# Optimized solution
List = list(map(int, input().split()))
A = list(map(int, input().split()))

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

D.append(A[0]+List[0]-A[List[1]-1])

print((List[0]-max(D)))
# Original solution
from sys import stdin


inp = stdin.readline

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

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

distance = [0]*(k + 2)

for i in range(k - 1):
    distance[1+i] = array[i+1] - array[i]
if array[0] == 0:
    distance[k] = 20-array[k-1]
else:
    distance[k] = abs(array[k - 1] - array[0])
distance[0] = distance[-2]
distance[-1] = distance[1]

minimum = 999999999

for i in range(k + 1):
    total = sum(distance[i:i+k-1])
    if minimum > total:
        minimum = total

print(minimum)


# Optimized solution
from sys import stdin


inp = stdin.readline

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

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

distance = [0]*(k * 2)

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

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

minimum = 999999999

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

print(minimum)
# Original solution
from sys import stdin


inp = stdin.readline

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

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

distance = [0]*(k * 2)

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

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

minimum = 999999999

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

print(minimum)


# Optimized solution
from sys import stdin


inp = stdin.readline

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

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

distance = [0]*(k * 2)

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

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

print((n-max(distance)))
# Original solution
k, n = list(map(int, input().split()))
l = list(map(int, input().split()))
c = n*(n-1)//(n-1)
s = [0 for _ in range(c)]
for i in range(c):
  for j in range(n-1):
    if l[j+1] == 0:
      s[i] += abs(k-l[j])
    else:
      s[i] += abs(l[j+1]-l[j])
  temp = l.pop(0)
  l.append(temp)
print((min(s)))

# Optimized solution
k,n = list(map(int, input().split()))
l = list(map(int, input().split()))
d = []
for i in range(n):
  if i + 1 < n:
    d.append(l[i+1]-l[i])
  else:
    if 0 in l:
      d.append(k - l[i])
    else:
      d.append(k - l[i] + l[0])
sum = sum(d)
a = []
for i in range(n):
  a.append(sum - d[i])
print((min(a)))
# Original solution
k,n = list(map(int, input().split()))
l = list(map(int, input().split()))
d = []
for i in range(n):
  if i + 1 < n:
    d.append(l[i+1]-l[i])
  else:
    if 0 in l:
      d.append(k - l[i])
    else:
      d.append(k - l[i] + l[0])
sum = sum(d)
a = []
for i in range(n):
  a.append(sum - d[i])
print((min(a)))

# Optimized solution
k, n = list(map(int, input().split()))
l = list(map(int, input().split()))
d = []
for i in range(n):
  if i+1<n:
    d.append(abs(l[i+1]-l[i]))
  else:
    if 0 in l:
      d.append(abs(k - l[i]))
    else:
      d.append(abs(k - l[i] + l[0]))
print((sum(d)-max(d)))
# Original solution
K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
L = []
T = K+A[0]-A[-1]
for i in range(N-1):
  L.append(A[i+1]-A[i])
L.append(T)
print((K-max(L)))

# Optimized solution
K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
L = [K+A[0]-A[-1]]
for i in range(N-1):
  L.append(A[i+1]-A[i])
print((K-max(L)))
# Original solution
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))

d = [None] * n
sum = 0
for i in range(n):
    if i == n-1:
        d[n-1] = k - a[n-1] + a[0]
    else:
        d[i] = a[i+1] - a[i]
    sum += d[i]
ans = 2 * 10**11
for i in range(n):
    if sum - d[i] < ans:
        ans = sum - d[i]
print(ans)

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

d = [None] * n
sum = 0
for i in range(n):
    if i == n-1:
        d[n-1] = k - a[n-1] + a[0]
    else:
        d[i] = a[i+1] - a[i]
    sum += d[i]
print((sum - max(d)))
# Original solution
#C - Traveling Salesman around Lake WA(ヒント)
K,N = list(map(int,input().split()))
A = list(map(int, input().split()))

A[N+1:] = A#Aを二回繰り返して円を表現
A_reverse = reversed(A)

distance_list = []
for i in range(N):#スタート地点の数
    distance = 0
    for j in range(N-1):#道の数
        distance += abs(A[i+1] - A[i])#道の距離
    distance_list.append(distance)
for k in range(N):#逆回りについて
    distance = 0
    for j in range(N-1):
        distance += abs(A[i+1] - A[i])
    distance_list.append(distance)
print((min(distance_list)))

# Optimized solution
#C - Traveling Salesman around Lake WA(ヒント)
K,N = list(map(int,input().split()))
A = list(map(int, input().split()))

A[N+1:] = [A[0] + K]#Aを二回繰り返して円を表現

distance_list = []
for i in range(N):
    distance = abs(A[i] - A[i+1])
    distance_list.append(distance)
distance_list.remove(max(distance_list))
print((sum(distance_list)))
# Original solution
K, N = list(map(int, input().split(' ')))
A_ls = list(map(int, input().split(' ')))
l, max_l = 0, 0
for i in range(N):
    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % len(A_ls)]
    max_l = max(max_l, l)
print((K - max_l))

# Optimized solution
K, N = list(map(int, input().split(' ')))
A_ls = list(map(int, input().split(' ')))
rst = 0
for i in range(N):
    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]
    if l < 0:
        l = A_ls[(i + 1) % len(A_ls)] + K - A_ls[i]
    rst = max(rst, l)
print((K - rst))
# Original solution
K, N = list(map(int, input().split(' ')))
A_ls = list(map(int, input().split(' ')))
max_l = -1
for i in range(N):
    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % len(A_ls)]
    if max_l == -1:
        max_l = l
    else:
        max_l = max(max_l, l)
print((K - max_l))

# Optimized solution
K, N = list(map(int, input().split(' ')))
A_ls = list(map(int, input().split(' ')))
max_l = 0
for i in range(N):
    l = A_ls[(i + 1) % N] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % N]
    max_l = max(max_l, l)
print((K - max_l))
# Original solution
K, N = list(map(int,input().split()))
#K: 湖1周の長さ, N:湖の周りにある家の数

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

ans = 10**10
for i in range(N):
    for j in range(2):
        if j == 0: #時計周り
            if i == 0:
                dif = A[0]+(K-A[-1])
            else:
                dif = A[i] - A[i-1]
            ans = min(ans, K-dif)
        else: #半時計周り
            if i == N-1:
                dif = A[0]+(K-A[-1])
            else:
                dif = A[i+1] - A[i]
            ans = min(ans, K-dif)

print(ans)

# Optimized solution
K, N = list(map(int,input().split()))
#K: 湖1周の長さ, N:湖の周りにある家の数

A = list(map(int, input().split()))
A.sort()
dist = [A[0]+K-A[-1]] + [A[i]-A[i-1] for i in range(1, N)]
print((K-max(dist)))
# Original solution
# スペース区切りの文字列の入力
K, N = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

ans = 0
max = 0

for i in range(N):
  if(i == N - 1):
    A2 = K + A[0]
  else:
    A2 = A[i+1]
    
  # 距離が最大のもの以外をansに加算
  if(max < abs(A2 - A[i])):
      ans += max
      max = abs(A2 - A[i])
  else:
    ans += abs(A2 - A[i])

print(ans)


# Optimized solution
# スペース区切りの文字列の入力
K, N = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

# 最も離れた場所を求める(通常)
max_val = 0
for i in range(N-1):
  max_val = max(max_val, A[i+1] - A[i])
# 最も離れた場所を求める(先頭と終端)
max_val = max(max_val, K - A[N-1] + A[0])

print((K-max_val))
# Original solution
i = list(map(int, input().split()))
K = i[0]
N = i[1]

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

m = K

for i in range(N):
  for j in range(i,N):
    t = max(abs(s[i] - s[j]), K - abs(s[i] - s[j]))
    if t <= m:
      m = t
      
print(m)

# Optimized solution
i = list(map(int, input().split()))
K = i[0]
N = i[1]

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

m = K

for i in range(N - 1):
  t = max(abs(s[i] - s[i + 1]), K - abs(s[i] - s[i + 1]))
  if t <= m:
     m = t

if max(abs(s[0] - s[N - 1]), K - abs(s[0] - s[N - 1])) <= m:
  m = max(abs(s[0] - s[N - 1]), K - abs(s[0] - s[N - 1]))

print(m)
# Original solution
circum_total = [int(n) for n in input().split()]

circum = circum_total[0]
total = circum_total[1]

distances = [int(n) for n in input().split()]

def dist_from_node_forward(node_index, distances):
    forward_distances = distances[node_index:]
    forward_distances.extend([n + circum for n in distances[:node_index]])
    #print(forward_distances)
    dist = 0
    for index in range(1,len(forward_distances)):
        dist += forward_distances[index] - forward_distances[index-1]
    #print(dist)
    return(dist)

def dist_from_node_backward(node_index, distances):
    before = [distances[node_index]]
    before.extend(list(reversed(distances[:node_index])))
    before.append(0)
    #print(before)
    after = list(distances[node_index:])
    after.pop(0)
    #print(after)
    before.extend(list(reversed([n - circum for n in after])))
    #print(before)
    dist = 0
    for index in range(1,len(before)):
        dist += before[index-1] - before[index]
    #print(dist)
    return(dist)

def mindist(node_index, distances):
    return min(dist_from_node_forward(node_index,distances),dist_from_node_backward(node_index,distances))

minimum = circum

for i in range(len(distances)):
    minimum = min(minimum,mindist(i,distances))

print(minimum)


# Optimized solution
circum_total = [int(n) for n in input().split()]

circum = circum_total[0]
total = circum_total[1]

distances = [int(n) for n in input().split()]

mindist = circum
for index in range(len(distances)):
    if index == len(distances) - 1:
        rightdist = distances[0] + circum - distances[index]
    else:
        rightdist = distances[index+1] - distances[index]
    if index == 0:
        leftdist = distances[index] - distances[index-1] + circum
    else:
        leftdist = distances[index] - distances[index-1]
    maxdist = max(rightdist,leftdist)
    mindist = min(mindist, circum - maxdist)

print(mindist)
# Original solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
s=0
b=[0]*(n-1)
for i in range(n-1):
  b[i]=a[i+1]-a[i]
  
b.append(k+a[0]-a[n-1])

c=sorted(b,reverse=True)
for i in range(1,n):
  s+=c[i]
print(s)

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

b.sort()
b[-1]=0
print((sum(b)))
# Original solution
k,n  = list(map(int,input().split()))
diff = lambda x:x[1]-x[0]
a = list(map(int,input().split()))
distance = a+[a[0]+k]
diffs = [diff(distance[i:i+2]) for i in range(len(distance)-1)]
diffs = diffs + [diffs[i] for i in range(0,n-2)]
sums = [sum(diffs[i:i+n-1]) for i in range(len(diffs)-(n-2))]
print((min(sums)))

# Optimized solution
k,n  = list(map(int,input().split()))
diff = lambda x:x[1]-x[0]
a = list(map(int,input().split()))
distance = a+[a[0]+k]
diffs = [diff(distance[i:i+2]) for i in range(len(distance)-1)]
print((k - max(diffs)))
# Original solution
k,n=list(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[-1]+A[0])
print((sum(l)-max(l)))

# Optimized solution
k,n=list(map(int,input().split()))
A=list(map(int,input().split()))
l=[k-A[-1]+A[0]]
[l.append(A[i+1]-A[i]) for i in range(n-1)]
print((k-max(l)))
# Original solution
n, k = list(map(int,input().split()))
a = list(map(int,input().split()))

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

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

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

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

print((min(k-counter,a[n-1]-a[0])))

# Optimized solution
k,n=list(map(int,input().split()))
a=list(map(int,input().split()))
a.append(a[0]+k)
cnt=0

for i in range(n):
  cnt=max(cnt,a[i+1]-a[i])
print((k-cnt))
# Original solution
(K, N) = list(map(int, input().split()))
A = list(map(int, input().split()))
A.append(A[0]+K)
A_next = []

max = 0
result = 0
j = 0
k = 0
#print(A)
for x in range(1, len(A)):
  #print(x)
  #print(A[x]-A[x-1])
  #print(A[x-1])
  #print(A_next)
  A_next.append(abs(A[x] - A[x-1]))
  if max < A_next[j]:
    max = A_next[j]
    #print(max)
    k = j
  j += 1
#print(k)
#print(A_next)
for x in range(len(A_next)):
  if x == k:
    pass
  else:
    result += A_next[x]
print(result)


    

# Optimized solution
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(N+a[0])
#print(a)
max = 0
for i in range(len(a)-1):
  p = abs(a[i]-a[i+1])
  max = p if max < p else max
print((N-max))
# Original solution
K, N = list(map(int, input().split()))
a = list(map(int, input().split()))

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

total = []
for i in range(len(dist)):
    total.append(sum(dist) - dist[i])

print((min(total)))


# Optimized solution
K, N = list(map(int, input().split()))
a = list(map(int, input().split()))

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

ans = sum(dist) - max(dist)

print(ans)
# Original solution
import statistics

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

for i in a:
  target = i
  tmp = [abs(j - target) if abs(j - target) < abs(j - (target + k)) else abs(j - (target + k)) for j in a]
  dist.append(sum(tmp))
print((min(dist)))

# Optimized solution
import statistics

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

for num, i in enumerate(a[:]):
  if num == (n - 1):
    dist.append(abs((k + a[0]) - i))
  else:
    dist.append(abs(a[num + 1] - i))
ind = dist.index(max(dist))
dist.pop(ind)
print((sum(dist)))
# Original solution
K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
B = []
C = []
for i in range(N-1):
  B.append(A[i+1]-A[i])
B.append(A[0]+K-A[N-1])
for i in range(N-1):
  C.append(min(B))
  B.remove(min(B))
print((sum(C)))

# Optimized solution
K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
B = []
C = []
for i in range(N-1):
  B.append(A[i+1]-A[i])
B.append(A[0]+K-A[N-1])
B.remove(max(B))
print((sum(B)))
# Original solution
K, N = list(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))

# Optimized solution
K, N = list(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))
# Original solution
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def rev(x, limit):
    s = 0
    for i in range(len(x) - 1):
        if x[i] == 0:
            x[i] = limit
        s += abs(x[i] - x[i + 1])
        # print(s)
    return s


def main():
    k, n = list(map(int, sys.stdin.readline().split()))
    a = list(map(int, sys.stdin.readline().split()))
    daf = a[-1] - a[0]
    minn = min(daf, int(10e10))
    for i in range(1, n):
        grab = [a[i]] + a[:i] + a[i + 1:]
        #print(grab)
        minn = min(rev(grab, k), minn)
    print(minn)


if __name__ == "__main__":
    #IOE()
    main()


# Optimized solution
#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    k, n = list(map(int, sys.stdin.readline().split()))
    a = list(map(int, sys.stdin.readline().split()))
    minn = min(a[n - 1] - a[0], int(10e10))
    for i in range(n):
        ans = (a[n - 1] - a[i]) + (k + a[i - 1] - a[n - 1])
        minn = min(ans, minn)
    print(minn)


if __name__ == "__main__":
    # IOE()
    main()
# Original solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
x = A[:]
k = -1
for _ in range(N):
    x = x[1:] + x[:1]
    diff = sum([min(K - abs(x[i] - x[i+1]), abs(x[i] - x[i+1])) for i in range(N - 1)])
    if k == -1 or k > diff:
        k = diff
print(k)

# Optimized solution
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
diff = [min(K - abs(A[i] - A[i+1]), abs(A[i] - A[i+1])) for i in range(-1, N - 1)]
diff.sort()
print((sum(diff) - diff[-1]))