import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int n = s.nextInt();

        if(n == 1) {
            System.out.println("0");
        }
        int arr[] = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        int res = K;
        for(int i = 0; i < n; i++) {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        }

        System.out.println(res);
    }
} import java.util.Scanner; Scanner java.util class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int n = s.nextInt();

        if(n == 1) {
            System.out.println("0");
        }
        int arr[] = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        int res = K;
        for(int i = 0; i < n; i++) {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        }

        System.out.println(res);
    }
} Main public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int n = s.nextInt();

        if(n == 1) {
            System.out.println("0");
        }
        int arr[] = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        int res = K;
        for(int i = 0; i < n; i++) {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        }

        System.out.println(res);
    } main {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int n = s.nextInt();

        if(n == 1) {
            System.out.println("0");
        }
        int arr[] = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        int res = K;
        for(int i = 0; i < n; i++) {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        }

        System.out.println(res);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int K = s.nextInt(); K s.nextInt() s.nextInt s int n = s.nextInt(); n s.nextInt() s.nextInt s if(n == 1) {
            System.out.println("0");
        } n == 1 n 1 {
            System.out.println("0");
        } System.out.println("0") System.out.println System.out System System.out "0" int arr[] = new int[n]; arr new int[n] n for(int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = s.nextInt();
        } {
            arr[i] = s.nextInt();
        } arr[i] = s.nextInt() arr[i] arr i s.nextInt() s.nextInt s int res = K; res K for(int i = 0; i < n; i++) {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        } {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        } if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i]))); i == 0 i 0 res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0]))) res Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0]))) Math.min Math res Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])) Math.min Math arr[n-1]-arr[0] arr[n-1] arr n-1 n 1 arr[0] arr 0 K-(arr[i+1] - arr[0]) K (arr[i+1] - arr[0]) arr[i+1] arr i+1 i 1 arr[0] arr 0 if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i]))); i == n-1 i n-1 n 1 res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2]))) res Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2]))) Math.min Math res Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])) Math.min Math Math.abs(arr[n-1]-arr[0]) Math.abs Math arr[n-1]-arr[0] arr[n-1] arr n-1 n 1 arr[0] arr 0 K - (arr[n-1] - arr[n-2]) K (arr[n-1] - arr[n-2]) arr[n-1] arr n-1 n 1 arr[n-2] arr n-2 n 2 res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i]))) res Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i]))) Math.min Math res Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])) Math.min Math K - (arr[i] - arr[i-1]) K (arr[i] - arr[i-1]) arr[i] arr i arr[i-1] arr i-1 i 1 K - (arr[i+1] - arr[i]) K (arr[i+1] - arr[i]) arr[i+1] arr i+1 i 1 arr[i] arr i System.out.println(res) System.out.println System.out System System.out res String[] args args
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int K = s.nextInt();
        int n = s.nextInt();

        if(n == 1) {
            System.out.println("0");
        }
        int arr[] = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        int res = K;
        for(int i = 0; i < n; i++) {
            if(i == 0)
                res = Math.min(res, Math.min(arr[n-1]-arr[0], K-(arr[i+1] - arr[0])));
            else if(i == n-1)
                res = Math.min(res, Math.min(Math.abs(arr[n-1]-arr[0]), K - (arr[n-1] - arr[n-2])));
            else
                res = Math.min(res, Math.min(K - (arr[i] - arr[i-1]), K - (arr[i+1] - arr[i])));
        }

        System.out.println(res);
    }
}
