import java.util.Scanner;

// 12
class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] a = new int[N + 1];
        for(int i = 0; i < N; ++i)
            a[i] = sc.nextInt();
        a[N] = a[0] + K;

        int t = 0;
        for(int i = 0; i < N; i++)
            t = Math.max(t, a[i + 1] - a[i]);

        System.out.println(K - t);
    }

} import java.util.Scanner; Scanner java.util class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] a = new int[N + 1];
        for(int i = 0; i < N; ++i)
            a[i] = sc.nextInt();
        a[N] = a[0] + K;

        int t = 0;
        for(int i = 0; i < N; i++)
            t = Math.max(t, a[i + 1] - a[i]);

        System.out.println(K - t);
    }

} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] a = new int[N + 1];
        for(int i = 0; i < N; ++i)
            a[i] = sc.nextInt();
        a[N] = a[0] + K;

        int t = 0;
        for(int i = 0; i < N; i++)
            t = Math.max(t, a[i + 1] - a[i]);

        System.out.println(K - t);
    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] a = new int[N + 1];
        for(int i = 0; i < N; ++i)
            a[i] = sc.nextInt();
        a[N] = a[0] + K;

        int t = 0;
        for(int i = 0; i < N; i++)
            t = Math.max(t, a[i + 1] - a[i]);

        System.out.println(K - t);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] a = new int[N + 1]; a new int[N + 1] N + 1 N 1 for(int i = 0; i < N; ++i)
            a[i] = sc.nextInt(); int i = 0; int i = 0; i 0 i < N i N ++i ++i i a[i] = sc.nextInt(); a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc a[N] = a[0] + K a[N] a N a[0] + K a[0] a 0 K int t = 0; t 0 for(int i = 0; i < N; i++)
            t = Math.max(t, a[i + 1] - a[i]); int i = 0; int i = 0; i 0 i < N i N i++ i++ i t = Math.max(t, a[i + 1] - a[i]); t = Math.max(t, a[i + 1] - a[i]) t Math.max(t, a[i + 1] - a[i]) Math.max Math t a[i + 1] - a[i] a[i + 1] a i + 1 i 1 a[i] a i System.out.println(K - t) System.out.println System.out System System.out K - t K t String[] args args
import java.util.Scanner;

// 12
class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] a = new int[N + 1];
        for(int i = 0; i < N; ++i)
            a[i] = sc.nextInt();
        a[N] = a[0] + K;

        int t = 0;
        for(int i = 0; i < N; i++)
            t = Math.max(t, a[i + 1] - a[i]);

        System.out.println(K - t);
    }

}
