import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long k=sc.nextLong();
  	int n=sc.nextInt();
  	long[] ai=new long[n];
  	for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	}
  	long s=0;
  	long[] si=new long[n];
  	for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	}
  	Arrays.sort(si);
  	for(int i=0;i<n-1;i++){
  		s+=si[i];
  	}
  	System.out.print(s);
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long k=sc.nextLong();
  	int n=sc.nextInt();
  	long[] ai=new long[n];
  	for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	}
  	long s=0;
  	long[] si=new long[n];
  	for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	}
  	Arrays.sort(si);
  	for(int i=0;i<n-1;i++){
  		s+=si[i];
  	}
  	System.out.print(s);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long k=sc.nextLong();
  	int n=sc.nextInt();
  	long[] ai=new long[n];
  	for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	}
  	long s=0;
  	long[] si=new long[n];
  	for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	}
  	Arrays.sort(si);
  	for(int i=0;i<n-1;i++){
  		s+=si[i];
  	}
  	System.out.print(s);
  } main {
    Scanner sc = new Scanner(System.in);
  	long k=sc.nextLong();
  	int n=sc.nextInt();
  	long[] ai=new long[n];
  	for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	}
  	long s=0;
  	long[] si=new long[n];
  	for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	}
  	Arrays.sort(si);
  	for(int i=0;i<n-1;i++){
  		s+=si[i];
  	}
  	System.out.print(s);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long k=sc.nextLong(); k sc.nextLong() sc.nextLong sc int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] ai=new long[n]; ai new long[n] n for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	} int i=0; int i=0; i 0 i<n i n i++ i++ i {
  		ai[i]=sc.nextLong();
  	} {
  		ai[i]=sc.nextLong();
  	} ai[i]=sc.nextLong() ai[i] ai i sc.nextLong() sc.nextLong sc long s=0; s 0 long[] si=new long[n]; si new long[n] n for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	} int i=0; int i=0; i 0 i<n i n i++ i++ i {
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	} {
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	} if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		} i==n-1 i n-1 n 1 {
  			si[i]=k+ai[0]-ai[i];
  		} si[i]=k+ai[0]-ai[i] si[i] si i k+ai[0]-ai[i] k+ai[0] k ai[0] ai 0 ai[i] ai i {
  			si[i]=ai[i+1]-ai[i];
  		} si[i]=ai[i+1]-ai[i] si[i] si i ai[i+1]-ai[i] ai[i+1] ai i+1 i 1 ai[i] ai i Arrays.sort(si) Arrays.sort Arrays si for(int i=0;i<n-1;i++){
  		s+=si[i];
  	} int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
  		s+=si[i];
  	} {
  		s+=si[i];
  	} s+=si[i] s si[i] si i System.out.print(s) System.out.print System.out System System.out s String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long k=sc.nextLong();
  	int n=sc.nextInt();
  	long[] ai=new long[n];
  	for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	}
  	long s=0;
  	long[] si=new long[n];
  	for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	}
  	Arrays.sort(si);
  	for(int i=0;i<n-1;i++){
  		s+=si[i];
  	}
  	System.out.print(s);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long k=sc.nextLong();
  	int n=sc.nextInt();
  	long[] ai=new long[n];
  	for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	}
  	long s=0;
  	long[] si=new long[n];
  	for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	}
  	Arrays.sort(si);
  	for(int i=0;i<n-1;i++){
  		s+=si[i];
  	}
  	System.out.print(s);
  }
} Main
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long k=sc.nextLong();
  	int n=sc.nextInt();
  	long[] ai=new long[n];
  	for(int i=0;i<n;i++){
  		ai[i]=sc.nextLong();
  	}
  	long s=0;
  	long[] si=new long[n];
  	for(int i=0;i<n;i++){
  		if(i==n-1){
  			si[i]=k+ai[0]-ai[i];
  		}else{
  			si[i]=ai[i+1]-ai[i];
  		}
  	}
  	Arrays.sort(si);
  	for(int i=0;i<n-1;i++){
  		s+=si[i];
  	}
  	System.out.print(s);
  }
}