import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] a = new int[n];
        int max = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        }
        for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        }
        int out = Math.abs(k - a[b]) + a[b - 1];
        if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        }
        System.out.println(out);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] a = new int[n];
        int max = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        }
        for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        }
        int out = Math.abs(k - a[b]) + a[b - 1];
        if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        }
        System.out.println(out);
    }
} Main public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] a = new int[n];
        int max = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        }
        for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        }
        int out = Math.abs(k - a[b]) + a[b - 1];
        if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        }
        System.out.println(out);
    } main {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] a = new int[n];
        int max = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        }
        for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        }
        int out = Math.abs(k - a[b]) + a[b - 1];
        if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        }
        System.out.println(out);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int[] a = new int[n]; a new int[n] n int max = 0; max 0 int b = 0; b 0 for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = scan.nextInt();
        } {
            a[i] = scan.nextInt();
        } a[i] = scan.nextInt() a[i] a i scan.nextInt() scan.nextInt scan for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        } {
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        } if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            } a[i] - a[i - 1] > max a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 max {
                max = a[i] - a[i - 1];
                b = i;
            } max = a[i] - a[i - 1] max a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 b = i b i int out = Math.abs(k - a[b]) + a[b - 1]; out Math.abs(k - a[b]) + a[b - 1] Math.abs(k - a[b]) Math.abs Math k - a[b] k a[b] a b a[b - 1] a b - 1 b 1 if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        } k - a[n - 1] + a[0] > max k - a[n - 1] + a[0] k - a[n - 1] k a[n - 1] a n - 1 n 1 a[0] a 0 max {
            out = a[n - 1] - a[0];
        } out = a[n - 1] - a[0] out a[n - 1] - a[0] a[n - 1] a n - 1 n 1 a[0] a 0 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 scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] a = new int[n];
        int max = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        }
        for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        }
        int out = Math.abs(k - a[b]) + a[b - 1];
        if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        }
        System.out.println(out);
    }
} public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] a = new int[n];
        int max = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        }
        for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        }
        int out = Math.abs(k - a[b]) + a[b - 1];
        if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        }
        System.out.println(out);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] a = new int[n];
        int max = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
        }
        for(int i = 1; i < n; i++){
            if(a[i] - a[i - 1] > max){
                max = a[i] - a[i - 1];
                b = i;
            }
        }
        int out = Math.abs(k - a[b]) + a[b - 1];
        if(k - a[n - 1] + a[0] > max){
            out = a[n - 1] - a[0];
        }
        System.out.println(out);
    }
}