import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) {
        Scanner itr = new Scanner(System.in);
        int K = itr.nextInt();
        int N = itr.nextInt();
        int[] place = new int[N];

        for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        }
        Arrays.sort(place);
        int max = K - place[N - 1] + place[0];
        for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        }

        System.out.println(K - max);
    }
} import java.util.*; util.* java import java.math.*; math.* java public class Main {
    public static void main(String[] args) {
        Scanner itr = new Scanner(System.in);
        int K = itr.nextInt();
        int N = itr.nextInt();
        int[] place = new int[N];

        for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        }
        Arrays.sort(place);
        int max = K - place[N - 1] + place[0];
        for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        }

        System.out.println(K - max);
    }
} Main public static void main(String[] args) {
        Scanner itr = new Scanner(System.in);
        int K = itr.nextInt();
        int N = itr.nextInt();
        int[] place = new int[N];

        for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        }
        Arrays.sort(place);
        int max = K - place[N - 1] + place[0];
        for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        }

        System.out.println(K - max);
    } main {
        Scanner itr = new Scanner(System.in);
        int K = itr.nextInt();
        int N = itr.nextInt();
        int[] place = new int[N];

        for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        }
        Arrays.sort(place);
        int max = K - place[N - 1] + place[0];
        for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        }

        System.out.println(K - max);
    } Scanner itr = new Scanner(System.in); itr new Scanner(System.in) int K = itr.nextInt(); K itr.nextInt() itr.nextInt itr int N = itr.nextInt(); N itr.nextInt() itr.nextInt itr int[] place = new int[N]; place new int[N] N for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            place[i] = itr.nextInt();
        } {
            place[i] = itr.nextInt();
        } place[i] = itr.nextInt() place[i] place i itr.nextInt() itr.nextInt itr Arrays.sort(place) Arrays.sort Arrays place int max = K - place[N - 1] + place[0]; max K - place[N - 1] + place[0] K - place[N - 1] K place[N - 1] place N - 1 N 1 place[0] place 0 for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            max = Math.max(max, place[i] - place[i - 1]);
        } {
            max = Math.max(max, place[i] - place[i - 1]);
        } max = Math.max(max, place[i] - place[i - 1]) max Math.max(max, place[i] - place[i - 1]) Math.max Math max place[i] - place[i - 1] place[i] place i place[i - 1] place i - 1 i 1 System.out.println(K - max) System.out.println System.out System System.out K - max K max String[] args args public class Main {
    public static void main(String[] args) {
        Scanner itr = new Scanner(System.in);
        int K = itr.nextInt();
        int N = itr.nextInt();
        int[] place = new int[N];

        for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        }
        Arrays.sort(place);
        int max = K - place[N - 1] + place[0];
        for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        }

        System.out.println(K - max);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner itr = new Scanner(System.in);
        int K = itr.nextInt();
        int N = itr.nextInt();
        int[] place = new int[N];

        for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        }
        Arrays.sort(place);
        int max = K - place[N - 1] + place[0];
        for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        }

        System.out.println(K - max);
    }
} Main
import java.util.*;
import java.math.*;

public class Main {
    public static void main(String[] args) {
        Scanner itr = new Scanner(System.in);
        int K = itr.nextInt();
        int N = itr.nextInt();
        int[] place = new int[N];

        for (int i = 0; i < N; i++) {
            place[i] = itr.nextInt();
        }
        Arrays.sort(place);
        int max = K - place[N - 1] + place[0];
        for (int i = 1; i < N; i++) {
            max = Math.max(max, place[i] - place[i - 1]);
        }

        System.out.println(K - max);
    }
}