import java.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();
        }
        Arrays.sort(a);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        }
        Arrays.sort(dist);
        System.out.print(k - dist[n-1]);
    }
} import java.util.*; util.* java 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();
        }
        Arrays.sort(a);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        }
        Arrays.sort(dist);
        System.out.print(k - dist[n-1]);
    }
} 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();
        }
        Arrays.sort(a);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        }
        Arrays.sort(dist);
        System.out.print(k - dist[n-1]);
    } 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();
        }
        Arrays.sort(a);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        }
        Arrays.sort(dist);
        System.out.print(k - dist[n-1]);
    } 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 Arrays.sort(a) Arrays.sort Arrays a int[] dist = new int[n]; dist new int[n] n for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        } {
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        } if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            } i != n-1 i n-1 n 1 {
                dist[i] = a[i+1] - a[i];
            } dist[i] = a[i+1] - a[i] dist[i] dist i a[i+1] - a[i] a[i+1] a i+1 i 1 a[i] a i {
                dist[i] = a[0]+k - a[i];
            } dist[i] = a[0]+k - a[i] dist[i] dist i a[0]+k - a[i] a[0]+k a[0] a 0 k a[i] a i Arrays.sort(dist) Arrays.sort Arrays dist System.out.print(k - dist[n-1]) System.out.print System.out System System.out k - dist[n-1] k dist[n-1] dist n-1 n 1 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();
        }
        Arrays.sort(a);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        }
        Arrays.sort(dist);
        System.out.print(k - dist[n-1]);
    }
} 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();
        }
        Arrays.sort(a);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        }
        Arrays.sort(dist);
        System.out.print(k - dist[n-1]);
    }
} Main
import java.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();
        }
        Arrays.sort(a);
        int[] dist = new int[n];
        for(int i = 0; i < n; i++){
            if(i != n-1){
                dist[i] = a[i+1] - a[i];
            } else {
                dist[i] = a[0]+k - a[i];
            }
        }
        Arrays.sort(dist);
        System.out.print(k - dist[n-1]);
    }
}