import java.util.*;
import java.lang.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer k = Integer.parseInt(scanner.next());
        Integer n = Integer.parseInt(scanner.next());

        Integer[] total = new Integer[n];
        for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        }


        List<Integer> kyori = new ArrayList<Integer>();
        for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        }

        kyori.add(Math.abs((k - total[n-1]) + total[0]));
        Collections.sort(kyori);
        kyori.remove(kyori.size() - 1);


        int sum = 0;
        for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        }

        System.out.println(sum);
    }
} import java.util.*; util.* java import java.lang.*; lang.* java import java.util.stream.*; stream.* java.util public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer k = Integer.parseInt(scanner.next());
        Integer n = Integer.parseInt(scanner.next());

        Integer[] total = new Integer[n];
        for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        }


        List<Integer> kyori = new ArrayList<Integer>();
        for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        }

        kyori.add(Math.abs((k - total[n-1]) + total[0]));
        Collections.sort(kyori);
        kyori.remove(kyori.size() - 1);


        int sum = 0;
        for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        }

        System.out.println(sum);
    }
} Main public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer k = Integer.parseInt(scanner.next());
        Integer n = Integer.parseInt(scanner.next());

        Integer[] total = new Integer[n];
        for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        }


        List<Integer> kyori = new ArrayList<Integer>();
        for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        }

        kyori.add(Math.abs((k - total[n-1]) + total[0]));
        Collections.sort(kyori);
        kyori.remove(kyori.size() - 1);


        int sum = 0;
        for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        }

        System.out.println(sum);
    } main {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer k = Integer.parseInt(scanner.next());
        Integer n = Integer.parseInt(scanner.next());

        Integer[] total = new Integer[n];
        for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        }


        List<Integer> kyori = new ArrayList<Integer>();
        for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        }

        kyori.add(Math.abs((k - total[n-1]) + total[0]));
        Collections.sort(kyori);
        kyori.remove(kyori.size() - 1);


        int sum = 0;
        for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        }

        System.out.println(sum);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) Integer k = Integer.parseInt(scanner.next()); k Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner Integer n = Integer.parseInt(scanner.next()); n Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner Integer[] total = new Integer[n]; total new Integer[n] n for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            total[i] = Integer.parseInt(scanner.next());
        } {
            total[i] = Integer.parseInt(scanner.next());
        } total[i] = Integer.parseInt(scanner.next()) total[i] total i Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner List<Integer> kyori = new ArrayList<Integer>(); kyori new ArrayList<Integer>() for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        } int i = 0; int i = 0; i 0 i < n-1 i n-1 n 1 i++ i++ i {
            kyori.add(Math.abs(total[i] - total[i+1]));
        } {
            kyori.add(Math.abs(total[i] - total[i+1]));
        } kyori.add(Math.abs(total[i] - total[i+1])) kyori.add kyori Math.abs(total[i] - total[i+1]) Math.abs Math total[i] - total[i+1] total[i] total i total[i+1] total i+1 i 1 kyori.add(Math.abs((k - total[n-1]) + total[0])) kyori.add kyori Math.abs((k - total[n-1]) + total[0]) Math.abs Math (k - total[n-1]) + total[0] (k - total[n-1]) k total[n-1] total n-1 n 1 total[0] total 0 Collections.sort(kyori) Collections.sort Collections kyori kyori.remove(kyori.size() - 1) kyori.remove kyori kyori.size() - 1 kyori.size() kyori.size kyori 1 int sum = 0; sum 0 for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        } int i = 0; int i = 0; i 0 i < kyori.size() i kyori.size() kyori.size kyori i++ i++ i {
            sum += kyori.get(i);
        } {
            sum += kyori.get(i);
        } sum += kyori.get(i) sum kyori.get(i) kyori.get kyori i System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer k = Integer.parseInt(scanner.next());
        Integer n = Integer.parseInt(scanner.next());

        Integer[] total = new Integer[n];
        for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        }


        List<Integer> kyori = new ArrayList<Integer>();
        for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        }

        kyori.add(Math.abs((k - total[n-1]) + total[0]));
        Collections.sort(kyori);
        kyori.remove(kyori.size() - 1);


        int sum = 0;
        for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        }

        System.out.println(sum);
    }
} public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer k = Integer.parseInt(scanner.next());
        Integer n = Integer.parseInt(scanner.next());

        Integer[] total = new Integer[n];
        for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        }


        List<Integer> kyori = new ArrayList<Integer>();
        for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        }

        kyori.add(Math.abs((k - total[n-1]) + total[0]));
        Collections.sort(kyori);
        kyori.remove(kyori.size() - 1);


        int sum = 0;
        for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        }

        System.out.println(sum);
    }
} Main
import java.util.*;
import java.lang.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer k = Integer.parseInt(scanner.next());
        Integer n = Integer.parseInt(scanner.next());

        Integer[] total = new Integer[n];
        for(int i = 0; i < n; i++) {
            total[i] = Integer.parseInt(scanner.next());
        }


        List<Integer> kyori = new ArrayList<Integer>();
        for(int i = 0; i < n-1; i++) {
            kyori.add(Math.abs(total[i] - total[i+1]));
        }

        kyori.add(Math.abs((k - total[n-1]) + total[0]));
        Collections.sort(kyori);
        kyori.remove(kyori.size() - 1);


        int sum = 0;
        for(int i = 0; i < kyori.size(); i++) {
            sum += kyori.get(i);
        }

        System.out.println(sum);
    }
}
