import java.util.Scanner;
import java.lang.Math;

public 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];

        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int maxGap = 0;
        for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        }

        System.out.println(K-maxGap);
    }
} import java.util.Scanner; Scanner java.util import java.lang.Math; Math java.lang public 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];

        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int maxGap = 0;
        for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        }

        System.out.println(K-maxGap);
    }
} 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];

        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int maxGap = 0;
        for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        }

        System.out.println(K-maxGap);
    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] a = new int[N];

        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int maxGap = 0;
        for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        }

        System.out.println(K-maxGap);
    } 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]; a new int[N] N 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] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int maxGap = 0; maxGap 0 for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        } int i = N; int i = N; i N i > 0 i 0 i-- i-- i {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        } {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        } if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            } i > 1 i 1 {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            } maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) maxGap Math.max(maxGap ,(a[i-1] - a[i-2])) Math.max Math maxGap (a[i-1] - a[i-2]) a[i-1] a i-1 i 1 a[i-2] a i-2 i 2 {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            } maxGap = Math.max(maxGap, (K - a[N-1] + a[0])) maxGap Math.max(maxGap, (K - a[N-1] + a[0])) Math.max Math maxGap (K - a[N-1] + a[0]) K - a[N-1] K a[N-1] a N-1 N 1 a[0] a 0 System.out.println(K-maxGap) System.out.println System.out System System.out K-maxGap K maxGap String[] args args public 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];

        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int maxGap = 0;
        for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        }

        System.out.println(K-maxGap);
    }
} public 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];

        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int maxGap = 0;
        for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        }

        System.out.println(K-maxGap);
    }
} Main
import java.util.Scanner;
import java.lang.Math;

public 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];

        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int maxGap = 0;
        for (int i = N; i > 0;i--) {
            if (i > 1) {
                maxGap = Math.max(maxGap ,(a[i-1] - a[i-2])) ;
            }

            else {
                maxGap = Math.max(maxGap, (K - a[N-1] + a[0]));
            }
        }

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