import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> house = new ArrayList<Integer>();
        for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        }
        int maxDistance = house.get(0) + K - house.get(N-1);
        for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        }
        System.out.println(K-maxDistance);
    }
} import java.util.ArrayList; ArrayList java.util import java.util.List; List java.util import java.util.Scanner; Scanner 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();
        List<Integer> house = new ArrayList<Integer>();
        for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        }
        int maxDistance = house.get(0) + K - house.get(N-1);
        for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        }
        System.out.println(K-maxDistance);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> house = new ArrayList<Integer>();
        for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        }
        int maxDistance = house.get(0) + K - house.get(N-1);
        for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        }
        System.out.println(K-maxDistance);
    } main {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> house = new ArrayList<Integer>();
        for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        }
        int maxDistance = house.get(0) + K - house.get(N-1);
        for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        }
        System.out.println(K-maxDistance);
    } 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> house = new ArrayList<Integer>(); house new ArrayList<Integer>() for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            house.add(sc.nextInt());
        } {
            house.add(sc.nextInt());
        } house.add(sc.nextInt()) house.add house sc.nextInt() sc.nextInt sc int maxDistance = house.get(0) + K - house.get(N-1); maxDistance house.get(0) + K - house.get(N-1) house.get(0) + K house.get(0) house.get house 0 K house.get(N-1) house.get house N-1 N 1 for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        } {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        } maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i)) maxDistance Math.max(maxDistance, house.get(i+1) - house.get(i)) Math.max Math maxDistance house.get(i+1) - house.get(i) house.get(i+1) house.get house i+1 i 1 house.get(i) house.get house i System.out.println(K-maxDistance) System.out.println System.out System System.out K-maxDistance K maxDistance 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();
        List<Integer> house = new ArrayList<Integer>();
        for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        }
        int maxDistance = house.get(0) + K - house.get(N-1);
        for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        }
        System.out.println(K-maxDistance);
    }
} public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> house = new ArrayList<Integer>();
        for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        }
        int maxDistance = house.get(0) + K - house.get(N-1);
        for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        }
        System.out.println(K-maxDistance);
    }
} Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int N = sc.nextInt();
        List<Integer> house = new ArrayList<Integer>();
        for (int i=0; i<N; i++) {
            house.add(sc.nextInt());
        }
        int maxDistance = house.get(0) + K - house.get(N-1);
        for (int i=0; i<N-1; i++) {
            maxDistance = Math.max(maxDistance, house.get(i+1) - house.get(i));
        }
        System.out.println(K-maxDistance);
    }
}