#template

def inputlist(): return [int(j) for j in input().split()]

#template

#issueから始める

K,N = inputlist()

A = inputlist()

lia = [0]*N

for i in range(1,N):

    k = A[i] - A[i-1]

    lia[i] = k

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

lia.sort()

ans = 0

for i in range(N-1):

    ans += lia[i]

print(ans)
#template

def inputlist(): return [int(j) for j in input().split()]

def listinput(): return input().split()

#template

K,N = inputlist()

A = inputlist()

dis = [0]*(N)

for i in range(N):

    if i == N-1:

        dis[i] = K+A[0] - A[i]

        continue

    dis[i] = A[i+1] - A[i]

dis.sort()

print((sum(dis[:-1])))
# template
def inputlist():
    return [int(j) for j in input().split()]


# template
# issueから始める
K, N = inputlist()
A = inputlist()
lia = [0] * N
for i in range(1, N):
    k = A[i] - A[i - 1]
    lia[i] = k
lia[0] = K + A[0] - A[-1]
lia.sort()
ans = 0
for i in range(N - 1):
    ans += lia[i]
print(ans)

# template
def inputlist():
    return [int(j) for j in input().split()]


def listinput():
    return input().split()


# template
K, N = inputlist()
A = inputlist()
dis = [0] * (N)
for i in range(N):
    if i == N - 1:
        dis[i] = K + A[0] - A[i]
        continue
    dis[i] = A[i + 1] - A[i]
dis.sort()
print((sum(dis[:-1])))

+def listinput(): +    return input().split() + + -# issueから始める -lia = [0] * N -for i in range(1, N): -    k = A[i] - A[i - 1] -    lia[i] = k -lia[0] = K + A[0] - A[-1] -lia.sort() -ans = 0 -for i in range(N - 1): -    ans += lia[i] -print(ans) +dis = [0] * (N) +for i in range(N): +    if i == N - 1: +        dis[i] = K + A[0] - A[i] +        continue +    dis[i] = A[i + 1] - A[i] +dis.sort() +print((sum(dis[:-1])))