import sys



# import bisect

# from collections import Counter, deque, defaultdict

# import copy

# from heapq import heappush, heappop, heapify

# from fractions import gcd

# import itertools

# from operator import attrgetter, itemgetter

import math



# import numpy as np



readline = sys.stdin.readline

MOD = 10 ** 9 + 7

INF = float('INF')

sys.setrecursionlimit(10 ** 5)





def main():

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

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



    ans = INF



    temp1 = a[-1] - a[0]

    temp2 = k - (a[1] - a[0])

    ans = min(ans, temp1, temp2)



    for i in range(1, n - 1):

        temp1 = k - (a[i + 1] - a[i])

        temp2 = k - (a[i] - a[i - 1])

        ans = min(ans, temp1, temp2)



    temp1 = k - (a[-1] - a[-2])

    temp2 = a[-1] - a[0]

    ans = min(ans, temp1, temp2)



    print(ans)





if __name__ == '__main__':

    main()

import sys



# import bisect

# from collections import Counter, deque, defaultdict



# import copy

# from heapq import heappush, heappop, heapify

# from fractions import gcd

# import itertools

# from operator import attrgetter, itemgetter

# import math



# import numpy as np



readline = sys.stdin.readline

MOD = 10 ** 9 + 7

INF = float('INF')

sys.setrecursionlimit(10 ** 5)





def main():

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

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



    d = [0] * (n - 1)

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



    for i in range(1, n - 1):

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



    print((k - max(d)))





if __name__ == '__main__':

    main()

import sys

# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
import math

# import numpy as np
readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


def main():
    k, n = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    ans = INF
    temp1 = a[-1] - a[0]
    temp2 = k - (a[1] - a[0])
    ans = min(ans, temp1, temp2)
    for i in range(1, n - 1):
        temp1 = k - (a[i + 1] - a[i])
        temp2 = k - (a[i] - a[i - 1])
        ans = min(ans, temp1, temp2)
    temp1 = k - (a[-1] - a[-2])
    temp2 = a[-1] - a[0]
    ans = min(ans, temp1, temp2)
    print(ans)


if __name__ == "__main__":
    main()

import sys

# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math
# import numpy as np
readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


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


if __name__ == "__main__":
    main()

-import math - +# import math -    ans = INF -    temp1 = a[-1] - a[0] -    temp2 = k - (a[1] - a[0]) -    ans = min(ans, temp1, temp2) +    d = [0] * (n - 1) +    d[0] = (k - a[-1]) + a[0] -        temp1 = k - (a[i + 1] - a[i]) -        temp2 = k - (a[i] - a[i - 1]) -        ans = min(ans, temp1, temp2) -    temp1 = k - (a[-1] - a[-2]) -    temp2 = a[-1] - a[0] -    ans = min(ans, temp1, temp2) -    print(ans) +        d[i] = a[i + 1] - a[i] +    print((k - max(d)))