
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int K = scanner.nextInt();
        int N = scanner.nextInt();

        int[] A = new int[N + 1];

        int longestDist = 0;
        int totalDist = 0;
        for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        }

        int dist = K - A[N] + A[1];
        totalDist += dist;
        longestDist = Math.max(longestDist, dist);
        System.out.println(totalDist - longestDist);
    }
} import java.util.Scanner; Scanner 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();

        int[] A = new int[N + 1];

        int longestDist = 0;
        int totalDist = 0;
        for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        }

        int dist = K - A[N] + A[1];
        totalDist += dist;
        longestDist = Math.max(longestDist, dist);
        System.out.println(totalDist - longestDist);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int K = scanner.nextInt();
        int N = scanner.nextInt();

        int[] A = new int[N + 1];

        int longestDist = 0;
        int totalDist = 0;
        for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        }

        int dist = K - A[N] + A[1];
        totalDist += dist;
        longestDist = Math.max(longestDist, dist);
        System.out.println(totalDist - longestDist);
    } main {
        Scanner scanner = new Scanner(System.in);

        int K = scanner.nextInt();
        int N = scanner.nextInt();

        int[] A = new int[N + 1];

        int longestDist = 0;
        int totalDist = 0;
        for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        }

        int dist = K - A[N] + A[1];
        totalDist += dist;
        longestDist = Math.max(longestDist, dist);
        System.out.println(totalDist - longestDist);
    } 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 int[] A = new int[N + 1]; A new int[N + 1] N + 1 N 1 int longestDist = 0; longestDist 0 int totalDist = 0; totalDist 0 for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        } int i = 1 ; int i = 1 ; i 1 i <= N i N i++ i++ i {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        } {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        } A[i] = scanner.nextInt() A[i] A i scanner.nextInt() scanner.nextInt scanner if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            } i != 1 i 1 {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            } int dist = A[i] - A[i - 1]; dist A[i] - A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 totalDist += dist totalDist dist longestDist = Math.max(longestDist, dist) longestDist Math.max(longestDist, dist) Math.max Math longestDist dist int dist = K - A[N] + A[1]; dist K - A[N] + A[1] K - A[N] K A[N] A N A[1] A 1 totalDist += dist totalDist dist longestDist = Math.max(longestDist, dist) longestDist Math.max(longestDist, dist) Math.max Math longestDist dist System.out.println(totalDist - longestDist) System.out.println System.out System System.out totalDist - longestDist totalDist longestDist 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();

        int[] A = new int[N + 1];

        int longestDist = 0;
        int totalDist = 0;
        for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        }

        int dist = K - A[N] + A[1];
        totalDist += dist;
        longestDist = Math.max(longestDist, dist);
        System.out.println(totalDist - longestDist);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int K = scanner.nextInt();
        int N = scanner.nextInt();

        int[] A = new int[N + 1];

        int longestDist = 0;
        int totalDist = 0;
        for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        }

        int dist = K - A[N] + A[1];
        totalDist += dist;
        longestDist = Math.max(longestDist, dist);
        System.out.println(totalDist - longestDist);
    }
} Main

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int K = scanner.nextInt();
        int N = scanner.nextInt();

        int[] A = new int[N + 1];

        int longestDist = 0;
        int totalDist = 0;
        for (int i = 1 ; i <= N ; i++) {
            A[i] = scanner.nextInt();
            if (i != 1) {
                int dist = A[i] - A[i - 1];
                totalDist += dist;
                longestDist = Math.max(longestDist, dist);
            }
        }

        int dist = K - A[N] + A[1];
        totalDist += dist;
        longestDist = Math.max(longestDist, dist);
        System.out.println(totalDist - longestDist);
    }
}
