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();

        long first = 0;
        long previous = 0;
        long max = 0;
        long sum = 0;
        for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        }
        long saigo = k - sum + first;

        if (saigo > max) {
            max = saigo;
        }
        System.out.println(k - max);
    }
} 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();

        long first = 0;
        long previous = 0;
        long max = 0;
        long sum = 0;
        for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        }
        long saigo = k - sum + first;

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

        int k = scanner.nextInt();
        int n = scanner.nextInt();

        long first = 0;
        long previous = 0;
        long max = 0;
        long sum = 0;
        for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        }
        long saigo = k - sum + first;

        if (saigo > max) {
            max = saigo;
        }
        System.out.println(k - max);
    } main {
        Scanner scanner = new Scanner(System.in);

        int k = scanner.nextInt();
        int n = scanner.nextInt();

        long first = 0;
        long previous = 0;
        long max = 0;
        long sum = 0;
        for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        }
        long saigo = k - sum + first;

        if (saigo > max) {
            max = saigo;
        }
        System.out.println(k - max);
    } 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 long first = 0; first 0 long previous = 0; previous 0 long max = 0; max 0 long sum = 0; sum 0 for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        } long i = 0; long i = 0; i 0 i < n i n i++ i++ i {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        } {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        } int a = scanner.nextInt(); a scanner.nextInt() scanner.nextInt scanner if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            } i == 0 i 0 {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } first = a first a previous = a previous a max = a max a sum = a sum a {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            } long aida = a - previous; aida a - previous a previous previous = a previous a sum = sum + aida sum sum + aida sum aida if (aida > max) {
                    max = aida;
                } aida > max aida max {
                    max = aida;
                } max = aida max aida long saigo = k - sum + first; saigo k - sum + first k - sum k sum first if (saigo > max) {
            max = saigo;
        } saigo > max saigo max {
            max = saigo;
        } max = saigo max saigo System.out.println(k - max) System.out.println System.out System System.out k - max k max 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();

        long first = 0;
        long previous = 0;
        long max = 0;
        long sum = 0;
        for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        }
        long saigo = k - sum + first;

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

        int k = scanner.nextInt();
        int n = scanner.nextInt();

        long first = 0;
        long previous = 0;
        long max = 0;
        long sum = 0;
        for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        }
        long saigo = k - sum + first;

        if (saigo > max) {
            max = saigo;
        }
        System.out.println(k - max);
    }
} 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();

        long first = 0;
        long previous = 0;
        long max = 0;
        long sum = 0;
        for (long i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (i == 0) {
                first = a;
                previous = a;
                max = a;
                sum = a;

            } else {
                long aida = a - previous;
                previous = a;
                sum = sum + aida;
                if (aida > max) {
                    max = aida;
                }
            }
        }
        long saigo = k - sum + first;

        if (saigo > max) {
            max = saigo;
        }
        System.out.println(k - max);
    }
}
