import java.util.*;
import java.io.*;
import java.math.*;
 
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        int ans = list.get(n-1)-list.get(0);
        for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        }
        System.out.println(ans);
    }

} import java.util.*; util.* java import java.io.*; io.* java import java.math.*; math.* java public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        int ans = list.get(n-1)-list.get(0);
        for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        }
        System.out.println(ans);
    }

} Main public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        int ans = list.get(n-1)-list.get(0);
        for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        }
        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        int ans = list.get(n-1)-list.get(0);
        for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        }
        System.out.println(ans);
    } 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 ArrayList<Integer> list = new ArrayList<>(); list new ArrayList<>() for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            list.add(sc.nextInt());
        } {
            list.add(sc.nextInt());
        } list.add(sc.nextInt()) list.add list sc.nextInt() sc.nextInt sc Collections.sort(list) Collections.sort Collections list int ans = list.get(n-1)-list.get(0); ans list.get(n-1)-list.get(0) list.get(n-1) list.get list n-1 n 1 list.get(0) list.get list 0 for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        } {
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        } ans = Math.min(ans, k-(list.get(i) - list.get(i-1))) ans Math.min(ans, k-(list.get(i) - list.get(i-1))) Math.min Math ans k-(list.get(i) - list.get(i-1)) k (list.get(i) - list.get(i-1)) list.get(i) list.get list i list.get(i-1) list.get list i-1 i 1 System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        int ans = list.get(n-1)-list.get(0);
        for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        }
        System.out.println(ans);
    }

} public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        int ans = list.get(n-1)-list.get(0);
        for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        }
        System.out.println(ans);
    }

} Main
import java.util.*;
import java.io.*;
import java.math.*;
 
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        int ans = list.get(n-1)-list.get(0);
        for(int i = 1; i < n; i++){
            ans = Math.min(ans, k-(list.get(i) - list.get(i-1)));
        }
        System.out.println(ans);
    }

}