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):
def main():
    length, num = map(int, input().split())
    a = list(map(int, input().split()))
    max_dis = 0 
    first_h = a[0]
    prev_h = a[0]
    for h in a[1:]:
        dis = h - prev_h
        if dis > max_dis:
            max_dis = dis
        prev_h = h

    dis = (length - prev_h) + first_h
    max_dis = max(max_dis, dis)

    print(length - max_dis)


if __name__ == "__main__":
    main()

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

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

max_dis = 0
for i in range(N-1):
    max_dis = max(max_dis, A[i+1]-A[i])
    sum_dis = A[i+1] - A[i]

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

print K-max_dis

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

Solution (Python):
k,n=map(int,input().split())
P=list(map(int,input().split()))
SA=[0]*(n)

for i in range(n-1):
    SA[i]=P[i+1]-P[i]
SA[-1]=k-P[-1]+P[0]
print(k-max(SA))

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

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

print(k-max_diff)

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

import numpy as np
print(k - max(max(np.diff(a)), (a[0]-0)+(k-a[-1])))

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

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

print(min(dis))

Solution (Python):


k, n = map(int, input().split())
a_list = list(map(int, input().split()))
d_list = [a_list[0] + k - a_list[n - 1]]
max_d = a_list[0] + k - a_list[n - 1]
for i in range(0, n - 1):
    d_list.append(a_list[i + 1] - a_list[i])
    if max_d < a_list[i + 1] - a_list[i]:
        max_d = a_list[i + 1] - a_list[i]
print(sum(d_list) - max_d)

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

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


if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):
import sys
import math
import bisect

def solve(m, A):
    
    n = len(A)
    A.sort()
    min_val = 10 ** 18
    for i in range(n):
        j = (i + 1) % n
        d = abs(A[i] - A[j])
        val = m - min(d, m - d)
        
        min_val = min(min_val, val)
    return min_val

def main():
    m, n = map(int, input().split())
    A = list(map(int, input().split()))
    print(solve(m, A))

if __name__ == "__main__":
    main()

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

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

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

Solution (Python):

"""
Created on Sat Mar 28 20:51:34 2020

@author: naoki
"""

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

diff = [0]*N

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

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

print(K - max(diff))

Solution (Python):

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

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

Solution (Python):

def main():
    k,n = map(int,input().split())
    A = sorted(list(map(int,input().split())))
    mx = 0

    for i,a in enumerate(A):
        if i == 0:
            continue
        if abs(A[i] - A[i-1]) > mx:
            mx = abs(A[i] - A[i-1])

    delta = max(A) - min(A)



    print(min(delta,k-mx))




if __name__ == '__main__':
    main()

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

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

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

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

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

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

print(K-max(dis))

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

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

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

    d = (K - A[-1]) + A[0]
    ans += d
    md = max(md, d)

    ans -= md

    print(ans)



if __name__ == "__main__":
    main()

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

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

print(K-mx)

Solution (Python):

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

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

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

print(K - max_length)

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

    max_diff = 0
    for i in range(N - 1):
        max_diff = max(max_diff, AN[i + 1] - AN[i])
    max_diff = max(max_diff, AN[0] + K - AN[-1])

    print(K - max_diff)


main()

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

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

print(k-max(kyori))

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

def combinations_count(n, r):
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))











K,N = map(int, input().split())
A = [int(i) for i in input().split()]
D = [0]*N
ans = 0

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

ans = K - max(D)

print(ans)

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

for i,l in enumerate(ll):
    if i == 0:
        continue
    tmp.append(ll[i]-ll[i-1])

m = max(tmp)
print(k-m)

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

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

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

print(k-ans)

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    D = []
    for i in range(N-1) :
        D.append(A[i+1] - A[i])
    D.append(A[0] + (K - A[N-1]))

    max_D = max(D)

    print(sum(D) - max(D))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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


now = 0
now_dis = K - A[-1] + A[0] 
for i in A:
  dis = i - now
  if dis > now_dis:
    now_dis = dis
  now = i
print(K-now_dis)

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

l = l + [i + K for i in l]

ans = 10**6
for i in range(N-1,2*N):
    ans = min(ans,l[i]-l[i-N+1])

print(ans)

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

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

sa = max(sa, a[0]+k-a[-1])

print(k-sa)

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

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

Solution (Python):
K, N = map(int, input().split())
As = list(map(int, input().split()))
Ds = []
past = 0
for i, a in enumerate(As):
    if not i==0:
        Ds.append(a-past)
    past = a
Ds.append(As[0]+K-As[-1])
print(K-max(Ds))

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

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

print(k - max(ls))

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

Solution (Python):





def solv(K, N, A):
    max_d = 0
    for i in range(N - 1):
        d = A[i + 1] - A[i]
        if d > max_d:
            max_d = d
    d = K + A[0] - A[-1]
    if d > max_d:
        max_d = d
    return K - max_d


if __name__ == "__main__":

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

    print(ans)

Solution (Python):
K,N = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
A.append(K+A[0])

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

ans = K - max_distance

print(ans)

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

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

sa = 0
for i in range(N-1):
  sa = max(sa,l[i+1]-l[i])
sa = max(sa,(K-l[N-1])+l[0]) 

print(K-sa)

Solution (Python):

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

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

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

ans.sort()

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

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

result_list = []
for i in range(len(As)-1):
    result_list.append(As[i+1]-As[i])
result_list.append(K-As[-1]+As[0])

result = sum(result_list) - max(result_list)

print(result)

Solution (Python):
import sys
input = sys.stdin.readline


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

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

Solution (Python):
def resolve():
    k, n = 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(k-(a[n-1]-a[0]))
    total = sum(dist)
    print(total-max(dist))
resolve()

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

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

print(k - max_dist)

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

A_diff = list([0] for _ in range(N))

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

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

ans = K - max(A_diff)

print(ans)

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

A = [A[i+1] - A[i] for i in range(N-1)]
A.append(K-e+s)
print(sum(A)-max(A))

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

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

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

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

ans = k

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

print(ans)

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

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

mi=max(li)
print(K-mi)

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    m = 0
    for i in range(N-1):
        m = max(A[i+1] - A[i], m)
    m = max(K-A[N-1] + A[0], m)
    print(K-m)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

Solution (Python):
K, N = 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(K-A[-1]+A[0])
print(sum(dist) - max(dist))

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

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

def solve(K, N, A):
    A.append(A[0] + K)
    B = [A[(i+1)] - A[i] for i in range(N)]
    B = sorted(B)
    return sum(B[:-1])


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Solution (Python):


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

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

print(k - max(s))

Solution (Python):
import sys
import math
import collections
from collections import deque

k, n = map(int, sys.stdin.readline().split())

a = list(map(int, sys.stdin.readline().split()))

d = []

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

d.append(k-a[n-1]+a[0])
e = sum(d)
f = max(d)
print(e-f)

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

Solution (Python):
def main():

    K, N = map(int, input().split())
    A = list(map(int, input().split()))
    A = sorted(A)
    max_dist = A[0] + (K - A[-1])
    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)


if __name__ == '__main__':
    main()

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

Solution (Python):
from sys import stdin
k,n = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [0] * n
for i in range(n-1):
    b[i] = a[i+1] - a[i]
b[n-1] = k - a[n-1] + a[0]
print(sum(b) - max(b))

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

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

Solution (Python):

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


A.sort()

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

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


print(K-maxa)

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

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

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

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

def find_max(a,n,k):
    m = a[0] + (k-a[n-1])
    for i in range(1,n):
        if a[i]-a[i-1] > m:
            m = a[i]-a[i-1]
    return m

print(k-find_max(A,n,k))

Solution (Python):
K,N,*A = map(int,open(0).read().split())
l = []
for i in range(N-1):
    l.append(A[i+1]-A[i])
l.append(K-A[-1] + A[0])   
s=sorted(l)
print(sum(l)-s[-1])

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_new = A[i+1] - A[i]
  D = max(D, D_new)
print(K-D)

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

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

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

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

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

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

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

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

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

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

max_diff=-1
diff=-1
for i in range(len(arr)):
    if i==0:
        diff= k-arr[-1]+arr[0]
    else:
        diff= arr[i]-arr[i-1]
    if diff>max_diff:
        max_diff=diff
print(k-max_diff)

Solution (Python):


from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key



__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x



if __name__ == '__main__':
    K, N = mulinputs()
    datas = list(mulinputs())

    dist = [0] * N
    dist[0] = K - datas[-1] + datas[0]
    i = 1
    while i < N:
        dist[i] = datas[i] - datas[i-1]
        i += 1

    
    max_idou = max(dist)
    print(K - max_idou)

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

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

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):
    max_len = 0
    for i in range(len(A)):
        if i == 0:
            max_len = A[0] + (K - A[N-1])
        else:
            if A[i] - A[i-1] > max_len:
                max_len = A[i] - A[i-1]
    print(K - max_len)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(K, N, A)

if __name__ == '__main__':
    main()

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

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

print(k - max_value)

Solution (Python):
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''

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

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

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

print(K - max(diffA))

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,-1,-1):
    temp=a[i-1]+(k-a[i])
    
    if temp>0 and temp<ans:ans=temp
print(ans)

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

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

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

B.append(A[N-1]-K-A[0])

print(K + min(B))

Solution (Python):

import sys


def solve(K: int, N: int, A: "List[int]"):    
    return K - max(a2 - a1 for a1, a2 in zip(A, A[1:]+[K+A[0]]))



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    K = int(next(tokens))  
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    print(solve(K, N, A))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    K,N = map(int, readline().split())
    A = [int(i) for i in readline().split()]

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

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

    print(K - d)


if __name__ == "__main__":
    main()

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

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

print(k-long)

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

Solution (Python):
k,n=map(int,input().split())
poses=list(map(int,input().split()))
print(k-max(k-poses[-1]+poses[0],max(pos1-pos0 for pos1,pos0 in zip(poses[1:],poses))))

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

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

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

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

print(K-dist_max)

Solution (Python):



def main():
    k, n  = map(int, input().split(" "))
    a = [int(s) for s in input().split(" ")]
    max_distance = a[0] + k - a[-1]
    p = 0
    for t in a:
        if t - p > max_distance:
            max_distance = t - p
        p = t
    print(k - max_distance)


if __name__ == '__main__':
    main()

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

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

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


print(K - max(B))

Solution (Python):




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

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

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

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

print(K - minn)

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

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

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

print(k - mx)

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

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


for i in range(n-1):
    d = a_list[i+1] - a_list[i]
    if i == 0:
        maxd = d
    else:
        maxd = max(d, maxd)
    


maxd = max(k-a_list[n-1]+a_list[0], maxd)
out = k-maxd
print(out)

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

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

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

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

print(K-max(B))

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

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

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


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

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

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

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

print(K-diff)