import sys

input = sys.stdin.readline



def main():

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

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

    longest = a[0] + k - a[-1]

    for i in range(n - 1):

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



    print((k - longest))





if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read

def main():

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

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

    dis_max = (k - a[-1]) + a[0]

    for i1 in range(1, n):

        dis = a[i1] - a[i1-1]

        dis_max = max(dis_max, dis)

    print((k - dis_max))

if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline


def main():
    k, n = list(map(int, input().split()))
    a = list(map(int, input().split()))
    longest = a[0] + k - a[-1]
    for i in range(n - 1):
        longest = max(longest, a[i + 1] - a[i])
    print((k - longest))


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read


def main():
    k, n = list(map(int, input().split()))
    a = tuple(map(int, input().split()))
    dis_max = (k - a[-1]) + a[0]
    for i1 in range(1, n):
        dis = a[i1] - a[i1 - 1]
        dis_max = max(dis_max, dis)
    print((k - dis_max))


if __name__ == "__main__":
    main()

-input = sys.stdin.readline +read = sys.stdin.read -    a = list(map(int, input().split())) -    longest = a[0] + k - a[-1] -    for i in range(n - 1): -        longest = max(longest, a[i + 1] - a[i]) -    print((k - longest)) +    a = tuple(map(int, input().split())) +    dis_max = (k - a[-1]) + a[0] +    for i1 in range(1, n): +        dis = a[i1] - a[i1 - 1] +        dis_max = max(dis_max, dis) +    print((k - dis_max))