import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int k = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        line = br.readLine().split(" ");
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        }
        int ma = a[0]+k-a[n-1];
        for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        }
        System.out.println(k-ma);
    }
} import java.io.*; io.* java public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int k = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        line = br.readLine().split(" ");
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        }
        int ma = a[0]+k-a[n-1];
        for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        }
        System.out.println(k-ma);
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int k = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        line = br.readLine().split(" ");
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        }
        int ma = a[0]+k-a[n-1];
        for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        }
        System.out.println(k-ma);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int k = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        line = br.readLine().split(" ");
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        }
        int ma = a[0]+k-a[n-1];
        for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        }
        System.out.println(k-ma);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] line = br.readLine().split(" "); line br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int k = Integer.parseInt(line[0]); k Integer.parseInt(line[0]) Integer.parseInt Integer line[0] line 0 int n = Integer.parseInt(line[1]); n Integer.parseInt(line[1]) Integer.parseInt Integer line[1] line 1 line = br.readLine().split(" ") line br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int a[] = new int[n]; a new int[n] n for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = Integer.parseInt(line[i]);
        } {
            a[i] = Integer.parseInt(line[i]);
        } a[i] = Integer.parseInt(line[i]) a[i] a i Integer.parseInt(line[i]) Integer.parseInt Integer line[i] line i int ma = a[0]+k-a[n-1]; ma a[0]+k-a[n-1] a[0]+k a[0] a 0 k a[n-1] a n-1 n 1 for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            ma = Math.max(ma, a[i]-a[i-1]);
        } {
            ma = Math.max(ma, a[i]-a[i-1]);
        } ma = Math.max(ma, a[i]-a[i-1]) ma Math.max(ma, a[i]-a[i-1]) Math.max Math ma a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(k-ma) System.out.println System.out System System.out k-ma k ma String[] args args public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int k = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        line = br.readLine().split(" ");
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        }
        int ma = a[0]+k-a[n-1];
        for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        }
        System.out.println(k-ma);
    }
} public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int k = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        line = br.readLine().split(" ");
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        }
        int ma = a[0]+k-a[n-1];
        for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        }
        System.out.println(k-ma);
    }
} Main
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int k = Integer.parseInt(line[0]);
        int n = Integer.parseInt(line[1]);
        line = br.readLine().split(" ");
        int a[] = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = Integer.parseInt(line[i]);
        }
        int ma = a[0]+k-a[n-1];
        for(int i = 1; i < n; i++){
            ma = Math.max(ma, a[i]-a[i-1]);
        }
        System.out.println(k-ma);
    }
}
