import java.util.*;
import javax.lang.model.util.ElementScanner6;
//import java.lang.Math;

public class Main {

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

        // 入力
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }

        // 処理
        int out = k;

        //時計回り
        for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }

        //反時計回り
        for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }
        
        // 出力
        System.out.println(out);
    }
} import java.util.*; util.* java import javax.lang.model.util.ElementScanner6; ElementScanner6 javax.lang.model.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[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }

        // 処理
        int out = k;

        //時計回り
        for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }

        //反時計回り
        for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }
        
        // 出力
        System.out.println(out);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 入力
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }

        // 処理
        int out = k;

        //時計回り
        for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }

        //反時計回り
        for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }
        
        // 出力
        System.out.println(out);
    } main {
        Scanner sc = new Scanner(System.in);

        // 入力
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }

        // 処理
        int out = k;

        //時計回り
        for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }

        //反時計回り
        for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }
        
        // 出力
        System.out.println(out);
    } 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[] a = new int[n]; a new int[n] n for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = sc.nextInt();
        } {
            a[i] = sc.nextInt();
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int out = k; out k for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        } {
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        } int dist = a[(i - 1 + n) % n] - a[i]; dist a[(i - 1 + n) % n] - a[i] a[(i - 1 + n) % n] a (i - 1 + n) % n (i - 1 + n) i - 1 i 1 n n a[i] a i if (dist < 0) {
                dist += k;
            } dist < 0 dist 0 {
                dist += k;
            } dist += k dist k out = dist < out ? dist : out out dist < out ? dist : out dist < out dist out dist out for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        } {
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        } int dist = k - a[(i + 1 + n) % n] + a[i]; dist k - a[(i + 1 + n) % n] + a[i] k - a[(i + 1 + n) % n] k a[(i + 1 + n) % n] a (i + 1 + n) % n (i + 1 + n) i + 1 + n i 1 n n a[i] a i if (dist < 0) {
                dist += k;
            } dist < 0 dist 0 {
                dist += k;
            } dist += k dist k out = dist < out ? dist : out out dist < out ? dist : out dist < out dist out dist out System.out.println(out) System.out.println System.out System System.out out 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[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }

        // 処理
        int out = k;

        //時計回り
        for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }

        //反時計回り
        for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }
        
        // 出力
        System.out.println(out);
    }
} public class Main {

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

        // 入力
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }

        // 処理
        int out = k;

        //時計回り
        for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }

        //反時計回り
        for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }
        
        // 出力
        System.out.println(out);
    }
} Main
import java.util.*;
import javax.lang.model.util.ElementScanner6;
//import java.lang.Math;

public class Main {

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

        // 入力
        int k = sc.nextInt();
        int n = sc.nextInt();

        int[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }

        // 処理
        int out = k;

        //時計回り
        for(int i = 0; i < n; i++){
            int dist = a[(i - 1 + n) % n] - a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }

        //反時計回り
        for(int i = 0; i < n; i++){
            int dist = k - a[(i + 1 + n) % n] + a[i];
            if (dist < 0) {
                dist += k;
            }
            out = dist < out ? dist : out;
        }
        
        // 出力
        System.out.println(out);
    }
}