import java.util.*;

class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i = 0;i < n;i++){
            int dis = sc.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = k- list.get(n-1);
        }else{
            max = k - list.get(n-1) + list.get(0);
        }

        for(int i = 1;i < n;i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
        System.out.println(k-max);
    }
} import java.util.*; util.* java class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i = 0;i < n;i++){
            int dis = sc.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = k- list.get(n-1);
        }else{
            max = k - list.get(n-1) + list.get(0);
        }

        for(int i = 1;i < n;i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
        System.out.println(k-max);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i = 0;i < n;i++){
            int dis = sc.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = k- list.get(n-1);
        }else{
            max = k - list.get(n-1) + list.get(0);
        }

        for(int i = 1;i < n;i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
        System.out.println(k-max);
    } main {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i = 0;i < n;i++){
            int dis = sc.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = k- list.get(n-1);
        }else{
            max = k - list.get(n-1) + list.get(0);
        }

        for(int i = 1;i < n;i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
        System.out.println(k-max);
    } 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 List<Integer> list = new ArrayList<>(); list new ArrayList<>() long max = 0; max 0 for(int i = 0;i < n;i++){
            int dis = sc.nextInt();
            list.add(dis);
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int dis = sc.nextInt();
            list.add(dis);
        } {
            int dis = sc.nextInt();
            list.add(dis);
        } int dis = sc.nextInt(); dis sc.nextInt() sc.nextInt sc list.add(dis) list.add list dis if(list.contains(0)){
            max = k- list.get(n-1);
        }else{
            max = k - list.get(n-1) + list.get(0);
        } list.contains(0) list.contains list 0 {
            max = k- list.get(n-1);
        } max = k- list.get(n-1) max k- list.get(n-1) k list.get(n-1) list.get list n-1 n 1 {
            max = k - list.get(n-1) + list.get(0);
        } max = k - list.get(n-1) + list.get(0) max 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 i = 1;i < n;i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        } {
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        } long distance = list.get(i)-list.get(i-1); distance list.get(i)-list.get(i-1) list.get(i) list.get list i list.get(i-1) list.get list i-1 i 1 if(distance>max){
                max = distance;
            } distance>max distance max {
                max = distance;
            } max = distance max distance System.out.println(k-max) System.out.println System.out System System.out k-max k max String[] args args
import java.util.*;

class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        long max = 0;

        for(int i = 0;i < n;i++){
            int dis = sc.nextInt();
            list.add(dis);
        }

        if(list.contains(0)){
            max = k- list.get(n-1);
        }else{
            max = k - list.get(n-1) + list.get(0);
        }

        for(int i = 1;i < n;i++){
            long distance = list.get(i)-list.get(i-1);
            if(distance>max){
                max = distance;
            }
        }
        
        System.out.println(k-max);
    }
}