import numpy as np



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

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

A.append(K + A[0])



D = np.diff(A)



print((int(K) - np.max(D)))

import numpy as np



import sys



readline = sys.stdin.buffer.readline



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

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

A.append(K + A[0])



D = np.diff(A)



print((int(K) - np.max(D)))



import numpy as np

K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
A.append(K + A[0])
D = np.diff(A)
print((int(K) - np.max(D)))

import numpy as np
import sys

readline = sys.stdin.buffer.readline
K, N = list(map(int, readline().split()))
A = list(map(int, readline().split()))
A.append(K + A[0])
D = np.diff(A)
print((int(K) - np.max(D)))

+import sys -K, N = list(map(int, input().split())) -A = list(map(int, input().split())) +readline = sys.stdin.buffer.readline +K, N = list(map(int, readline().split())) +A = list(map(int, readline().split()))