import java.util.*;
 
public class Main {
	public static void main (String[] args) {
		Scanner in = new Scanner(System.in);
		
		int ollm = in.nextInt();
		int hous = in.nextInt();
		int fast = in.nextInt();
		int memo = fast;
		int second =  0;
		int last = 0;
		int now =  0;
		long sum = 0;
		long maxm = 0;
		
		for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		}
		
		last = ollm-(second - memo);
		sum = sum + last;
		if(maxm< last){
		    maxm = last;
		}
		
		System.out.println(sum - maxm);
      
	}
} import java.util.*; util.* java public class Main {
	public static void main (String[] args) {
		Scanner in = new Scanner(System.in);
		
		int ollm = in.nextInt();
		int hous = in.nextInt();
		int fast = in.nextInt();
		int memo = fast;
		int second =  0;
		int last = 0;
		int now =  0;
		long sum = 0;
		long maxm = 0;
		
		for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		}
		
		last = ollm-(second - memo);
		sum = sum + last;
		if(maxm< last){
		    maxm = last;
		}
		
		System.out.println(sum - maxm);
      
	}
} Main public static void main (String[] args) {
		Scanner in = new Scanner(System.in);
		
		int ollm = in.nextInt();
		int hous = in.nextInt();
		int fast = in.nextInt();
		int memo = fast;
		int second =  0;
		int last = 0;
		int now =  0;
		long sum = 0;
		long maxm = 0;
		
		for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		}
		
		last = ollm-(second - memo);
		sum = sum + last;
		if(maxm< last){
		    maxm = last;
		}
		
		System.out.println(sum - maxm);
      
	} main {
		Scanner in = new Scanner(System.in);
		
		int ollm = in.nextInt();
		int hous = in.nextInt();
		int fast = in.nextInt();
		int memo = fast;
		int second =  0;
		int last = 0;
		int now =  0;
		long sum = 0;
		long maxm = 0;
		
		for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		}
		
		last = ollm-(second - memo);
		sum = sum + last;
		if(maxm< last){
		    maxm = last;
		}
		
		System.out.println(sum - maxm);
      
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int ollm = in.nextInt(); ollm in.nextInt() in.nextInt in int hous = in.nextInt(); hous in.nextInt() in.nextInt in int fast = in.nextInt(); fast in.nextInt() in.nextInt in int memo = fast; memo fast int second =  0; second 0 int last = 0; last 0 int now =  0; now 0 long sum = 0; sum 0 long maxm = 0; maxm 0 for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		} int i = 0; int i = 0; i 0 i < hous -1 i hous -1 hous 1 i++ i++ i {
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		} {
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		} second = in.nextInt() second in.nextInt() in.nextInt in now = second - fast now second - fast second fast if(maxm < now){
		       maxm = now;
		   } maxm < now maxm now {
		       maxm = now;
		   } maxm = now maxm now sum = sum + now sum sum + now sum now fast = second fast second last = ollm-(second - memo) last ollm-(second - memo) ollm (second - memo) second memo sum = sum + last sum sum + last sum last if(maxm< last){
		    maxm = last;
		} maxm< last maxm last {
		    maxm = last;
		} maxm = last maxm last System.out.println(sum - maxm) System.out.println System.out System System.out sum - maxm sum maxm String[] args args public class Main {
	public static void main (String[] args) {
		Scanner in = new Scanner(System.in);
		
		int ollm = in.nextInt();
		int hous = in.nextInt();
		int fast = in.nextInt();
		int memo = fast;
		int second =  0;
		int last = 0;
		int now =  0;
		long sum = 0;
		long maxm = 0;
		
		for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		}
		
		last = ollm-(second - memo);
		sum = sum + last;
		if(maxm< last){
		    maxm = last;
		}
		
		System.out.println(sum - maxm);
      
	}
} public class Main {
	public static void main (String[] args) {
		Scanner in = new Scanner(System.in);
		
		int ollm = in.nextInt();
		int hous = in.nextInt();
		int fast = in.nextInt();
		int memo = fast;
		int second =  0;
		int last = 0;
		int now =  0;
		long sum = 0;
		long maxm = 0;
		
		for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		}
		
		last = ollm-(second - memo);
		sum = sum + last;
		if(maxm< last){
		    maxm = last;
		}
		
		System.out.println(sum - maxm);
      
	}
} Main
import java.util.*;
 
public class Main {
	public static void main (String[] args) {
		Scanner in = new Scanner(System.in);
		
		int ollm = in.nextInt();
		int hous = in.nextInt();
		int fast = in.nextInt();
		int memo = fast;
		int second =  0;
		int last = 0;
		int now =  0;
		long sum = 0;
		long maxm = 0;
		
		for(int i = 0;i < hous -1;i++){
		   second = in.nextInt();
		   now = second - fast;
		   if(maxm < now){
		       maxm = now;
		   }
		    sum = sum + now;
		    fast = second;
		}
		
		last = ollm-(second - memo);
		sum = sum + last;
		if(maxm< last){
		    maxm = last;
		}
		
		System.out.println(sum - maxm);
      
	}
}