import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		long k = sc.nextLong();

		int n = sc.nextInt();

		List<Long> list = new ArrayList<Long>();

		for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		}

		list.add(k);

		long totalA = 0;
		long maxLength = 0;
		int point = 0;

		for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		}

		if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		}

		for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		}

		if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		}

		for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		}

		System.out.println(totalA);

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

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		long k = sc.nextLong();

		int n = sc.nextInt();

		List<Long> list = new ArrayList<Long>();

		for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		}

		list.add(k);

		long totalA = 0;
		long maxLength = 0;
		int point = 0;

		for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		}

		if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		}

		for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		}

		if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		}

		for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		}

		System.out.println(totalA);

	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		long k = sc.nextLong();

		int n = sc.nextInt();

		List<Long> list = new ArrayList<Long>();

		for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		}

		list.add(k);

		long totalA = 0;
		long maxLength = 0;
		int point = 0;

		for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		}

		if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		}

		for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		}

		if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		}

		for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		}

		System.out.println(totalA);

	} main {

		Scanner sc = new Scanner(System.in);

		long k = sc.nextLong();

		int n = sc.nextInt();

		List<Long> list = new ArrayList<Long>();

		for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		}

		list.add(k);

		long totalA = 0;
		long maxLength = 0;
		int point = 0;

		for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		}

		if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		}

		for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		}

		if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		}

		for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		}

		System.out.println(totalA);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc List<Long> list = new ArrayList<Long>(); list new ArrayList<Long>() for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		} long i = 0; long i = 0; i 0 i < n i n i++ i++ i {
			list.add(sc.nextLong());
		} {
			list.add(sc.nextLong());
		} list.add(sc.nextLong()) list.add list sc.nextLong() sc.nextLong sc list.add(k) list.add list k long totalA = 0; totalA 0 long maxLength = 0; maxLength 0 int point = 0; point 0 for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		} int num = 0; int num = 0; num 0 num < list.size() - 1 num list.size() - 1 list.size() list.size list 1 num++ num++ num {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		} {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		} Long a = list.get(num); a list.get(num) list.get list num Long b = list.get(num + 1); b list.get(num + 1) list.get list num + 1 num 1 if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			} maxLength <= (b - a) maxLength (b - a) b a {
				maxLength = b - a;
				point = num + 1;
			} maxLength = b - a maxLength b - a b a point = num + 1 point num + 1 num 1 if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		} maxLength <= k - list.get(list.size() - 2) + list.get(0) maxLength k - list.get(list.size() - 2) + list.get(0) k - list.get(list.size() - 2) k list.get(list.size() - 2) list.get list list.size() - 2 list.size() list.size list 2 list.get(0) list.get list 0 {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		} maxLength = k - list.get(list.size() - 2) + list.get(0) maxLength k - list.get(list.size() - 2) + list.get(0) k - list.get(list.size() - 2) k list.get(list.size() - 2) list.get list list.size() - 2 list.size() list.size list 2 list.get(0) list.get list 0 point = 0 point 0 for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		} int num = point; int num = point; num point num < list.size() - 1 num list.size() - 1 list.size() list.size list 1 num++ num++ num {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		} {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		} Long a = list.get(num); a list.get(num) list.get list num Long b = list.get(num + 1); b list.get(num + 1) list.get list num + 1 num 1 if (point == 0 && num + 1 == n) {
				break;
			} point == 0 && num + 1 == n point == 0 point 0 num + 1 == n num + 1 num 1 n {
				break;
			} break; totalA += b - a totalA b - a b a if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		} 0 < totalA && list.get(0) != 0 && 1 < point 0 < totalA && list.get(0) != 0 && 1 < point 0 < totalA 0 totalA list.get(0) != 0 list.get(0) list.get list 0 0 1 < point 1 point {
			totalA += list.get(0);
		} totalA += list.get(0) totalA list.get(0) list.get list 0 for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		} int num = 0; int num = 0; num 0 num < point - 1 num point - 1 point 1 num++ num++ num {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		} {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		} if (num + 1 == n) {
				break;
			} num + 1 == n num + 1 num 1 n {
				break;
			} break; Long a = list.get(num); a list.get(num) list.get list num Long b = list.get(num + 1); b list.get(num + 1) list.get list num + 1 num 1 totalA += b - a totalA b - a b a System.out.println(totalA) System.out.println System.out System System.out totalA String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		long k = sc.nextLong();

		int n = sc.nextInt();

		List<Long> list = new ArrayList<Long>();

		for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		}

		list.add(k);

		long totalA = 0;
		long maxLength = 0;
		int point = 0;

		for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		}

		if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		}

		for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		}

		if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		}

		for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		}

		System.out.println(totalA);

	}
} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		long k = sc.nextLong();

		int n = sc.nextInt();

		List<Long> list = new ArrayList<Long>();

		for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		}

		list.add(k);

		long totalA = 0;
		long maxLength = 0;
		int point = 0;

		for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		}

		if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		}

		for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		}

		if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		}

		for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		}

		System.out.println(totalA);

	}
} Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		long k = sc.nextLong();

		int n = sc.nextInt();

		List<Long> list = new ArrayList<Long>();

		for (long i = 0; i < n; i++) {
			list.add(sc.nextLong());
		}

		list.add(k);

		long totalA = 0;
		long maxLength = 0;
		int point = 0;

		for (int num = 0; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (maxLength <= (b - a)) {
				maxLength = b - a;
				point = num + 1;
			}
		}

		if (maxLength <= k - list.get(list.size() - 2) + list.get(0)) {
			maxLength = k - list.get(list.size() - 2) + list.get(0);
			point = 0;
		}

		for (int num = point; num < list.size() - 1; num++) {
			Long a = list.get(num);
			Long b = list.get(num + 1);

			if (point == 0 && num + 1 == n) {
				break;
			}

			totalA += b - a;
		}

		if (0 < totalA && list.get(0) != 0 && 1 < point) {
			totalA += list.get(0);
		}

		for (int num = 0; num < point - 1; num++) {

			if (num + 1 == n) {
				break;
			}

			Long a = list.get(num);
			Long b = list.get(num + 1);

			totalA += b - a;
		}

		System.out.println(totalA);

	}
}
