import java.util.*;

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

        int[] distances = new int[N];

        for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        }

        stdIn.close();

        int max = distances[0] + K - distances[N-1];
        for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        }

        System.out.println(K - max);

    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int min;
        int K = stdIn.nextInt();
        int N = stdIn.nextInt();

        int[] distances = new int[N];

        for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        }

        stdIn.close();

        int max = distances[0] + K - distances[N-1];
        for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        }

        System.out.println(K - max);

    }
} Main public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int min;
        int K = stdIn.nextInt();
        int N = stdIn.nextInt();

        int[] distances = new int[N];

        for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        }

        stdIn.close();

        int max = distances[0] + K - distances[N-1];
        for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        }

        System.out.println(K - max);

    } main {
        Scanner stdIn = new Scanner(System.in);
        int min;
        int K = stdIn.nextInt();
        int N = stdIn.nextInt();

        int[] distances = new int[N];

        for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        }

        stdIn.close();

        int max = distances[0] + K - distances[N-1];
        for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        }

        System.out.println(K - max);

    } Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int min; min int K = stdIn.nextInt(); K stdIn.nextInt() stdIn.nextInt stdIn int N = stdIn.nextInt(); N stdIn.nextInt() stdIn.nextInt stdIn int[] distances = new int[N]; distances new int[N] N for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            distances[i] = stdIn.nextInt();
        } {
            distances[i] = stdIn.nextInt();
        } distances[i] = stdIn.nextInt() distances[i] distances i stdIn.nextInt() stdIn.nextInt stdIn stdIn.close() stdIn.close stdIn int max = distances[0] + K - distances[N-1]; max distances[0] + K - distances[N-1] distances[0] + K distances[0] distances 0 K distances[N-1] distances N-1 N 1 for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        } {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        } if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            } max < distances[i+1] - distances[i] max distances[i+1] - distances[i] distances[i+1] distances i+1 i 1 distances[i] distances i {
                max = distances[i+1] - distances[i];
            } max = distances[i+1] - distances[i] max distances[i+1] - distances[i] distances[i+1] distances i+1 i 1 distances[i] distances i 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 stdIn = new Scanner(System.in);
        int min;
        int K = stdIn.nextInt();
        int N = stdIn.nextInt();

        int[] distances = new int[N];

        for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        }

        stdIn.close();

        int max = distances[0] + K - distances[N-1];
        for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        }

        System.out.println(K - max);

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

        int[] distances = new int[N];

        for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        }

        stdIn.close();

        int max = distances[0] + K - distances[N-1];
        for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        }

        System.out.println(K - max);

    }
} Main
import java.util.*;

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

        int[] distances = new int[N];

        for (int i = 0; i < N; i++) {
            distances[i] = stdIn.nextInt();
        }

        stdIn.close();

        int max = distances[0] + K - distances[N-1];
        for (int i = 0; i < N-1; i++) {
            if (max < distances[i+1] - distances[i]) {
                max = distances[i+1] - distances[i];
            }
        }

        System.out.println(K - max);

    }
}