import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int circumference = sc.nextInt(); //円周
		int numOfHouses = sc.nextInt();
		int[] distance = new int[numOfHouses];
		for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		}
		
		int longestDistance = 0;
		for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		}
		
		System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance));
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int circumference = sc.nextInt(); //円周
		int numOfHouses = sc.nextInt();
		int[] distance = new int[numOfHouses];
		for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		}
		
		int longestDistance = 0;
		for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		}
		
		System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance));
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int circumference = sc.nextInt(); //円周
		int numOfHouses = sc.nextInt();
		int[] distance = new int[numOfHouses];
		for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		}
		
		int longestDistance = 0;
		for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		}
		
		System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance));
	} main {
		Scanner sc = new Scanner(System.in);
		int circumference = sc.nextInt(); //円周
		int numOfHouses = sc.nextInt();
		int[] distance = new int[numOfHouses];
		for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		}
		
		int longestDistance = 0;
		for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		}
		
		System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance));
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int circumference = sc.nextInt(); circumference sc.nextInt() sc.nextInt sc int numOfHouses = sc.nextInt(); numOfHouses sc.nextInt() sc.nextInt sc int[] distance = new int[numOfHouses]; distance new int[numOfHouses] numOfHouses for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < numOfHouses i numOfHouses i++ i++ i {
			distance[i] = sc.nextInt();
		} {
			distance[i] = sc.nextInt();
		} distance[i] = sc.nextInt() distance[i] distance i sc.nextInt() sc.nextInt sc int longestDistance = 0; longestDistance 0 for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		} int i = 2; int i = 2; i 2 i < numOfHouses i numOfHouses i++ i++ i {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		} {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		} int tmp = distance[i] - distance[i - 1]; tmp distance[i] - distance[i - 1] distance[i] distance i distance[i - 1] distance i - 1 i 1 if (tmp > longestDistance) {
				longestDistance = tmp;
			} tmp > longestDistance tmp longestDistance {
				longestDistance = tmp;
			} longestDistance = tmp longestDistance tmp System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance)) System.out.println System.out System System.out Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance) Math.min Math distance[numOfHouses - 1] - distance[0] distance[numOfHouses - 1] distance numOfHouses - 1 numOfHouses 1 distance[0] distance 0 circumference - longestDistance circumference longestDistance String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int circumference = sc.nextInt(); //円周
		int numOfHouses = sc.nextInt();
		int[] distance = new int[numOfHouses];
		for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		}
		
		int longestDistance = 0;
		for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		}
		
		System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance));
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int circumference = sc.nextInt(); //円周
		int numOfHouses = sc.nextInt();
		int[] distance = new int[numOfHouses];
		for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		}
		
		int longestDistance = 0;
		for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		}
		
		System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance));
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int circumference = sc.nextInt(); //円周
		int numOfHouses = sc.nextInt();
		int[] distance = new int[numOfHouses];
		for (int i = 0; i < numOfHouses; i++) {
			distance[i] = sc.nextInt();
		}
		
		int longestDistance = 0;
		for (int i = 2; i < numOfHouses; i++) {
			int tmp = distance[i] - distance[i - 1];
			if (tmp > longestDistance) {
				longestDistance = tmp;
			}
		}
		
		System.out.println(Math.min(distance[numOfHouses - 1] - distance[0], circumference - longestDistance));
	}
}