import java.util.Scanner;

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

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int[] dist = new int[y];

        for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        }

        scanner.close();

        int[] sa = new int[y];

        for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        }

        dist = null;

        int max = sa[0];
        int sum = 0;
        for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        }

        System.out.println(sum - max);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int[] dist = new int[y];

        for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        }

        scanner.close();

        int[] sa = new int[y];

        for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        }

        dist = null;

        int max = sa[0];
        int sum = 0;
        for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        }

        System.out.println(sum - max);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int[] dist = new int[y];

        for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        }

        scanner.close();

        int[] sa = new int[y];

        for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        }

        dist = null;

        int max = sa[0];
        int sum = 0;
        for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        }

        System.out.println(sum - max);
    } main {
        Scanner scanner = new Scanner(System.in);

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int[] dist = new int[y];

        for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        }

        scanner.close();

        int[] sa = new int[y];

        for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        }

        dist = null;

        int max = sa[0];
        int sum = 0;
        for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        }

        System.out.println(sum - max);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int x = scanner.nextInt(); x scanner.nextInt() scanner.nextInt scanner int y = scanner.nextInt(); y scanner.nextInt() scanner.nextInt scanner int[] dist = new int[y]; dist new int[y] y for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < y i y i++ i++ i {
            dist[i] = scanner.nextInt();
        } {
            dist[i] = scanner.nextInt();
        } dist[i] = scanner.nextInt() dist[i] dist i scanner.nextInt() scanner.nextInt scanner scanner.close() scanner.close scanner int[] sa = new int[y]; sa new int[y] y for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        } int i = 0; int i = 0; i 0 i < dist.length i dist.length dist dist.length i++ i++ i {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        } {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        } if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            } i == 0 i 0 {
                sa[0] = dist[0];
            } sa[0] = dist[0] sa[0] sa 0 dist[0] dist 0 {
                sa[i] = dist[i] - dist[i - 1];
            } sa[i] = dist[i] - dist[i - 1] sa[i] sa i dist[i] - dist[i - 1] dist[i] dist i dist[i - 1] dist i - 1 i 1 if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            } i == dist.length - 1 i dist.length - 1 dist.length dist dist.length 1 {
                sa[0] += x - dist[i];
            } sa[0] += x - dist[i] sa[0] sa 0 x - dist[i] x dist[i] dist i dist = null dist null int max = sa[0]; max sa[0] sa 0 int sum = 0; sum 0 for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        } int i sa {
            if (i > max) {
                max = i;
            }
            sum += i;
        } {
            if (i > max) {
                max = i;
            }
            sum += i;
        } if (i > max) {
                max = i;
            } i > max i max {
                max = i;
            } max = i max i sum += i sum i System.out.println(sum - max) System.out.println System.out System System.out sum - max sum max String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int[] dist = new int[y];

        for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        }

        scanner.close();

        int[] sa = new int[y];

        for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        }

        dist = null;

        int max = sa[0];
        int sum = 0;
        for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        }

        System.out.println(sum - max);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int[] dist = new int[y];

        for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        }

        scanner.close();

        int[] sa = new int[y];

        for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        }

        dist = null;

        int max = sa[0];
        int sum = 0;
        for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        }

        System.out.println(sum - max);
    }
} Main
import java.util.Scanner;

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

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int[] dist = new int[y];

        for (int i = 0; i < y; i++) {
            dist[i] = scanner.nextInt();
        }

        scanner.close();

        int[] sa = new int[y];

        for (int i = 0; i < dist.length; i++) {
            if (i == 0) {
                sa[0] = dist[0];
            } else {
                sa[i] = dist[i] - dist[i - 1];
            }
            if (i == dist.length - 1) {
                sa[0] += x - dist[i];
            }
        }

        dist = null;

        int max = sa[0];
        int sum = 0;
        for (int i : sa) {
            if (i > max) {
                max = i;
            }
            sum += i;
        }

        System.out.println(sum - max);
    }
}