import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] a = new int[n];
    int sub = 0;
    int dis = 0;
    int max_dis = 0;

    for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    }

    for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    }
    System.out.println(dis - max_dis);





  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] a = new int[n];
    int sub = 0;
    int dis = 0;
    int max_dis = 0;

    for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    }

    for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    }
    System.out.println(dis - max_dis);





  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] a = new int[n];
    int sub = 0;
    int dis = 0;
    int max_dis = 0;

    for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    }

    for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    }
    System.out.println(dis - max_dis);





  } main {
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] a = new int[n];
    int sub = 0;
    int dis = 0;
    int max_dis = 0;

    for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    }

    for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    }
    System.out.println(dis - max_dis);





  } 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 int sub = 0; sub 0 int dis = 0; dis 0 int max_dis = 0; max_dis 0 for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      a[i] = sc.nextInt();
    } {
      a[i] = sc.nextInt();
    } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    } int j=0; int j=0; j 0 j<n j n j++ j++ j {
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    } {
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    } if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      } j<n-1 j n-1 n 1 {
        sub = a[j+1]-a[j];
        dis += sub;
      } sub = a[j+1]-a[j] sub a[j+1]-a[j] a[j+1] a j+1 j 1 a[j] a j dis += sub dis sub if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      } j==n-1 j n-1 n 1 {
        sub = k - a[n-1]+a[0];
        dis += sub;
      } sub = k - a[n-1]+a[0] sub k - a[n-1]+a[0] k - a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 dis += sub dis sub max_dis = Math.max(max_dis,sub) max_dis Math.max(max_dis,sub) Math.max Math max_dis sub System.out.println(dis - max_dis) System.out.println System.out System System.out dis - max_dis dis max_dis String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] a = new int[n];
    int sub = 0;
    int dis = 0;
    int max_dis = 0;

    for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    }

    for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    }
    System.out.println(dis - max_dis);





  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] a = new int[n];
    int sub = 0;
    int dis = 0;
    int max_dis = 0;

    for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    }

    for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    }
    System.out.println(dis - max_dis);





  }
} Main
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] a = new int[n];
    int sub = 0;
    int dis = 0;
    int max_dis = 0;

    for(int i=0; i<n; i++){
      a[i] = sc.nextInt();
    }

    for(int j=0; j<n; j++){
      if(j<n-1){
        sub = a[j+1]-a[j];
        dis += sub;
      }else if(j==n-1){
        sub = k - a[n-1]+a[0];
        dis += sub;
      }

      max_dis = Math.max(max_dis,sub);
      //System.out.println("MAX_DIS"+max_dis);


    }
    System.out.println(dis - max_dis);





  }
}