import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());

		int[] house = new int[n];
		for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		}
		int[] area = new int[n];
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		}
		Arrays.sort(area);
		int length = 0;
		for (int i = 0; i < n - 1; i++) {
			length += area[i];
		}
		System.out.println(length);

	}

} import java.util.Arrays; Arrays 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 = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());

		int[] house = new int[n];
		for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		}
		int[] area = new int[n];
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		}
		Arrays.sort(area);
		int length = 0;
		for (int i = 0; i < n - 1; i++) {
			length += area[i];
		}
		System.out.println(length);

	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());

		int[] house = new int[n];
		for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		}
		int[] area = new int[n];
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		}
		Arrays.sort(area);
		int length = 0;
		for (int i = 0; i < n - 1; i++) {
			length += area[i];
		}
		System.out.println(length);

	} main {
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());

		int[] house = new int[n];
		for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		}
		int[] area = new int[n];
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		}
		Arrays.sort(area);
		int length = 0;
		for (int i = 0; i < n - 1; i++) {
			length += area[i];
		}
		System.out.println(length);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = Integer.parseInt(sc.next()); k Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int n = Integer.parseInt(sc.next()); n Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] house = new int[n]; house new int[n] n for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			house[i] = Integer.parseInt(sc.next());
		} {
			house[i] = Integer.parseInt(sc.next());
		} house[i] = Integer.parseInt(sc.next()) house[i] house i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] area = new int[n]; area new int[n] n for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		} {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		} if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			} i == n - 1 i n - 1 n 1 {
				area[i] =  house[0]+k-house[i];
			} area[i] =  house[0]+k-house[i] area[i] area i house[0]+k-house[i] house[0]+k house[0] house 0 k house[i] house i {
				area[i] = house[i + 1] - house[i];
			} area[i] = house[i + 1] - house[i] area[i] area i house[i + 1] - house[i] house[i + 1] house i + 1 i 1 house[i] house i Arrays.sort(area) Arrays.sort Arrays area int length = 0; length 0 for (int i = 0; i < n - 1; i++) {
			length += area[i];
		} int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
			length += area[i];
		} {
			length += area[i];
		} length += area[i] length area[i] area i System.out.println(length) System.out.println System.out System System.out length String[] args args public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());

		int[] house = new int[n];
		for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		}
		int[] area = new int[n];
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		}
		Arrays.sort(area);
		int length = 0;
		for (int i = 0; i < n - 1; i++) {
			length += area[i];
		}
		System.out.println(length);

	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());

		int[] house = new int[n];
		for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		}
		int[] area = new int[n];
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		}
		Arrays.sort(area);
		int length = 0;
		for (int i = 0; i < n - 1; i++) {
			length += area[i];
		}
		System.out.println(length);

	}

} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int n = Integer.parseInt(sc.next());

		int[] house = new int[n];
		for (int i = 0; i < n; i++) {
			house[i] = Integer.parseInt(sc.next());
		}
		int[] area = new int[n];
		for (int i = 0; i < n; i++) {
			if (i == n - 1) {
				area[i] =  house[0]+k-house[i];
			} else {
				area[i] = house[i + 1] - house[i];
			}
		}
		Arrays.sort(area);
		int length = 0;
		for (int i = 0; i < n - 1; i++) {
			length += area[i];
		}
		System.out.println(length);

	}

}
