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)

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

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

-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) +print((n - max(distance)))