import java.util.ArrayList;
import java.util.List;
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();
        List<Integer> list = new ArrayList<>();
        int maxDeffrence = 0;
        int[] reverseIndex = new int[2];

        for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        }
        
        if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        }
    }
} import java.util.ArrayList; ArrayList java.util import java.util.List; List 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();
        List<Integer> list = new ArrayList<>();
        int maxDeffrence = 0;
        int[] reverseIndex = new int[2];

        for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        }
        
        if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        }
    }
} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        int maxDeffrence = 0;
        int[] reverseIndex = new int[2];

        for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        }
        
        if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        }
    } main {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        int maxDeffrence = 0;
        int[] reverseIndex = new int[2];

        for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        }
        
        if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        }
    } 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 List<Integer> list = new ArrayList<>(); list new ArrayList<>() int maxDeffrence = 0; maxDeffrence 0 int[] reverseIndex = new int[2]; reverseIndex new int[2] 2 for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        } {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        } list.add(sc.nextInt()) list.add list sc.nextInt() sc.nextInt sc if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            } i > 0 i 0 {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            } int oldMaxDeffrence = maxDeffrence; oldMaxDeffrence maxDeffrence maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1)) maxDeffrence Math.max(maxDeffrence, list.get(i) - list.get(i - 1)) Math.max Math maxDeffrence 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 if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                } maxDeffrence > oldMaxDeffrence maxDeffrence oldMaxDeffrence {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                } reverseIndex[0] = i - 1 reverseIndex[0] reverseIndex 0 i - 1 i 1 reverseIndex[1] = i reverseIndex[1] reverseIndex 1 i if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        } maxDeffrence <= list.get(0) + K - list.get(list.size()-1) maxDeffrence list.get(0) + K - list.get(list.size()-1) list.get(0) + K list.get(0) list.get list 0 K list.get(list.size()-1) list.get list list.size()-1 list.size() list.size list 1 {
            System.out.println(list.get(list.size()-1) - list.get(0));
        } System.out.println(list.get(list.size()-1) - list.get(0)) System.out.println System.out System System.out list.get(list.size()-1) - list.get(0) list.get(list.size()-1) list.get list list.size()-1 list.size() list.size list 1 list.get(0) list.get list 0 {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        } int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]); lap list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]) list.get(reverseIndex[0]) + K list.get(reverseIndex[0]) list.get list reverseIndex[0] reverseIndex 0 K list.get(reverseIndex[1]) list.get list reverseIndex[1] reverseIndex 1 System.out.println(lap) System.out.println System.out System System.out lap 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();
        List<Integer> list = new ArrayList<>();
        int maxDeffrence = 0;
        int[] reverseIndex = new int[2];

        for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        }
        
        if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        }
    }
} public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        int maxDeffrence = 0;
        int[] reverseIndex = new int[2];

        for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        }
        
        if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        }
    }
} Main
import java.util.ArrayList;
import java.util.List;
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();
        List<Integer> list = new ArrayList<>();
        int maxDeffrence = 0;
        int[] reverseIndex = new int[2];

        for (int i = 0; i < N; i++) {
            list.add(sc.nextInt());
            if (i > 0) {
                int oldMaxDeffrence = maxDeffrence;
                maxDeffrence = Math.max(maxDeffrence, list.get(i) - list.get(i - 1));
                if (maxDeffrence > oldMaxDeffrence) {
                    reverseIndex[0] = i - 1;
                    reverseIndex[1] = i;
                }
            }
        }
        
        if(maxDeffrence <= list.get(0) + K - list.get(list.size()-1)){
            System.out.println(list.get(list.size()-1) - list.get(0));
        } else {
            int lap = list.get(reverseIndex[0]) + K - list.get(reverseIndex[1]);
            System.out.println(lap);
        }
    }
}