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();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        int distance = k - list.get(n-1) + list.get(0);
        for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        }
        System.out.println(k - distance);
    }
} 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();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        int distance = k - list.get(n-1) + list.get(0);
        for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        }
        System.out.println(k - distance);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        int distance = k - list.get(n-1) + list.get(0);
        for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        }
        System.out.println(k - distance);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        int distance = k - list.get(n-1) + list.get(0);
        for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        }
        System.out.println(k - distance);
    } 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 ArrayList<Integer> list = new ArrayList<Integer>(); list new ArrayList<Integer>() for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            list.add(sc.nextInt());
        } {
            list.add(sc.nextInt());
        } list.add(sc.nextInt()) list.add list sc.nextInt() sc.nextInt sc int distance = k - list.get(n-1) + list.get(0); distance k - list.get(n-1) + list.get(0) k - list.get(n-1) k list.get(n-1) list.get list n-1 n 1 list.get(0) list.get list 0 for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        } int j = 2; int j = 2; j 2 j < n j n j++ j++ j {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        } {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        } if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            } distance < list.get(j) - list.get(j-1) distance list.get(j) - list.get(j-1) list.get(j) list.get list j list.get(j-1) list.get list j-1 j 1 {
                distance = list.get(j) - list.get(j-1);
            } distance = list.get(j) - list.get(j-1) distance list.get(j) - list.get(j-1) list.get(j) list.get list j list.get(j-1) list.get list j-1 j 1 System.out.println(k - distance) System.out.println System.out System System.out k - distance k distance 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();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        int distance = k - list.get(n-1) + list.get(0);
        for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        }
        System.out.println(k - distance);
    }
} public class Main {
  	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        int distance = k - list.get(n-1) + list.get(0);
        for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        }
        System.out.println(k - distance);
    }
} 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();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        int distance = k - list.get(n-1) + list.get(0);
        for (int j = 2; j < n; j++) {
            if (distance < list.get(j) - list.get(j-1)) {
                distance = list.get(j) - list.get(j-1);
            }
        }
        System.out.println(k - distance);
    }
}
