import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int N=sc.nextInt();
        int[] arr=new int[N];
        int max=0;
        for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        }
        max =  Math.max(k - arr[(N - 1)] + arr[0], max);
        System.out.println(k-max);
    }

} import java.util.Arrays; Arrays java.util import java.util.Collections; Collections java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int N=sc.nextInt();
        int[] arr=new int[N];
        int max=0;
        for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        }
        max =  Math.max(k - arr[(N - 1)] + arr[0], max);
        System.out.println(k-max);
    }

} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int N=sc.nextInt();
        int[] arr=new int[N];
        int max=0;
        for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        }
        max =  Math.max(k - arr[(N - 1)] + arr[0], max);
        System.out.println(k-max);
    } main {

        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int N=sc.nextInt();
        int[] arr=new int[N];
        int max=0;
        for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        }
        max =  Math.max(k - arr[(N - 1)] + arr[0], max);
        System.out.println(k-max);
    } 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[] arr=new int[N]; arr new int[N] N int max=0; max 0 for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        } {
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        } arr[i]=sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            } i>1 i 1 {
                max = Math.max(arr[i] - arr[i - 1], max);
            } max = Math.max(arr[i] - arr[i - 1], max) max Math.max(arr[i] - arr[i - 1], max) Math.max Math arr[i] - arr[i - 1] arr[i] arr i arr[i - 1] arr i - 1 i 1 max max =  Math.max(k - arr[(N - 1)] + arr[0], max) max Math.max(k - arr[(N - 1)] + arr[0], max) Math.max Math k - arr[(N - 1)] + arr[0] k - arr[(N - 1)] k arr[(N - 1)] arr (N - 1) N 1 arr[0] arr 0 max System.out.println(k-max) System.out.println System.out System System.out k-max k max 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[] arr=new int[N];
        int max=0;
        for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        }
        max =  Math.max(k - arr[(N - 1)] + arr[0], max);
        System.out.println(k-max);
    }

} public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int N=sc.nextInt();
        int[] arr=new int[N];
        int max=0;
        for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        }
        max =  Math.max(k - arr[(N - 1)] + arr[0], max);
        System.out.println(k-max);
    }

} Main
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int N=sc.nextInt();
        int[] arr=new int[N];
        int max=0;
        for (int i=0;i<N;i++){
            arr[i]=sc.nextInt();
            if (i>1){
                max = Math.max(arr[i] - arr[i - 1], max);
            }
        }
        max =  Math.max(k - arr[(N - 1)] + arr[0], max);
        System.out.println(k-max);
    }

}
