import 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[] dis = new Integer[N];
        Integer[] total = new Integer[N];

        for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        }
        for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        }

        total[N-1] = Math.abs((K - dis[N-1]) + dis[0]);
        Arrays.sort(total);
        total[total.length-1] = 0;
        Integer sum = 0;
        for (int i = 0; i < total.length; i++) sum += total[i];
        System.out.println(sum);
    }
} import java.util.*; util.* java 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[] dis = new Integer[N];
        Integer[] total = new Integer[N];

        for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        }
        for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        }

        total[N-1] = Math.abs((K - dis[N-1]) + dis[0]);
        Arrays.sort(total);
        total[total.length-1] = 0;
        Integer sum = 0;
        for (int i = 0; i < total.length; i++) sum += total[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[] dis = new Integer[N];
        Integer[] total = new Integer[N];

        for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        }
        for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        }

        total[N-1] = Math.abs((K - dis[N-1]) + dis[0]);
        Arrays.sort(total);
        total[total.length-1] = 0;
        Integer sum = 0;
        for (int i = 0; i < total.length; i++) sum += total[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[] dis = new Integer[N];
        Integer[] total = new Integer[N];

        for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        }
        for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        }

        total[N-1] = Math.abs((K - dis[N-1]) + dis[0]);
        Arrays.sort(total);
        total[total.length-1] = 0;
        Integer sum = 0;
        for (int i = 0; i < total.length; i++) sum += total[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[] dis = new Integer[N]; dis new Integer[N] N Integer[] total = new Integer[N]; total new Integer[N] N for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
           dis[i] = Integer.parseInt(scanner.next());
        } {
           dis[i] = Integer.parseInt(scanner.next());
        } dis[i] = Integer.parseInt(scanner.next()) dis[i] dis i Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        } {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        } total[i] = Math.abs(dis[i] - dis[i + 1]) total[i] total i Math.abs(dis[i] - dis[i + 1]) Math.abs Math dis[i] - dis[i + 1] dis[i] dis i dis[i + 1] dis i + 1 i 1 total[N-1] = Math.abs((K - dis[N-1]) + dis[0]) total[N-1] total N-1 N 1 Math.abs((K - dis[N-1]) + dis[0]) Math.abs Math (K - dis[N-1]) + dis[0] (K - dis[N-1]) K dis[N-1] dis N-1 N 1 dis[0] dis 0 Arrays.sort(total) Arrays.sort Arrays total total[total.length-1] = 0 total[total.length-1] total total.length-1 total.length total total.length 1 0 Integer sum = 0; sum 0 for (int i = 0; i < total.length; i++) sum += total[i]; int i = 0; int i = 0; i 0 i < total.length i total.length total total.length i++ i++ i sum += total[i]; sum += total[i] sum total[i] total 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[] dis = new Integer[N];
        Integer[] total = new Integer[N];

        for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        }
        for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        }

        total[N-1] = Math.abs((K - dis[N-1]) + dis[0]);
        Arrays.sort(total);
        total[total.length-1] = 0;
        Integer sum = 0;
        for (int i = 0; i < total.length; i++) sum += total[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[] dis = new Integer[N];
        Integer[] total = new Integer[N];

        for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        }
        for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        }

        total[N-1] = Math.abs((K - dis[N-1]) + dis[0]);
        Arrays.sort(total);
        total[total.length-1] = 0;
        Integer sum = 0;
        for (int i = 0; i < total.length; i++) sum += total[i];
        System.out.println(sum);
    }
} Main
import 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[] dis = new Integer[N];
        Integer[] total = new Integer[N];

        for (int i = 0; i < N; i++) {
           dis[i] = Integer.parseInt(scanner.next());
        }
        for (int i = 0; i < N-1; i++) {
            total[i] = Math.abs(dis[i] - dis[i + 1]);
        }

        total[N-1] = Math.abs((K - dis[N-1]) + dis[0]);
        Arrays.sort(total);
        total[total.length-1] = 0;
        Integer sum = 0;
        for (int i = 0; i < total.length; i++) sum += total[i];
        System.out.println(sum);
    }
}
