

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws java.lang.Exception {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));

		String input[] = sc.nextLine().split(" ");
		int k = Integer.parseInt(input[0]);
		int n = Integer.parseInt(input[1]);
		int ar[] = new int[n];
		String input2[] = sc.nextLine().split(" ");
		for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]);
		int tour[] = new int[(2 * n) - 1];
		for(int i = 0; i < n; i++)
			tour[i] = ar[i];
		
		for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k;
		
		/*for(int i = 0; i < tour.length; i++)
			System.out.print(tour[i] + " ");
		
		System.out.println();*/
		int distance = 0;
		for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]);
		//System.out.println(distance);
		int mindistance = distance;
		
		for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		}
		System.out.println(mindistance);
		out.close();
	}

	public static PrintWriter out;

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

} import java.io.BufferedOutputStream; BufferedOutputStream java.io import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.PrintWriter; PrintWriter java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {

	public static void main(String[] args) throws java.lang.Exception {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));

		String input[] = sc.nextLine().split(" ");
		int k = Integer.parseInt(input[0]);
		int n = Integer.parseInt(input[1]);
		int ar[] = new int[n];
		String input2[] = sc.nextLine().split(" ");
		for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]);
		int tour[] = new int[(2 * n) - 1];
		for(int i = 0; i < n; i++)
			tour[i] = ar[i];
		
		for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k;
		
		/*for(int i = 0; i < tour.length; i++)
			System.out.print(tour[i] + " ");
		
		System.out.println();*/
		int distance = 0;
		for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]);
		//System.out.println(distance);
		int mindistance = distance;
		
		for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		}
		System.out.println(mindistance);
		out.close();
	}

	public static PrintWriter out;

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

} Main public static PrintWriter out; out public static void main(String[] args) throws java.lang.Exception {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));

		String input[] = sc.nextLine().split(" ");
		int k = Integer.parseInt(input[0]);
		int n = Integer.parseInt(input[1]);
		int ar[] = new int[n];
		String input2[] = sc.nextLine().split(" ");
		for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]);
		int tour[] = new int[(2 * n) - 1];
		for(int i = 0; i < n; i++)
			tour[i] = ar[i];
		
		for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k;
		
		/*for(int i = 0; i < tour.length; i++)
			System.out.print(tour[i] + " ");
		
		System.out.println();*/
		int distance = 0;
		for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]);
		//System.out.println(distance);
		int mindistance = distance;
		
		for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		}
		System.out.println(mindistance);
		out.close();
	} main {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));

		String input[] = sc.nextLine().split(" ");
		int k = Integer.parseInt(input[0]);
		int n = Integer.parseInt(input[1]);
		int ar[] = new int[n];
		String input2[] = sc.nextLine().split(" ");
		for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]);
		int tour[] = new int[(2 * n) - 1];
		for(int i = 0; i < n; i++)
			tour[i] = ar[i];
		
		for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k;
		
		/*for(int i = 0; i < tour.length; i++)
			System.out.print(tour[i] + " ");
		
		System.out.println();*/
		int distance = 0;
		for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]);
		//System.out.println(distance);
		int mindistance = distance;
		
		for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		}
		System.out.println(mindistance);
		out.close();
	} MyScanner sc = new MyScanner(); sc new MyScanner() out = new PrintWriter(new BufferedOutputStream(System.out)) out new PrintWriter(new BufferedOutputStream(System.out)) String input[] = sc.nextLine().split(" "); input sc.nextLine().split(" ") sc.nextLine().split sc.nextLine() sc.nextLine sc " " int k = Integer.parseInt(input[0]); k Integer.parseInt(input[0]) Integer.parseInt Integer input[0] input 0 int n = Integer.parseInt(input[1]); n Integer.parseInt(input[1]) Integer.parseInt Integer input[1] input 1 int ar[] = new int[n]; ar new int[n] n String input2[] = sc.nextLine().split(" "); input2 sc.nextLine().split(" ") sc.nextLine().split sc.nextLine() sc.nextLine sc " " for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]); int i = 0; int i = 0; i 0 i < n i n i++ i++ i ar[i] = Integer.parseInt(input2[i]); ar[i] = Integer.parseInt(input2[i]) ar[i] ar i Integer.parseInt(input2[i]) Integer.parseInt Integer input2[i] input2 i int tour[] = new int[(2 * n) - 1]; tour new int[(2 * n) - 1] (2 * n) - 1 (2 * n) 2 n 1 for(int i = 0; i < n; i++)
			tour[i] = ar[i]; int i = 0; int i = 0; i 0 i < n i n i++ i++ i tour[i] = ar[i]; tour[i] = ar[i] tour[i] tour i ar[i] ar i for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k; int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i tour[i + n] = ar[i] + k; tour[i + n] = ar[i] + k tour[i + n] tour i + n i n ar[i] + k ar[i] ar i k int distance = 0; distance 0 for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]); int i = 1; int i = 1; i 1 i < n i n i++ i++ i distance = distance + (tour[i] - tour[i - 1]); distance = distance + (tour[i] - tour[i - 1]) distance distance + (tour[i] - tour[i - 1]) distance (tour[i] - tour[i - 1]) tour[i] tour i tour[i - 1] tour i - 1 i 1 int mindistance = distance; mindistance distance for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		} {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		} distance = distance - Math.abs(ar[i] - ar[i - 1]) distance distance - Math.abs(ar[i] - ar[i - 1]) distance Math.abs(ar[i] - ar[i - 1]) Math.abs Math ar[i] - ar[i - 1] ar[i] ar i ar[i - 1] ar i - 1 i 1 distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2])) distance distance + Math.abs((tour[i + n -1] - tour[i + n -2])) distance Math.abs((tour[i + n -1] - tour[i + n -2])) Math.abs Math (tour[i + n -1] - tour[i + n -2]) tour[i + n -1] tour i + n -1 i + n i n 1 tour[i + n -2] tour i + n -2 i + n i n 2 mindistance = Math.min(distance, mindistance) mindistance Math.min(distance, mindistance) Math.min Math distance mindistance System.out.println(mindistance) System.out.println System.out System System.out mindistance out.close() out.close out String[] args args public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	} MyScanner BufferedReader br; br StringTokenizer st; st public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		} MyScanner {
			br = new BufferedReader(new InputStreamReader(System.in));
		} br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		} next {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		} while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} IOException e {
					e.printStackTrace();
				} e.printStackTrace() e.printStackTrace e {
					st = new StringTokenizer(br.readLine());
				} st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
			return Integer.parseInt(next());
		} nextInt {
			return Integer.parseInt(next());
		} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
			return Long.parseLong(next());
		} nextLong {
			return Long.parseLong(next());
		} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble() {
			return Double.parseDouble(next());
		} nextDouble {
			return Double.parseDouble(next());
		} return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		} nextLine {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		} String str = ""; str "" try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} IOException e {
				e.printStackTrace();
			} e.printStackTrace() e.printStackTrace e {
				str = br.readLine();
			} str = br.readLine() str br.readLine() br.readLine br return str; str public class Main {

	public static void main(String[] args) throws java.lang.Exception {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));

		String input[] = sc.nextLine().split(" ");
		int k = Integer.parseInt(input[0]);
		int n = Integer.parseInt(input[1]);
		int ar[] = new int[n];
		String input2[] = sc.nextLine().split(" ");
		for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]);
		int tour[] = new int[(2 * n) - 1];
		for(int i = 0; i < n; i++)
			tour[i] = ar[i];
		
		for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k;
		
		/*for(int i = 0; i < tour.length; i++)
			System.out.print(tour[i] + " ");
		
		System.out.println();*/
		int distance = 0;
		for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]);
		//System.out.println(distance);
		int mindistance = distance;
		
		for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		}
		System.out.println(mindistance);
		out.close();
	}

	public static PrintWriter out;

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

} public class Main {

	public static void main(String[] args) throws java.lang.Exception {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));

		String input[] = sc.nextLine().split(" ");
		int k = Integer.parseInt(input[0]);
		int n = Integer.parseInt(input[1]);
		int ar[] = new int[n];
		String input2[] = sc.nextLine().split(" ");
		for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]);
		int tour[] = new int[(2 * n) - 1];
		for(int i = 0; i < n; i++)
			tour[i] = ar[i];
		
		for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k;
		
		/*for(int i = 0; i < tour.length; i++)
			System.out.print(tour[i] + " ");
		
		System.out.println();*/
		int distance = 0;
		for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]);
		//System.out.println(distance);
		int mindistance = distance;
		
		for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		}
		System.out.println(mindistance);
		out.close();
	}

	public static PrintWriter out;

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

} Main


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws java.lang.Exception {
		MyScanner sc = new MyScanner();
		out = new PrintWriter(new BufferedOutputStream(System.out));

		String input[] = sc.nextLine().split(" ");
		int k = Integer.parseInt(input[0]);
		int n = Integer.parseInt(input[1]);
		int ar[] = new int[n];
		String input2[] = sc.nextLine().split(" ");
		for(int i = 0; i < n; i++)
			ar[i] = Integer.parseInt(input2[i]);
		int tour[] = new int[(2 * n) - 1];
		for(int i = 0; i < n; i++)
			tour[i] = ar[i];
		
		for(int i = 0; i < n - 1; i++)
			tour[i + n] = ar[i] + k;
		
		/*for(int i = 0; i < tour.length; i++)
			System.out.print(tour[i] + " ");
		
		System.out.println();*/
		int distance = 0;
		for(int i = 1; i < n; i++)
			distance = distance + (tour[i] - tour[i - 1]);
		//System.out.println(distance);
		int mindistance = distance;
		
		for(int i = 1; i < n; i++) {
			distance = distance - Math.abs(ar[i] - ar[i - 1]);
			distance = distance + Math.abs((tour[i + n -1] - tour[i + n -2]));
			//System.out.println(ar[i]+" : " +distance+ " ");
			mindistance = Math.min(distance, mindistance);
		}
		System.out.println(mindistance);
		out.close();
	}

	public static PrintWriter out;

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

}
