
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();
	 int[] a = new int[n];
	 
	 for(int i = 0; i < n; i++ ) {
		 a[i] = scan.nextInt();
	 }
	 
	 int big = 0;
	 int first = 0;
	 int end = 0;
	 int tmp = 0;
	 int exc = 0;
	 int ans = 0;
	 int total = 0;
	 
	 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 }
	 
	 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 }
	 
	 
	 System.out.println(total - big);
	 
	 
}
} 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();
	 int[] a = new int[n];
	 
	 for(int i = 0; i < n; i++ ) {
		 a[i] = scan.nextInt();
	 }
	 
	 int big = 0;
	 int first = 0;
	 int end = 0;
	 int tmp = 0;
	 int exc = 0;
	 int ans = 0;
	 int total = 0;
	 
	 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 }
	 
	 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 }
	 
	 
	 System.out.println(total - big);
	 
	 
}
} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
	 Scanner scan = new Scanner(System.in);
	 
	 int k = scan.nextInt(); 
	 int n = scan.nextInt();
	 int[] a = new int[n];
	 
	 for(int i = 0; i < n; i++ ) {
		 a[i] = scan.nextInt();
	 }
	 
	 int big = 0;
	 int first = 0;
	 int end = 0;
	 int tmp = 0;
	 int exc = 0;
	 int ans = 0;
	 int total = 0;
	 
	 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 }
	 
	 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 }
	 
	 
	 System.out.println(total - big);
	 
	 
} main {
		// TODO 自動生成されたメソッド・スタブ
		
	 Scanner scan = new Scanner(System.in);
	 
	 int k = scan.nextInt(); 
	 int n = scan.nextInt();
	 int[] a = new int[n];
	 
	 for(int i = 0; i < n; i++ ) {
		 a[i] = scan.nextInt();
	 }
	 
	 int big = 0;
	 int first = 0;
	 int end = 0;
	 int tmp = 0;
	 int exc = 0;
	 int ans = 0;
	 int total = 0;
	 
	 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 }
	 
	 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 }
	 
	 
	 System.out.println(total - big);
	 
	 
} 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 int[] a = new int[n]; a new int[n] n 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 int big = 0; big 0 int first = 0; first 0 int end = 0; end 0 int tmp = 0; tmp 0 int exc = 0; exc 0 int ans = 0; ans 0 int total = 0; total 0 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 } {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 } if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 } i < n - 1 i n - 1 n 1 {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 } tmp = a[i+1] - a[i] tmp a[i+1] - a[i] a[i+1] a i+1 i 1 a[i] a i total += tmp total tmp if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 } i == n - 1 i n - 1 n 1 {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 } exc =  k - a[n-1] + a[0] exc k - a[n-1] + a[0] k - a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 total += exc total exc if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 } big < tmp big tmp {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 } big = tmp big tmp first = i first i end = i + 1 end i + 1 i 1 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 } big < exc big exc {
			 big = exc;
			 first = 0;
			 end = n-1;
		 } big = exc big exc first = 0 first 0 end = n-1 end n-1 n 1 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 } first == 0 first 0 {
		 ans = a[end] - a[first];
	 } ans = a[end] - a[first] ans a[end] - a[first] a[end] a end a[first] a first {
		 ans = k - a[end] + a[first];
	 } ans = k - a[end] + a[first] ans k - a[end] + a[first] k - a[end] k a[end] a end a[first] a first System.out.println(total - big) System.out.println System.out System System.out total - big total big 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();
	 int[] a = new int[n];
	 
	 for(int i = 0; i < n; i++ ) {
		 a[i] = scan.nextInt();
	 }
	 
	 int big = 0;
	 int first = 0;
	 int end = 0;
	 int tmp = 0;
	 int exc = 0;
	 int ans = 0;
	 int total = 0;
	 
	 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 }
	 
	 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 }
	 
	 
	 System.out.println(total - big);
	 
	 
}
} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
	 Scanner scan = new Scanner(System.in);
	 
	 int k = scan.nextInt(); 
	 int n = scan.nextInt();
	 int[] a = new int[n];
	 
	 for(int i = 0; i < n; i++ ) {
		 a[i] = scan.nextInt();
	 }
	 
	 int big = 0;
	 int first = 0;
	 int end = 0;
	 int tmp = 0;
	 int exc = 0;
	 int ans = 0;
	 int total = 0;
	 
	 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 }
	 
	 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 }
	 
	 
	 System.out.println(total - big);
	 
	 
}
} Main

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();
	 int[] a = new int[n];
	 
	 for(int i = 0; i < n; i++ ) {
		 a[i] = scan.nextInt();
	 }
	 
	 int big = 0;
	 int first = 0;
	 int end = 0;
	 int tmp = 0;
	 int exc = 0;
	 int ans = 0;
	 int total = 0;
	 
	 for(int i = 0; i < n; i++) {
		 
		 
		 if(i < n - 1 ) {
			 tmp = a[i+1] - a[i];
			 total += tmp;
		 }
		 else if(i == n - 1) {
			 exc =  k - a[n-1] + a[0];
			 total += exc;
		 }
		 
		 
		 if(big < tmp ) {
			 big = tmp;
			 first = i;
			 end = i + 1;
		 }
		 if(big < exc ) {
			 big = exc;
			 first = 0;
			 end = n-1;
		 }
	 }
	 
	 if(first == 0) {
		 ans = a[end] - a[first];
	 }
	 else {
		 ans = k - a[end] + a[first];
	 }
	 
	 
	 System.out.println(total - big);
	 
	 
}
}
