import sys

import numpy as np



sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1

printV = lambda x: print(*x, sep="\n")

printH = lambda x: print(" ".join(map(str,x)))

def IS(): return sys.stdin.readline()[:-1]

def II(): return int(sys.stdin.readline())

def MI(): return map(int, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def LI1(): return list(map(int1, sys.stdin.readline().split()))

def LII(rows_number): return [II() for _ in range(rows_number)]

def LLI(rows_number): return [LI() for _ in range(rows_number)]

def LLI1(rows_number): return [LI1() for _ in range(rows_number)]



def main():

	K,N = MI()

	A = np.array(LI())

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

	B = A[1:]-A[:N-1]

	B = np.sort(B)

	print(K-max(B[N-2],A_LAST))



if __name__ == '__main__':

	main()

import sys



sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1

printV = lambda x: print(*x, sep="\n")

printH = lambda x: print(" ".join(map(str,x)))

def IS(): return sys.stdin.readline()[:-1]

def II(): return int(sys.stdin.readline())

def MI(): return map(int, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def LI1(): return list(map(int1, sys.stdin.readline().split()))

def LII(rows_number): return [II() for _ in range(rows_number)]

def LLI(rows_number): return [LI() for _ in range(rows_number)]

def LLI1(rows_number): return [LI1() for _ in range(rows_number)]



def main():

	k,n = MI()

	a = LI()

	b = [0]*n

	for i in range(n):

		if i == n-1:

			b[i] = a[0]+k-a[i]

		else:

			b[i] = a[i+1]-a[i]

	b.sort()

	print(k-b[-1])





if __name__ == '__main__':

	main()

import sys
import numpy as np

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str, x)))


def IS():
    return sys.stdin.readline()[:-1]


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def LI1():
    return list(map(int1, sys.stdin.readline().split()))


def LII(rows_number):
    return [II() for _ in range(rows_number)]


def LLI(rows_number):
    return [LI() for _ in range(rows_number)]


def LLI1(rows_number):
    return [LI1() for _ in range(rows_number)]


def main():
    K, N = MI()
    A = np.array(LI())
    A_LAST = K + A[0] - A[N - 1]
    B = A[1:] - A[: N - 1]
    B = np.sort(B)
    print(K - max(B[N - 2], A_LAST))


if __name__ == "__main__":
    main()

import sys

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str, x)))


def IS():
    return sys.stdin.readline()[:-1]


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def LI1():
    return list(map(int1, sys.stdin.readline().split()))


def LII(rows_number):
    return [II() for _ in range(rows_number)]


def LLI(rows_number):
    return [LI() for _ in range(rows_number)]


def LLI1(rows_number):
    return [LI1() for _ in range(rows_number)]


def main():
    k, n = MI()
    a = LI()
    b = [0] * n
    for i in range(n):
        if i == n - 1:
            b[i] = a[0] + k - a[i]
        else:
            b[i] = a[i + 1] - a[i]
    b.sort()
    print(k - b[-1])


if __name__ == "__main__":
    main()

-import numpy as np -    K, N = MI() -    A = np.array(LI()) -    A_LAST = K + A[0] - A[N - 1] -    B = A[1:] - A[: N - 1] -    B = np.sort(B) -    print(K - max(B[N - 2], A_LAST)) +    k, n = MI() +    a = LI() +    b = [0] * n +    for i in range(n): +        if i == n - 1: +            b[i] = a[0] + k - a[i] +        else: +            b[i] = a[i + 1] - a[i] +    b.sort() +    print(k - b[-1])