import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int K = stdIn.nextInt();
		int N = stdIn.nextInt();
		
		ArrayList<Integer> A = new ArrayList<Integer>();
		for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		}
		
		Collections.sort(A);
		int distance = 0;
		for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		}
		
		int longest = A.get(0) + K - A.get(A.size() - 1);
		
		for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		}
		
		System.out.println(distance - longest);
	}
} import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int K = stdIn.nextInt();
		int N = stdIn.nextInt();
		
		ArrayList<Integer> A = new ArrayList<Integer>();
		for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		}
		
		Collections.sort(A);
		int distance = 0;
		for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		}
		
		int longest = A.get(0) + K - A.get(A.size() - 1);
		
		for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		}
		
		System.out.println(distance - longest);
	}
} Main public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int K = stdIn.nextInt();
		int N = stdIn.nextInt();
		
		ArrayList<Integer> A = new ArrayList<Integer>();
		for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		}
		
		Collections.sort(A);
		int distance = 0;
		for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		}
		
		int longest = A.get(0) + K - A.get(A.size() - 1);
		
		for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		}
		
		System.out.println(distance - longest);
	} main {
		Scanner stdIn = new Scanner(System.in);
		int K = stdIn.nextInt();
		int N = stdIn.nextInt();
		
		ArrayList<Integer> A = new ArrayList<Integer>();
		for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		}
		
		Collections.sort(A);
		int distance = 0;
		for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		}
		
		int longest = A.get(0) + K - A.get(A.size() - 1);
		
		for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		}
		
		System.out.println(distance - longest);
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int K = stdIn.nextInt(); K stdIn.nextInt() stdIn.nextInt stdIn int N = stdIn.nextInt(); N stdIn.nextInt() stdIn.nextInt stdIn ArrayList<Integer> A = new ArrayList<Integer>(); A new ArrayList<Integer>() for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		} int i = 0; int i = 0; i 0 i < N i N i ++ i ++ i {
			A.add(stdIn.nextInt());
		} {
			A.add(stdIn.nextInt());
		} A.add(stdIn.nextInt()) A.add A stdIn.nextInt() stdIn.nextInt stdIn Collections.sort(A) Collections.sort Collections A int distance = 0; distance 0 for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		} int i = 0; int i = 0; i 0 i < A.size() i A.size() A.size A i ++ i ++ i {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		} {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		} if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			} i == 0 i 0 {
				distance += A.get(i);
			} distance += A.get(i) distance A.get(i) A.get A i {
				distance += A.get(i) - A.get(i - 1);
			} distance += A.get(i) - A.get(i - 1) distance A.get(i) - A.get(i - 1) A.get(i) A.get A i A.get(i - 1) A.get A i - 1 i 1 if(i == A.size() - 1) {
				distance += K - A.get(i);
			} i == A.size() - 1 i A.size() - 1 A.size() A.size A 1 {
				distance += K - A.get(i);
			} distance += K - A.get(i) distance K - A.get(i) K A.get(i) A.get A i int longest = A.get(0) + K - A.get(A.size() - 1); longest A.get(0) + K - A.get(A.size() - 1) A.get(0) + K A.get(0) A.get A 0 K A.get(A.size() - 1) A.get A A.size() - 1 A.size() A.size A 1 for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		} int i = 1; int i = 1; i 1 i < A.size() i A.size() A.size A i ++ i ++ i {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		} {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		} longest = Math.max(longest, A.get(i) - A.get(i - 1)) longest Math.max(longest, A.get(i) - A.get(i - 1)) Math.max Math longest A.get(i) - A.get(i - 1) A.get(i) A.get A i A.get(i - 1) A.get A i - 1 i 1 System.out.println(distance - longest) System.out.println System.out System System.out distance - longest distance longest String[] args args public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int K = stdIn.nextInt();
		int N = stdIn.nextInt();
		
		ArrayList<Integer> A = new ArrayList<Integer>();
		for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		}
		
		Collections.sort(A);
		int distance = 0;
		for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		}
		
		int longest = A.get(0) + K - A.get(A.size() - 1);
		
		for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		}
		
		System.out.println(distance - longest);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int K = stdIn.nextInt();
		int N = stdIn.nextInt();
		
		ArrayList<Integer> A = new ArrayList<Integer>();
		for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		}
		
		Collections.sort(A);
		int distance = 0;
		for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		}
		
		int longest = A.get(0) + K - A.get(A.size() - 1);
		
		for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		}
		
		System.out.println(distance - longest);
	}
} Main
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		int K = stdIn.nextInt();
		int N = stdIn.nextInt();
		
		ArrayList<Integer> A = new ArrayList<Integer>();
		for(int i = 0; i < N; i ++) {
			A.add(stdIn.nextInt());
		}
		
		Collections.sort(A);
		int distance = 0;
		for(int i = 0; i < A.size(); i ++) {
			if(i == 0) {
				distance += A.get(i);
			}else {
				distance += A.get(i) - A.get(i - 1);
			}
			
			if(i == A.size() - 1) {
				distance += K - A.get(i);
			}
		}
		
		int longest = A.get(0) + K - A.get(A.size() - 1);
		
		for(int i = 1; i < A.size(); i ++) {
			longest = Math.max(longest, A.get(i) - A.get(i - 1));
		}
		
		System.out.println(distance - longest);
	}
}