import 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();
        ArrayList<Integer> A = new ArrayList<>();
        for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        }
        Collections.sort(A);
        ArrayList<Integer> B = new ArrayList<>();
        B.add(k-A.get(n-1)+A.get(0));
        for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        }
        System.out.println(k-Collections.max(B));

    }

} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        ArrayList<Integer> A = new ArrayList<>();
        for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        }
        Collections.sort(A);
        ArrayList<Integer> B = new ArrayList<>();
        B.add(k-A.get(n-1)+A.get(0));
        for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        }
        System.out.println(k-Collections.max(B));

    }

} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        ArrayList<Integer> A = new ArrayList<>();
        for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        }
        Collections.sort(A);
        ArrayList<Integer> B = new ArrayList<>();
        B.add(k-A.get(n-1)+A.get(0));
        for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        }
        System.out.println(k-Collections.max(B));

    } main {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        ArrayList<Integer> A = new ArrayList<>();
        for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        }
        Collections.sort(A);
        ArrayList<Integer> B = new ArrayList<>();
        B.add(k-A.get(n-1)+A.get(0));
        for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        }
        System.out.println(k-Collections.max(B));

    } 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 ArrayList<Integer> A = new ArrayList<>(); A new ArrayList<>() for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            A.add(scanner.nextInt());
        } {
            A.add(scanner.nextInt());
        } A.add(scanner.nextInt()) A.add A scanner.nextInt() scanner.nextInt scanner Collections.sort(A) Collections.sort Collections A ArrayList<Integer> B = new ArrayList<>(); B new ArrayList<>() B.add(k-A.get(n-1)+A.get(0)) B.add B k-A.get(n-1)+A.get(0) k-A.get(n-1) k A.get(n-1) A.get A n-1 n 1 A.get(0) A.get A 0 for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        } int i=1; int i=1; i 1 i<A.size() i A.size() A.size A i++ i++ i {
            B.add(A.get(i)-A.get(i-1));
        } {
            B.add(A.get(i)-A.get(i-1));
        } B.add(A.get(i)-A.get(i-1)) B.add B A.get(i)-A.get(i-1) A.get(i) A.get A i A.get(i-1) A.get A i-1 i 1 System.out.println(k-Collections.max(B)) System.out.println System.out System System.out k-Collections.max(B) k Collections.max(B) Collections.max Collections B 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();
        ArrayList<Integer> A = new ArrayList<>();
        for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        }
        Collections.sort(A);
        ArrayList<Integer> B = new ArrayList<>();
        B.add(k-A.get(n-1)+A.get(0));
        for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        }
        System.out.println(k-Collections.max(B));

    }

} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        ArrayList<Integer> A = new ArrayList<>();
        for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        }
        Collections.sort(A);
        ArrayList<Integer> B = new ArrayList<>();
        B.add(k-A.get(n-1)+A.get(0));
        for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        }
        System.out.println(k-Collections.max(B));

    }

} Main
import 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();
        ArrayList<Integer> A = new ArrayList<>();
        for (int i=0;i<n;i++){
            A.add(scanner.nextInt());
        }
        Collections.sort(A);
        ArrayList<Integer> B = new ArrayList<>();
        B.add(k-A.get(n-1)+A.get(0));
        for (int i=1;i<A.size();i++){
            B.add(A.get(i)-A.get(i-1));
        }
        System.out.println(k-Collections.max(B));

    }

}