import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main{
  public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    List<Integer> a = new ArrayList<Integer>();
    for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    }
    a.add(a.get(0) + k);
    int max_Length = 0;
    for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    }
    int ans = k - max_Length;
    System.out.println(ans);
  }
} import java.util.ArrayList; ArrayList java.util import java.util.List; List java.util import java.util.Scanner; Scanner 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();
    List<Integer> a = new ArrayList<Integer>();
    for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    }
    a.add(a.get(0) + k);
    int max_Length = 0;
    for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    }
    int ans = k - max_Length;
    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();
    List<Integer> a = new ArrayList<Integer>();
    for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    }
    a.add(a.get(0) + k);
    int max_Length = 0;
    for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    }
    int ans = k - max_Length;
    System.out.println(ans);
  } main {
  Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    List<Integer> a = new ArrayList<Integer>();
    for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    }
    a.add(a.get(0) + k);
    int max_Length = 0;
    for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    }
    int ans = k - max_Length;
    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 List<Integer> a = new ArrayList<Integer>(); a new ArrayList<Integer>() for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      a.add(sc.nextInt());
    } {
      a.add(sc.nextInt());
    } a.add(sc.nextInt()) a.add a sc.nextInt() sc.nextInt sc a.add(a.get(0) + k) a.add a a.get(0) + k a.get(0) a.get a 0 k int max_Length = 0; max_Length 0 for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    } int j = 0; int j = 0; j 0 j < n j n j++ j++ j {
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    } {
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    } int temp = a.get(j+1) - a.get(j); temp a.get(j+1) - a.get(j) a.get(j+1) a.get a j+1 j 1 a.get(j) a.get a j if(temp > max_Length){
        max_Length = temp;
      } temp > max_Length temp max_Length {
        max_Length = temp;
      } max_Length = temp max_Length temp int ans = k - max_Length; ans k - max_Length k max_Length System.out.println(ans) System.out.println System.out System System.out ans 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();
    List<Integer> a = new ArrayList<Integer>();
    for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    }
    a.add(a.get(0) + k);
    int max_Length = 0;
    for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    }
    int ans = k - max_Length;
    System.out.println(ans);
  }
} public class Main{
  public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    List<Integer> a = new ArrayList<Integer>();
    for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    }
    a.add(a.get(0) + k);
    int max_Length = 0;
    for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    }
    int ans = k - max_Length;
    System.out.println(ans);
  }
} Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main{
  public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    List<Integer> a = new ArrayList<Integer>();
    for(int i = 0; i < n; i++){
      a.add(sc.nextInt());
    }
    a.add(a.get(0) + k);
    int max_Length = 0;
    for(int j = 0; j < n; j++){
      int temp = a.get(j+1) - a.get(j);
      if(temp > max_Length){
        max_Length = temp;
      }
    }
    int ans = k - max_Length;
    System.out.println(ans);
  }
}