k,n=list(map(int, input().split()))

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

a.append(k+a[0])

import numpy as np

a=np.array(a)

print((k-max(a[1:]-a[:n])))

#!/usr/bin python3

# -*- coding: utf-8 -*-



import sys

input = sys.stdin.readline



def main():

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

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

    A.append(K+A[0])

    print((K - max([A[i]-A[i-1] for i in range(1, N+1)])))



if __name__ == '__main__':

    main()
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(k + a[0])
import numpy as np

a = np.array(a)
print((k - max(a[1:] - a[:n])))

#!/usr/bin python3
# -*- coding: utf-8 -*-
import sys

input = sys.stdin.readline


def main():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.append(K + A[0])
    print((K - max([A[i] - A[i - 1] for i in range(1, N + 1)])))


if __name__ == "__main__":
    main()

-k, n = list(map(int, input().split())) -a = list(map(int, input().split())) -a.append(k + a[0]) -import numpy as np +#!/usr/bin python3 +# -*- coding: utf-8 -*- +import sys -a = np.array(a) -print((k - max(a[1:] - a[:n]))) +input = sys.stdin.readline + + +def main(): +    K, N = list(map(int, input().split())) +    A = list(map(int, input().split())) +    A.append(K + A[0]) +    print((K - max([A[i] - A[i - 1] for i in range(1, N + 1)]))) + + +if __name__ == "__main__": +    main()