import java.util.*;

public class Main{
	public static void main(String[] arg){
    	Scanner sc=new Scanner(System.in);
      int k=sc.nextInt();
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      }
      int max=Integer.MIN_VALUE;
      for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      }
      int m=k-a[n-1] + a[0];
      System.out.println(k-Math.max(max,m));
        
    }
} import java.util.*; util.* java public class Main{
	public static void main(String[] arg){
    	Scanner sc=new Scanner(System.in);
      int k=sc.nextInt();
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      }
      int max=Integer.MIN_VALUE;
      for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      }
      int m=k-a[n-1] + a[0];
      System.out.println(k-Math.max(max,m));
        
    }
} Main public static void main(String[] arg){
    	Scanner sc=new Scanner(System.in);
      int k=sc.nextInt();
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      }
      int max=Integer.MIN_VALUE;
      for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      }
      int m=k-a[n-1] + a[0];
      System.out.println(k-Math.max(max,m));
        
    } main {
    	Scanner sc=new Scanner(System.in);
      int k=sc.nextInt();
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      }
      int max=Integer.MIN_VALUE;
      for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      }
      int m=k-a[n-1] + a[0];
      System.out.println(k-Math.max(max,m));
        
    } Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int a[]=new int[n]; a new int[n] n for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      } int i=0; int i=0; i 0 i<n i n i++ i++ i {
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      } {
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      } a[i]=sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc if(a[i]>=k){
              a[i]-=k;
          } a[i]>=k a[i] a i k {
              a[i]-=k;
          } a[i]-=k a[i] a i k int max=Integer.MIN_VALUE; max Integer.MIN_VALUE Integer Integer.MIN_VALUE for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      } int i=1; int i=1; i 1 i<n i n i++ i++ i {
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      } {
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      } max=Math.max(max,Math.abs(a[i]-a[i-1])) max Math.max(max,Math.abs(a[i]-a[i-1])) Math.max Math max Math.abs(a[i]-a[i-1]) Math.abs Math a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 int m=k-a[n-1] + a[0]; m k-a[n-1] + a[0] k-a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 System.out.println(k-Math.max(max,m)) System.out.println System.out System System.out k-Math.max(max,m) k Math.max(max,m) Math.max Math max m String[] arg arg public class Main{
	public static void main(String[] arg){
    	Scanner sc=new Scanner(System.in);
      int k=sc.nextInt();
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      }
      int max=Integer.MIN_VALUE;
      for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      }
      int m=k-a[n-1] + a[0];
      System.out.println(k-Math.max(max,m));
        
    }
} public class Main{
	public static void main(String[] arg){
    	Scanner sc=new Scanner(System.in);
      int k=sc.nextInt();
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      }
      int max=Integer.MIN_VALUE;
      for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      }
      int m=k-a[n-1] + a[0];
      System.out.println(k-Math.max(max,m));
        
    }
} Main
import java.util.*;

public class Main{
	public static void main(String[] arg){
    	Scanner sc=new Scanner(System.in);
      int k=sc.nextInt();
      int n=sc.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++){
          a[i]=sc.nextInt();
          if(a[i]>=k){
              a[i]-=k;
          }
          
      }
      int max=Integer.MIN_VALUE;
      for(int i=1;i<n;i++){
          max=Math.max(max,Math.abs(a[i]-a[i-1]));
      }
      int m=k-a[n-1] + a[0];
      System.out.println(k-Math.max(max,m));
        
    }
}