import java.util.*;
import java.math.RoundingMode;
import java.math.BigDecimal;
 
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int a[] = new int[n];
		int d[] = new int[n];
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		}
		d[n-1] = k-a[n-1]+a[0];
		Arrays.sort(d);
		for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		}
		System.out.println(ans);
	}
} import java.util.*; util.* java import java.math.RoundingMode; RoundingMode java.math import java.math.BigDecimal; BigDecimal java.math public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int a[] = new int[n];
		int d[] = new int[n];
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		}
		d[n-1] = k-a[n-1]+a[0];
		Arrays.sort(d);
		for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		}
		System.out.println(ans);
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int a[] = new int[n];
		int d[] = new int[n];
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		}
		d[n-1] = k-a[n-1]+a[0];
		Arrays.sort(d);
		for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int a[] = new int[n];
		int d[] = new int[n];
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		}
		d[n-1] = k-a[n-1]+a[0];
		Arrays.sort(d);
		for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int a[] = new int[n]; a new int[n] n int d[] = new int[n]; d new int[n] n int ans = 0; ans 0 for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a[i] = sc.nextInt();
		} {
			a[i] = sc.nextInt();
		} a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		} int i = 0; int i = 0; i 0 i < n-1 i n-1 n 1 i++ i++ i {
			d[i] = a[i+1]-a[i];
		} {
			d[i] = a[i+1]-a[i];
		} d[i] = a[i+1]-a[i] d[i] d i a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i d[n-1] = k-a[n-1]+a[0] d[n-1] d n-1 n 1 k-a[n-1]+a[0] k-a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 Arrays.sort(d) Arrays.sort Arrays d for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		} int i = 0; int i = 0; i 0 i < n-1 i n-1 n 1 i++ i++ i {
			ans+=d[i];
		} {
			ans+=d[i];
		} ans+=d[i] ans d[i] d i System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int a[] = new int[n];
		int d[] = new int[n];
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		}
		d[n-1] = k-a[n-1]+a[0];
		Arrays.sort(d);
		for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		}
		System.out.println(ans);
	}
} public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int a[] = new int[n];
		int d[] = new int[n];
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		}
		d[n-1] = k-a[n-1]+a[0];
		Arrays.sort(d);
		for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		}
		System.out.println(ans);
	}
} Main
import java.util.*;
import java.math.RoundingMode;
import java.math.BigDecimal;
 
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		int a[] = new int[n];
		int d[] = new int[n];
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		for(int i = 0; i < n-1; i++) {
			d[i] = a[i+1]-a[i];
		}
		d[n-1] = k-a[n-1]+a[0];
		Arrays.sort(d);
		for(int i = 0; i < n-1; i++) {
			ans+=d[i];
		}
		System.out.println(ans);
	}
}