import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] a = new int[n];
		int[] arr = new int[n];
		
		long min = 0;
		
		for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		}
		if (a[0] > 0) {
			arr[n - 1] = a[0];
		}
		arr[n - 1] += k - a[n-1];
		
		int i = 1;
		int e = n - 1;
		long nextMin = min;
		while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		}
		System.out.println(min);
	}

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] a = new int[n];
		int[] arr = new int[n];
		
		long min = 0;
		
		for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		}
		if (a[0] > 0) {
			arr[n - 1] = a[0];
		}
		arr[n - 1] += k - a[n-1];
		
		int i = 1;
		int e = n - 1;
		long nextMin = min;
		while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		}
		System.out.println(min);
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] a = new int[n];
		int[] arr = new int[n];
		
		long min = 0;
		
		for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		}
		if (a[0] > 0) {
			arr[n - 1] = a[0];
		}
		arr[n - 1] += k - a[n-1];
		
		int i = 1;
		int e = n - 1;
		long nextMin = min;
		while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		}
		System.out.println(min);
	} main {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] a = new int[n];
		int[] arr = new int[n];
		
		long min = 0;
		
		for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		}
		if (a[0] > 0) {
			arr[n - 1] = a[0];
		}
		arr[n - 1] += k - a[n-1];
		
		int i = 1;
		int e = n - 1;
		long nextMin = min;
		while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		}
		System.out.println(min);
	} 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 int[] arr = new int[n]; arr new int[n] n long min = 0; min 0 for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		} {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		} a[i] = scan.nextInt() a[i] a i scan.nextInt() scan.nextInt scan if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			} i > 0 i 0 {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			} arr[i-1] = a[i] - a[i - 1] arr[i-1] arr i-1 i 1 a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 min += arr[i-1] min arr[i-1] arr i-1 i 1 if (a[0] > 0) {
			arr[n - 1] = a[0];
		} a[0] > 0 a[0] a 0 0 {
			arr[n - 1] = a[0];
		} arr[n - 1] = a[0] arr[n - 1] arr n - 1 n 1 a[0] a 0 arr[n - 1] += k - a[n-1] arr[n - 1] arr n - 1 n 1 k - a[n-1] k a[n-1] a n-1 n 1 int i = 1; i 1 int e = n - 1; e n - 1 n 1 long nextMin = min; nextMin min while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		} i < n i n {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		} nextMin = nextMin - arr[i - 1] + arr[e++] nextMin nextMin - arr[i - 1] + arr[e++] nextMin - arr[i - 1] nextMin arr[i - 1] arr i - 1 i 1 arr[e++] arr e++ e min = Math.min(min, nextMin) min Math.min(min, nextMin) Math.min Math min nextMin if (e >= n) {
				e = 0;
			} e >= n e n {
				e = 0;
			} e = 0 e 0 i++ i System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] a = new int[n];
		int[] arr = new int[n];
		
		long min = 0;
		
		for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		}
		if (a[0] > 0) {
			arr[n - 1] = a[0];
		}
		arr[n - 1] += k - a[n-1];
		
		int i = 1;
		int e = n - 1;
		long nextMin = min;
		while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		}
		System.out.println(min);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] a = new int[n];
		int[] arr = new int[n];
		
		long min = 0;
		
		for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		}
		if (a[0] > 0) {
			arr[n - 1] = a[0];
		}
		arr[n - 1] += k - a[n-1];
		
		int i = 1;
		int e = n - 1;
		long nextMin = min;
		while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		}
		System.out.println(min);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int[] a = new int[n];
		int[] arr = new int[n];
		
		long min = 0;
		
		for (int i = 0; i < n; i++) {
			a[i] = scan.nextInt();
			if (i > 0) {
				arr[i-1] = a[i] - a[i - 1];
				min += arr[i-1]; 
			}
		}
		if (a[0] > 0) {
			arr[n - 1] = a[0];
		}
		arr[n - 1] += k - a[n-1];
		
		int i = 1;
		int e = n - 1;
		long nextMin = min;
		while (i < n) {
			
			nextMin = nextMin - arr[i - 1] + arr[e++];
			min = Math.min(min, nextMin);
			
			if (e >= n) {
				e = 0;
			}
			i++;
		}
		System.out.println(min);
	}

}
