import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=sc.nextInt();
    }
    int[] dist = new int[n];
    for(int i=0;i<n-1;i++){
      dist[i]=arr[i+1]-arr[i];
    }
    dist[n-1]=k-arr[n-1]+arr[0];
    Arrays.sort(dist);
    int ans=0;
    for(int i=0;i<n-1;i++){
      ans = ans +dist[i];
    }
    System.out.println(ans);
  }
} import java.util.*; util.* java class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=sc.nextInt();
    }
    int[] dist = new int[n];
    for(int i=0;i<n-1;i++){
      dist[i]=arr[i+1]-arr[i];
    }
    dist[n-1]=k-arr[n-1]+arr[0];
    Arrays.sort(dist);
    int ans=0;
    for(int i=0;i<n-1;i++){
      ans = ans +dist[i];
    }
    System.out.println(ans);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=sc.nextInt();
    }
    int[] dist = new int[n];
    for(int i=0;i<n-1;i++){
      dist[i]=arr[i+1]-arr[i];
    }
    dist[n-1]=k-arr[n-1]+arr[0];
    Arrays.sort(dist);
    int ans=0;
    for(int i=0;i<n-1;i++){
      ans = ans +dist[i];
    }
    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=sc.nextInt();
    }
    int[] dist = new int[n];
    for(int i=0;i<n-1;i++){
      dist[i]=arr[i+1]-arr[i];
    }
    dist[n-1]=k-arr[n-1]+arr[0];
    Arrays.sort(dist);
    int ans=0;
    for(int i=0;i<n-1;i++){
      ans = ans +dist[i];
    }
    System.out.println(ans);
  } 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[] arr = new int[n]; arr new int[n] n for(int i=0;i<n;i++){
      arr[i]=sc.nextInt();
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      arr[i]=sc.nextInt();
    } {
      arr[i]=sc.nextInt();
    } arr[i]=sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc int[] dist = new int[n]; dist new int[n] n for(int i=0;i<n-1;i++){
      dist[i]=arr[i+1]-arr[i];
    } int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
      dist[i]=arr[i+1]-arr[i];
    } {
      dist[i]=arr[i+1]-arr[i];
    } dist[i]=arr[i+1]-arr[i] dist[i] dist i arr[i+1]-arr[i] arr[i+1] arr i+1 i 1 arr[i] arr i dist[n-1]=k-arr[n-1]+arr[0] dist[n-1] dist n-1 n 1 k-arr[n-1]+arr[0] k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 Arrays.sort(dist) Arrays.sort Arrays dist int ans=0; ans 0 for(int i=0;i<n-1;i++){
      ans = ans +dist[i];
    } int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
      ans = ans +dist[i];
    } {
      ans = ans +dist[i];
    } ans = ans +dist[i] ans ans +dist[i] ans dist[i] dist i System.out.println(ans) System.out.println System.out System System.out ans String[] args args
import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] arr = new int[n];
    for(int i=0;i<n;i++){
      arr[i]=sc.nextInt();
    }
    int[] dist = new int[n];
    for(int i=0;i<n-1;i++){
      dist[i]=arr[i+1]-arr[i];
    }
    dist[n-1]=k-arr[n-1]+arr[0];
    Arrays.sort(dist);
    int ans=0;
    for(int i=0;i<n-1;i++){
      ans = ans +dist[i];
    }
    System.out.println(ans);
  }
}