import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[]=new int[n],ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++){a[i]=scan.nextInt();}
		scan.close();

		for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		}
		System.out.println(ans);
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[]=new int[n],ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++){a[i]=scan.nextInt();}
		scan.close();

		for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		}
		System.out.println(ans);
	}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[]=new int[n],ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++){a[i]=scan.nextInt();}
		scan.close();

		for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		}
		System.out.println(ans);
	} main {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[]=new int[n],ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++){a[i]=scan.nextInt();}
		scan.close();

		for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		}
		System.out.println(ans);
	} 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 ans=Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE 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 scan.close() scan.close scan for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		} {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		} int right = a[n-1]-a[i]; right a[n-1]-a[i] a[n-1] a n-1 n 1 a[i] a i if(i!=0)right+=a[i-1]+k-a[n-1]; i!=0 i 0 right+=a[i-1]+k-a[n-1] right a[i-1]+k-a[n-1] a[i-1]+k a[i-1] a i-1 i 1 k a[n-1] a n-1 n 1 int left = a[i]; left a[i] a i if(i!=n-1)left+=k-a[i+1]; i!=n-1 i n-1 n 1 left+=k-a[i+1] left k-a[i+1] k a[i+1] a i+1 i 1 if(ans>right)ans=right; ans>right ans right ans=right ans right if(ans>left)ans=left; ans>left ans left ans=left ans left 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 scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[]=new int[n],ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++){a[i]=scan.nextInt();}
		scan.close();

		for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		}
		System.out.println(ans);
	}
} public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[]=new int[n],ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++){a[i]=scan.nextInt();}
		scan.close();

		for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		}
		System.out.println(ans);
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int k = scan.nextInt();
		int n = scan.nextInt();
		int a[]=new int[n],ans=Integer.MAX_VALUE;
		for(int i=0;i<n;i++){a[i]=scan.nextInt();}
		scan.close();

		for(int i=0;i<n;i++) {
			int right = a[n-1]-a[i];
			if(i!=0)right+=a[i-1]+k-a[n-1];
			int left = a[i];
			if(i!=n-1)left+=k-a[i+1];

			if(ans>right)ans=right;
			if(ans>left)ans=left;
		}
		System.out.println(ans);
	}
}