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

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

array_distance = []



for i in range(N):

    min = 0

    if i == N-1:

        min = abs(array[0] - array[N-1])

        if min>=K/2:

            min = abs(K-min)

        array_distance.append(min)

    else:

        min = abs(array[i] - array[i+1])

        if min>=K/2:

            min = abs(K-min)

        array_distance.append(min)



array_distance= sorted(array_distance, reverse = True)

# print(array_distance)

# print(array_distance[0])

# print(sum(array_distance))

print((sum(array_distance) - array_distance[0]))



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

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

array_distance = []



for i in range(N):

    min = 0

    if i == N-1:

        min = abs(array[0] - array[N-1])

        if min>=K/2:

            min = abs(K-min)

        array_distance.append(min)

    else:

        min = abs(array[i] - array[i+1])



        array_distance.append(min)



array_distance= sorted(array_distance, reverse = True)

# print(array_distance)

# print(array_distance[0])

# print(sum(array_distance))

print((sum(array_distance) - array_distance[0]))



K, N = list(map(int, input().split()))
array = list(map(int, input().split()))
array_distance = []
for i in range(N):
    min = 0
    if i == N - 1:
        min = abs(array[0] - array[N - 1])
        if min >= K / 2:
            min = abs(K - min)
        array_distance.append(min)
    else:
        min = abs(array[i] - array[i + 1])
        if min >= K / 2:
            min = abs(K - min)
        array_distance.append(min)
array_distance = sorted(array_distance, reverse=True)
# print(array_distance)
# print(array_distance[0])
# print(sum(array_distance))
print((sum(array_distance) - array_distance[0]))

K, N = list(map(int, input().split()))
array = list(map(int, input().split()))
array_distance = []
for i in range(N):
    min = 0
    if i == N - 1:
        min = abs(array[0] - array[N - 1])
        if min >= K / 2:
            min = abs(K - min)
        array_distance.append(min)
    else:
        min = abs(array[i] - array[i + 1])
        array_distance.append(min)
array_distance = sorted(array_distance, reverse=True)
# print(array_distance)
# print(array_distance[0])
# print(sum(array_distance))
print((sum(array_distance) - array_distance[0]))

-        if min >= K / 2: -            min = abs(K - min)