import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt(), n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt();
        int max = k - nums[n - 1] + nums[0];
        for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max);
        System.out.println(k - max);
    }

} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt(), n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt();
        int max = k - nums[n - 1] + nums[0];
        for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max);
        System.out.println(k - max);
    }

} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt(), n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt();
        int max = k - nums[n - 1] + nums[0];
        for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max);
        System.out.println(k - max);
    } main {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt(), n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt();
        int max = k - nums[n - 1] + nums[0];
        for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max);
        System.out.println(k - max);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int k = scanner.nextInt() k scanner.nextInt() scanner.nextInt scanner n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] nums = new int[n]; nums new int[n] n for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt(); int i = 0; int i = 0; i 0 i < n i n i ++ i ++ i nums[i] = scanner.nextInt(); nums[i] = scanner.nextInt() nums[i] nums i scanner.nextInt() scanner.nextInt scanner int max = k - nums[n - 1] + nums[0]; max k - nums[n - 1] + nums[0] k - nums[n - 1] k nums[n - 1] nums n - 1 n 1 nums[0] nums 0 for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max); int i = 1; int i = 1; i 1 i < n i n i ++ i ++ i max = Math.max(nums[i] - nums[i - 1], max); max = Math.max(nums[i] - nums[i - 1], max) max Math.max(nums[i] - nums[i - 1], max) Math.max Math nums[i] - nums[i - 1] nums[i] nums i nums[i - 1] nums i - 1 i 1 max 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 scanner = new Scanner(System.in);
        int k = scanner.nextInt(), n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt();
        int max = k - nums[n - 1] + nums[0];
        for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max);
        System.out.println(k - max);
    }

} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt(), n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt();
        int max = k - nums[n - 1] + nums[0];
        for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max);
        System.out.println(k - max);
    }

} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt(), n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i ++)
            nums[i] = scanner.nextInt();
        int max = k - nums[n - 1] + nums[0];
        for (int i = 1; i < n; i ++)
            max = Math.max(nums[i] - nums[i - 1], max);
        System.out.println(k - max);
    }

}