import java.io.*;
import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner inputs = new Scanner(System.in);
		int total = inputs.nextInt();
		int houses = inputs.nextInt();
		int maxgap = -1;
		int dist = 0;
		int first = inputs.nextInt();
		int prev = first;
		int num=0;
		for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		}
		if(num!=0)
			dist+= first+total-num;
		maxgap = Math.max(maxgap, first+total-num);
		dist-=maxgap;
		System.out.println(dist);
	}

} import java.io.*; io.* java import java.util.*; util.* java public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner inputs = new Scanner(System.in);
		int total = inputs.nextInt();
		int houses = inputs.nextInt();
		int maxgap = -1;
		int dist = 0;
		int first = inputs.nextInt();
		int prev = first;
		int num=0;
		for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		}
		if(num!=0)
			dist+= first+total-num;
		maxgap = Math.max(maxgap, first+total-num);
		dist-=maxgap;
		System.out.println(dist);
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner inputs = new Scanner(System.in);
		int total = inputs.nextInt();
		int houses = inputs.nextInt();
		int maxgap = -1;
		int dist = 0;
		int first = inputs.nextInt();
		int prev = first;
		int num=0;
		for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		}
		if(num!=0)
			dist+= first+total-num;
		maxgap = Math.max(maxgap, first+total-num);
		dist-=maxgap;
		System.out.println(dist);
	} main {
		// TODO Auto-generated method stub
		Scanner inputs = new Scanner(System.in);
		int total = inputs.nextInt();
		int houses = inputs.nextInt();
		int maxgap = -1;
		int dist = 0;
		int first = inputs.nextInt();
		int prev = first;
		int num=0;
		for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		}
		if(num!=0)
			dist+= first+total-num;
		maxgap = Math.max(maxgap, first+total-num);
		dist-=maxgap;
		System.out.println(dist);
	} Scanner inputs = new Scanner(System.in); inputs new Scanner(System.in) int total = inputs.nextInt(); total inputs.nextInt() inputs.nextInt inputs int houses = inputs.nextInt(); houses inputs.nextInt() inputs.nextInt inputs int maxgap = -1; maxgap -1 1 int dist = 0; dist 0 int first = inputs.nextInt(); first inputs.nextInt() inputs.nextInt inputs int prev = first; prev first int num=0; num 0 for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		} int i=1; int i=1; i 1 i<houses i houses i++ i++ i {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		} {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		} num = inputs.nextInt() num inputs.nextInt() inputs.nextInt inputs dist+=num-prev dist num-prev num prev maxgap = Math.max(maxgap, num-prev) maxgap Math.max(maxgap, num-prev) Math.max Math maxgap num-prev num prev prev=num prev num if(num!=0)
			dist+= first+total-num; num!=0 num 0 dist+= first+total-num dist first+total-num first+total first total num maxgap = Math.max(maxgap, first+total-num) maxgap Math.max(maxgap, first+total-num) Math.max Math maxgap first+total-num first+total first total num dist-=maxgap dist maxgap System.out.println(dist) System.out.println System.out System System.out dist String[] args args public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner inputs = new Scanner(System.in);
		int total = inputs.nextInt();
		int houses = inputs.nextInt();
		int maxgap = -1;
		int dist = 0;
		int first = inputs.nextInt();
		int prev = first;
		int num=0;
		for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		}
		if(num!=0)
			dist+= first+total-num;
		maxgap = Math.max(maxgap, first+total-num);
		dist-=maxgap;
		System.out.println(dist);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner inputs = new Scanner(System.in);
		int total = inputs.nextInt();
		int houses = inputs.nextInt();
		int maxgap = -1;
		int dist = 0;
		int first = inputs.nextInt();
		int prev = first;
		int num=0;
		for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		}
		if(num!=0)
			dist+= first+total-num;
		maxgap = Math.max(maxgap, first+total-num);
		dist-=maxgap;
		System.out.println(dist);
	}

} Main
import java.io.*;
import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner inputs = new Scanner(System.in);
		int total = inputs.nextInt();
		int houses = inputs.nextInt();
		int maxgap = -1;
		int dist = 0;
		int first = inputs.nextInt();
		int prev = first;
		int num=0;
		for(int i=1;i<houses;i++) {
			num = inputs.nextInt();
			dist+=num-prev;
			maxgap = Math.max(maxgap, num-prev);
			prev=num;
		}
		if(num!=0)
			dist+= first+total-num;
		maxgap = Math.max(maxgap, first+total-num);
		dist-=maxgap;
		System.out.println(dist);
	}

}
