import 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();
        int lastPoints = 0;
        int firstDistance = 0;
        int[] distanceArray = new int[N];
        for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        }
        sc.close();
        Arrays.sort(distanceArray);
        int minDistance = 0;
        for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        }

        System.out.println(minDistance);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int lastPoints = 0;
        int firstDistance = 0;
        int[] distanceArray = new int[N];
        for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        }
        sc.close();
        Arrays.sort(distanceArray);
        int minDistance = 0;
        for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        }

        System.out.println(minDistance);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int lastPoints = 0;
        int firstDistance = 0;
        int[] distanceArray = new int[N];
        for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        }
        sc.close();
        Arrays.sort(distanceArray);
        int minDistance = 0;
        for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        }

        System.out.println(minDistance);
    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int lastPoints = 0;
        int firstDistance = 0;
        int[] distanceArray = new int[N];
        for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        }
        sc.close();
        Arrays.sort(distanceArray);
        int minDistance = 0;
        for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        }

        System.out.println(minDistance);
    } 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 lastPoints = 0; lastPoints 0 int firstDistance = 0; firstDistance 0 int[] distanceArray = new int[N]; distanceArray new int[N] N for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        } {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        } int point = sc.nextInt(); point sc.nextInt() sc.nextInt sc int distance = 0; distance 0 if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            } i == 0 i 0 {
                lastPoints = point;
                firstDistance = point;
                continue;
            } lastPoints = point lastPoints point firstDistance = point firstDistance point continue; distance = point - lastPoints distance point - lastPoints point lastPoints if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            } i == N - 1 i N - 1 N 1 {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            } int lastDistance = K - point + firstDistance; lastDistance K - point + firstDistance K - point K point firstDistance distanceArray[i] = lastDistance distanceArray[i] distanceArray i lastDistance distanceArray[i - 1] = distance distanceArray[i - 1] distanceArray i - 1 i 1 distance lastPoints = point lastPoints point sc.close() sc.close sc Arrays.sort(distanceArray) Arrays.sort Arrays distanceArray int minDistance = 0; minDistance 0 for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        } int i = 0; int i = 0; i 0 i < N - 1 i N - 1 N 1 i++ i++ i {
            minDistance += distanceArray[i];
        } {
            minDistance += distanceArray[i];
        } minDistance += distanceArray[i] minDistance distanceArray[i] distanceArray i System.out.println(minDistance) System.out.println System.out System System.out minDistance 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();
        int lastPoints = 0;
        int firstDistance = 0;
        int[] distanceArray = new int[N];
        for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        }
        sc.close();
        Arrays.sort(distanceArray);
        int minDistance = 0;
        for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        }

        System.out.println(minDistance);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        int lastPoints = 0;
        int firstDistance = 0;
        int[] distanceArray = new int[N];
        for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        }
        sc.close();
        Arrays.sort(distanceArray);
        int minDistance = 0;
        for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        }

        System.out.println(minDistance);
    }
} Main
import 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();
        int lastPoints = 0;
        int firstDistance = 0;
        int[] distanceArray = new int[N];
        for (int i = 0; i < N; i++) {
            int point = sc.nextInt();
            int distance = 0;
            if (i == 0) {
                lastPoints = point;
                firstDistance = point;
                continue;
            }
            distance = point - lastPoints;
            if (i == N - 1) {
                int lastDistance = K - point + firstDistance;
                distanceArray[i] = lastDistance;
            }
            distanceArray[i - 1] = distance;
            lastPoints = point;
        }
        sc.close();
        Arrays.sort(distanceArray);
        int minDistance = 0;
        for (int i = 0; i < N - 1; i++) {
            minDistance += distanceArray[i];
        }

        System.out.println(minDistance);
    }
}
