import sys

from collections import deque

import numpy as np

import math

sys.setrecursionlimit(10**6)

def S(): return sys.stdin.readline().rstrip()

def SL(): return list(map(str,sys.stdin.readline().rstrip().split()))

def I(): return int(sys.stdin.readline().rstrip())

def IL(): return list(map(int,sys.stdin.readline().rstrip().split()))



def Main():

    k,n = IL()

    a = list(IL())

    a.sort()

    dist = []

    for i in range(n):

        if i<n-1:

            dist.append(a[i+1]-a[i])

        else:

            dist.append(k-a[i]+a[0])

    x = max(dist)

    print((k-x))

    return



if __name__=='__main__':

    Main()
import sys

def IL(): return list(map(int,sys.stdin.readline().rstrip().split()))



def Main():

    k,n = IL()

    a = list(IL())

    a.sort()

    m = 0

    for i in range(n):

        if i<n-1:

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

        else:

            m = max(m,k-a[i]+a[0])

    print((k-m))

    return



if __name__=='__main__':

    Main()
import sys
from collections import deque
import numpy as np
import math

sys.setrecursionlimit(10**6)


def S():
    return sys.stdin.readline().rstrip()


def SL():
    return list(map(str, sys.stdin.readline().rstrip().split()))


def I():
    return int(sys.stdin.readline().rstrip())


def IL():
    return list(map(int, sys.stdin.readline().rstrip().split()))


def Main():
    k, n = IL()
    a = list(IL())
    a.sort()
    dist = []
    for i in range(n):
        if i < n - 1:
            dist.append(a[i + 1] - a[i])
        else:
            dist.append(k - a[i] + a[0])
    x = max(dist)
    print((k - x))
    return


if __name__ == "__main__":
    Main()

import sys


def IL():
    return list(map(int, sys.stdin.readline().rstrip().split()))


def Main():
    k, n = IL()
    a = list(IL())
    a.sort()
    m = 0
    for i in range(n):
        if i < n - 1:
            m = max(m, a[i + 1] - a[i])
        else:
            m = max(m, k - a[i] + a[0])
    print((k - m))
    return


if __name__ == "__main__":
    Main()

-from collections import deque -import numpy as np -import math - -sys.setrecursionlimit(10**6) - - -def S(): -    return sys.stdin.readline().rstrip() - - -def SL(): -    return list(map(str, sys.stdin.readline().rstrip().split())) - - -def I(): -    return int(sys.stdin.readline().rstrip()) -    dist = [] +    m = 0 -            dist.append(a[i + 1] - a[i]) +            m = max(m, a[i + 1] - a[i]) -            dist.append(k - a[i] + a[0]) -    x = max(dist) -    print((k - x)) +            m = max(m, k - a[i] + a[0]) +    print((k - m))