#どっちで出すか注意, rstrip注意

#提出前に見返すこと！

def main():

    import sys

    input = sys.stdin.readline

    sys.setrecursionlimit(10**7)

    from collections import Counter, deque

    #from collections import defaultdict

    from itertools import combinations, permutations, accumulate

    #from itertools import product

    from bisect import bisect_left,bisect_right

    import heapq

    from math import floor, ceil

    #from operator import itemgetter



    #inf = 10**17

    #mod = 10**9 + 7



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

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



    res = 10**20

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

        res = min(K-(a[i+1]-a[i]), K-(a[i]-a[i-1]), res)

    res = min(K-(a[1]-a[0]), res, a[N-1]-a[0])

    res = min(res, K-(a[N-1]-a[N-2]))

    print(res)





if __name__ == '__main__':

    main()
#どっちで出すか注意, rstrip注意

#提出前に見返すこと！

def main():

    import sys

    input = sys.stdin.readline

    sys.setrecursionlimit(10**7)

    from collections import Counter, deque

    #from collections import defaultdict

    from itertools import combinations, permutations, accumulate

    #from itertools import product

    from bisect import bisect_left,bisect_right

    import heapq

    from math import floor, ceil

    #from operator import itemgetter



    #inf = 10**17

    #mod = 10**9 + 7



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

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

    res = 0

    for i in range(n-1):

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

    res = max(res, a[0]+k-a[n-1])

    print((k-res))



if __name__ == '__main__':

    main()
# どっちで出すか注意, rstrip注意
# 提出前に見返すこと！
def main():
    import sys

    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque

    # from collections import defaultdict
    from itertools import combinations, permutations, accumulate

    # from itertools import product
    from bisect import bisect_left, bisect_right
    import heapq
    from math import floor, ceil

    # from operator import itemgetter
    # inf = 10**17
    # mod = 10**9 + 7
    K, N = list(map(int, input().split()))
    a = list(map(int, input().split()))
    res = 10**20
    for i in range(1, N - 1):
        res = min(K - (a[i + 1] - a[i]), K - (a[i] - a[i - 1]), res)
    res = min(K - (a[1] - a[0]), res, a[N - 1] - a[0])
    res = min(res, K - (a[N - 1] - a[N - 2]))
    print(res)


if __name__ == "__main__":
    main()

# どっちで出すか注意, rstrip注意
# 提出前に見返すこと！
def main():
    import sys

    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque

    # from collections import defaultdict
    from itertools import combinations, permutations, accumulate

    # from itertools import product
    from bisect import bisect_left, bisect_right
    import heapq
    from math import floor, ceil

    # from operator import itemgetter
    # inf = 10**17
    # mod = 10**9 + 7
    k, n = list(map(int, input().split()))
    a = list(map(int, input().split()))
    res = 0
    for i in range(n - 1):
        res = max(res, a[i + 1] - a[i])
    res = max(res, a[0] + k - a[n - 1])
    print((k - res))


if __name__ == "__main__":
    main()

-    K, N = list(map(int, input().split())) +    k, n = list(map(int, input().split())) -    res = 10**20 -    for i in range(1, N - 1): -        res = min(K - (a[i + 1] - a[i]), K - (a[i] - a[i - 1]), res) -    res = min(K - (a[1] - a[0]), res, a[N - 1] - a[0]) -    res = min(res, K - (a[N - 1] - a[N - 2])) -    print(res) +    res = 0 +    for i in range(n - 1): +        res = max(res, a[i + 1] - a[i]) +    res = max(res, a[0] + k - a[n - 1]) +    print((k - res))