import sys



n, k, *a = list(map(int, sys.stdin.read().split()))

a.append(n + a[0])



def main():

    d = 0

    for i in range(k):

        d = max(d, a[i+1] - a[i])

    res = n - d

    print(res)



if __name__ == "__main__":

    main()
import sys



k, n, *a = list(map(int, sys.stdin.read().split()))

a += [a[0] + k]



def main():

    d = [a[i+1] - a[i] for i in range(n)]

    print((k - max(d)))



if __name__ ==  '__main__':

    main()
import sys

n, k, *a = list(map(int, sys.stdin.read().split()))
a.append(n + a[0])


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


if __name__ == "__main__":
    main()

import sys

k, n, *a = list(map(int, sys.stdin.read().split()))
a += [a[0] + k]


def main():
    d = [a[i + 1] - a[i] for i in range(n)]
    print((k - max(d)))


if __name__ == "__main__":
    main()

-n, k, *a = list(map(int, sys.stdin.read().split())) -a.append(n + a[0]) +k, n, *a = list(map(int, sys.stdin.read().split())) +a += [a[0] + k] -    d = 0 -    for i in range(k): -        d = max(d, a[i + 1] - a[i]) -    res = n - d -    print(res) +    d = [a[i + 1] - a[i] for i in range(n)] +    print((k - max(d)))