import java.util.*;
import java.math.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    int n = sc.nextInt();

    long[] a = new long[n];

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

    long sum = 0;

    long[] data = new long[n+1];
    data[n] = k-a[n-1];
    data[0] = a[0];

    for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    }

    // for (int i = 0; i <= n; i++) {
    //   System.out.println(data[i]);
    // }

    Arrays.sort(data);

    for (int i = 0; i < n; i++) {
      sum += data[i];
    }

    long ans = 0;

    for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    }

    System.out.println(Math.min(ans,sum));

  }

} import java.util.*; util.* java import java.math.*; math.* 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[] a = new long[n];

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

    long sum = 0;

    long[] data = new long[n+1];
    data[n] = k-a[n-1];
    data[0] = a[0];

    for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    }

    // for (int i = 0; i <= n; i++) {
    //   System.out.println(data[i]);
    // }

    Arrays.sort(data);

    for (int i = 0; i < n; i++) {
      sum += data[i];
    }

    long ans = 0;

    for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    }

    System.out.println(Math.min(ans,sum));

  }

} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    int n = sc.nextInt();

    long[] a = new long[n];

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

    long sum = 0;

    long[] data = new long[n+1];
    data[n] = k-a[n-1];
    data[0] = a[0];

    for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    }

    // for (int i = 0; i <= n; i++) {
    //   System.out.println(data[i]);
    // }

    Arrays.sort(data);

    for (int i = 0; i < n; i++) {
      sum += data[i];
    }

    long ans = 0;

    for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    }

    System.out.println(Math.min(ans,sum));

  } main {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    int n = sc.nextInt();

    long[] a = new long[n];

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

    long sum = 0;

    long[] data = new long[n+1];
    data[n] = k-a[n-1];
    data[0] = a[0];

    for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    }

    // for (int i = 0; i <= n; i++) {
    //   System.out.println(data[i]);
    // }

    Arrays.sort(data);

    for (int i = 0; i < n; i++) {
      sum += data[i];
    }

    long ans = 0;

    for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    }

    System.out.println(Math.min(ans,sum));

  } 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[] a = new long[n]; a new long[n] n for (int i = 0; i < n; i++) {
      a[i] = sc.nextLong();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      a[i] = sc.nextLong();
    } {
      a[i] = sc.nextLong();
    } a[i] = sc.nextLong() a[i] a i sc.nextLong() sc.nextLong sc long sum = 0; sum 0 long[] data = new long[n+1]; data new long[n+1] n+1 n 1 data[n] = k-a[n-1] data[n] data n k-a[n-1] k a[n-1] a n-1 n 1 data[0] = a[0] data[0] data 0 a[0] a 0 for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
      data[i] = a[i]-a[i-1];
    } {
      data[i] = a[i]-a[i-1];
    } data[i] = a[i]-a[i-1] data[i] data i a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 Arrays.sort(data) Arrays.sort Arrays data for (int i = 0; i < n; i++) {
      sum += data[i];
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      sum += data[i];
    } {
      sum += data[i];
    } sum += data[i] sum data[i] data i long ans = 0; ans 0 for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    } int i = n-1; int i = n-1; i n-1 n 1 i > 0 i 0 i-- i-- i {
      ans += a[i]-a[i-1];
    } {
      ans += a[i]-a[i-1];
    } ans += a[i]-a[i-1] ans a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(Math.min(ans,sum)) System.out.println System.out System System.out Math.min(ans,sum) Math.min Math ans sum 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[] a = new long[n];

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

    long sum = 0;

    long[] data = new long[n+1];
    data[n] = k-a[n-1];
    data[0] = a[0];

    for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    }

    // for (int i = 0; i <= n; i++) {
    //   System.out.println(data[i]);
    // }

    Arrays.sort(data);

    for (int i = 0; i < n; i++) {
      sum += data[i];
    }

    long ans = 0;

    for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    }

    System.out.println(Math.min(ans,sum));

  }

} public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    int n = sc.nextInt();

    long[] a = new long[n];

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

    long sum = 0;

    long[] data = new long[n+1];
    data[n] = k-a[n-1];
    data[0] = a[0];

    for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    }

    // for (int i = 0; i <= n; i++) {
    //   System.out.println(data[i]);
    // }

    Arrays.sort(data);

    for (int i = 0; i < n; i++) {
      sum += data[i];
    }

    long ans = 0;

    for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    }

    System.out.println(Math.min(ans,sum));

  }

} Main
import java.util.*;
import java.math.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long k = sc.nextLong();
    int n = sc.nextInt();

    long[] a = new long[n];

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

    long sum = 0;

    long[] data = new long[n+1];
    data[n] = k-a[n-1];
    data[0] = a[0];

    for (int i = 1; i < n; i++) {
      data[i] = a[i]-a[i-1];
    }

    // for (int i = 0; i <= n; i++) {
    //   System.out.println(data[i]);
    // }

    Arrays.sort(data);

    for (int i = 0; i < n; i++) {
      sum += data[i];
    }

    long ans = 0;

    for (int i = n-1; i > 0; i--) {
      ans += a[i]-a[i-1];
    }

    System.out.println(Math.min(ans,sum));

  }

}
