import java.util.Scanner;

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

        int[] a = new int[k];
        for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        }

        int maxDistance = 0;
        for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        }
        maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]);

        System.out.println(k-maxDistance);


    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();

        int[] a = new int[k];
        for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        }

        int maxDistance = 0;
        for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        }
        maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]);

        System.out.println(k-maxDistance);


    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();

        int[] a = new int[k];
        for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        }

        int maxDistance = 0;
        for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        }
        maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]);

        System.out.println(k-maxDistance);


    } main {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();

        int[] a = new int[k];
        for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        }

        int maxDistance = 0;
        for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        }
        maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]);

        System.out.println(k-maxDistance);


    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] a = new int[k]; a new int[k] k for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] =scanner.nextInt();
        } {
            a[i] =scanner.nextInt();
        } a[i] =scanner.nextInt() a[i] a i scanner.nextInt() scanner.nextInt scanner int maxDistance = 0; maxDistance 0 for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        } {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        } maxDistance = Math.max(maxDistance, a[i]-a[i-1]) maxDistance Math.max(maxDistance, a[i]-a[i-1]) Math.max Math maxDistance a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]) maxDistance Math.max(maxDistance, a[0]+k-a[n-1]) Math.max Math maxDistance a[0]+k-a[n-1] a[0]+k a[0] a 0 k a[n-1] a n-1 n 1 System.out.println(k-maxDistance) System.out.println System.out System System.out k-maxDistance k maxDistance String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();

        int[] a = new int[k];
        for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        }

        int maxDistance = 0;
        for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        }
        maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]);

        System.out.println(k-maxDistance);


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

        int[] a = new int[k];
        for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        }

        int maxDistance = 0;
        for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        }
        maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]);

        System.out.println(k-maxDistance);


    }
} Main
import java.util.Scanner;

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

        int[] a = new int[k];
        for (int i = 0; i < n; i++) {
            a[i] =scanner.nextInt();
        }

        int maxDistance = 0;
        for (int i = 1; i < n; i++) {
            maxDistance = Math.max(maxDistance, a[i]-a[i-1]);
        }
        maxDistance = Math.max(maxDistance, a[0]+k-a[n-1]);

        System.out.println(k-maxDistance);


    }
}
