import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] houses = new int[n];
        int prev = sc.nextInt();
        houses[0] = prev;
        int max = 0;
        int sum = 0;
        for (int i = 1; i < n; i++) {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        }
        int gap = houses[0] + k - houses[n-1];
        max = Integer.max(max, gap);
        sum += gap;
        sum -= max;
        System.out.println(sum);
    }
} import java.util.*; util.* java class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] houses = new int[n];
        int prev = sc.nextInt();
        houses[0] = prev;
        int max = 0;
        int sum = 0;
        for (int i = 1; i < n; i++) {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        }
        int gap = houses[0] + k - houses[n-1];
        max = Integer.max(max, gap);
        sum += gap;
        sum -= max;
        System.out.println(sum);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] houses = new int[n];
        int prev = sc.nextInt();
        houses[0] = prev;
        int max = 0;
        int sum = 0;
        for (int i = 1; i < n; i++) {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        }
        int gap = houses[0] + k - houses[n-1];
        max = Integer.max(max, gap);
        sum += gap;
        sum -= max;
        System.out.println(sum);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] houses = new int[n];
        int prev = sc.nextInt();
        houses[0] = prev;
        int max = 0;
        int sum = 0;
        for (int i = 1; i < n; i++) {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        }
        int gap = houses[0] + k - houses[n-1];
        max = Integer.max(max, gap);
        sum += gap;
        sum -= max;
        System.out.println(sum);
    } 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 int[] houses = new int[n]; houses new int[n] n int prev = sc.nextInt(); prev sc.nextInt() sc.nextInt sc houses[0] = prev houses[0] houses 0 prev int max = 0; max 0 int sum = 0; sum 0 for (int i = 1; i < n; i++) {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        } {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        } int place = sc.nextInt(); place sc.nextInt() sc.nextInt sc int gap = place - prev; gap place - prev place prev max = Integer.max(max, gap) max Integer.max(max, gap) Integer.max Integer max gap prev = place prev place sum += gap sum gap houses[i] = place houses[i] houses i place int gap = houses[0] + k - houses[n-1]; gap houses[0] + k - houses[n-1] houses[0] + k houses[0] houses 0 k houses[n-1] houses n-1 n 1 max = Integer.max(max, gap) max Integer.max(max, gap) Integer.max Integer max gap sum += gap sum gap sum -= max sum max System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        int[] houses = new int[n];
        int prev = sc.nextInt();
        houses[0] = prev;
        int max = 0;
        int sum = 0;
        for (int i = 1; i < n; i++) {
            int place = sc.nextInt();
            int gap = place - prev;
            max = Integer.max(max, gap);
            prev = place;
            sum += gap;
            houses[i] = place;
        }
        int gap = houses[0] + k - houses[n-1];
        max = Integer.max(max, gap);
        sum += gap;
        sum -= max;
        System.out.println(sum);
    }
}
