import java.util.Arrays;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		int result = 0;

		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}

		b[0] = k-a[n-1]+a[0];

		for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		}

		Arrays.parallelSort(b);
		for(int i=0;i<n-1;i++) {
			result+=b[i];
		}

		System.out.println(result);


	}

} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		int result = 0;

		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}

		b[0] = k-a[n-1]+a[0];

		for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		}

		Arrays.parallelSort(b);
		for(int i=0;i<n-1;i++) {
			result+=b[i];
		}

		System.out.println(result);


	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		int result = 0;

		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}

		b[0] = k-a[n-1]+a[0];

		for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		}

		Arrays.parallelSort(b);
		for(int i=0;i<n-1;i++) {
			result+=b[i];
		}

		System.out.println(result);


	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		int result = 0;

		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}

		b[0] = k-a[n-1]+a[0];

		for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		}

		Arrays.parallelSort(b);
		for(int i=0;i<n-1;i++) {
			result+=b[i];
		}

		System.out.println(result);


	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan Integer[] a = new Integer[n]; a new Integer[n] n Integer[] b = new Integer[n]; b new Integer[n] n int result = 0; result 0 for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			a[i] = scan.nextInt();
		} {
			a[i] = scan.nextInt();
		} a[i] = scan.nextInt() a[i] a i scan.nextInt() scan.nextInt scan b[0] = k-a[n-1]+a[0] b[0] b 0 k-a[n-1]+a[0] k-a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			b[i] = a[i]-a[i-1];
		} {
			b[i] = a[i]-a[i-1];
		} b[i] = a[i]-a[i-1] b[i] b i a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 Arrays.parallelSort(b) Arrays.parallelSort Arrays b for(int i=0;i<n-1;i++) {
			result+=b[i];
		} int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
			result+=b[i];
		} {
			result+=b[i];
		} result+=b[i] result b[i] b i System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		int result = 0;

		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}

		b[0] = k-a[n-1]+a[0];

		for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		}

		Arrays.parallelSort(b);
		for(int i=0;i<n-1;i++) {
			result+=b[i];
		}

		System.out.println(result);


	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		int result = 0;

		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}

		b[0] = k-a[n-1]+a[0];

		for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		}

		Arrays.parallelSort(b);
		for(int i=0;i<n-1;i++) {
			result+=b[i];
		}

		System.out.println(result);


	}

} Main
import java.util.Arrays;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);

		int k = scan.nextInt();
		int n = scan.nextInt();
		Integer[] a = new Integer[n];
		Integer[] b = new Integer[n];
		int result = 0;

		for(int i=0;i<n;i++) {
			a[i] = scan.nextInt();
		}

		b[0] = k-a[n-1]+a[0];

		for(int i=1;i<n;i++) {
			b[i] = a[i]-a[i-1];
		}

		Arrays.parallelSort(b);
		for(int i=0;i<n-1;i++) {
			result+=b[i];
		}

		System.out.println(result);


	}

}
