import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);    
    int circle = scan.nextInt();
    int num = scan.nextInt();
    
    List<Integer> length = new ArrayList<>();
    for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    }
    scan.close();

    List<Integer> diff = new ArrayList<>();
    
    for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    }
      diff.add(circle - length.get(length.size() - 1) + length.get(0));
    
    Collections.sort(diff, Collections.reverseOrder());
    
    System.out.println(circle - diff.get(0));
    
  }
} import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections 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 scan = new Scanner(System.in);    
    int circle = scan.nextInt();
    int num = scan.nextInt();
    
    List<Integer> length = new ArrayList<>();
    for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    }
    scan.close();

    List<Integer> diff = new ArrayList<>();
    
    for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    }
      diff.add(circle - length.get(length.size() - 1) + length.get(0));
    
    Collections.sort(diff, Collections.reverseOrder());
    
    System.out.println(circle - diff.get(0));
    
  }
} Main public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);    
    int circle = scan.nextInt();
    int num = scan.nextInt();
    
    List<Integer> length = new ArrayList<>();
    for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    }
    scan.close();

    List<Integer> diff = new ArrayList<>();
    
    for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    }
      diff.add(circle - length.get(length.size() - 1) + length.get(0));
    
    Collections.sort(diff, Collections.reverseOrder());
    
    System.out.println(circle - diff.get(0));
    
  } main {
    Scanner scan = new Scanner(System.in);    
    int circle = scan.nextInt();
    int num = scan.nextInt();
    
    List<Integer> length = new ArrayList<>();
    for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    }
    scan.close();

    List<Integer> diff = new ArrayList<>();
    
    for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    }
      diff.add(circle - length.get(length.size() - 1) + length.get(0));
    
    Collections.sort(diff, Collections.reverseOrder());
    
    System.out.println(circle - diff.get(0));
    
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int circle = scan.nextInt(); circle scan.nextInt() scan.nextInt scan int num = scan.nextInt(); num scan.nextInt() scan.nextInt scan List<Integer> length = new ArrayList<>(); length new ArrayList<>() for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    } int i = 0; int i = 0; i 0 i < num i num i++ i++ i {
      length.add(scan.nextInt());
    } {
      length.add(scan.nextInt());
    } length.add(scan.nextInt()) length.add length scan.nextInt() scan.nextInt scan scan.close() scan.close scan List<Integer> diff = new ArrayList<>(); diff new ArrayList<>() for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    } int i = 0; int i = 0; i 0 i < length.size() - 1 i length.size() - 1 length.size() length.size length 1 i++ i++ i {
      diff.add(length.get(i + 1) - length.get(i));
    } {
      diff.add(length.get(i + 1) - length.get(i));
    } diff.add(length.get(i + 1) - length.get(i)) diff.add diff length.get(i + 1) - length.get(i) length.get(i + 1) length.get length i + 1 i 1 length.get(i) length.get length i diff.add(circle - length.get(length.size() - 1) + length.get(0)) diff.add diff circle - length.get(length.size() - 1) + length.get(0) circle - length.get(length.size() - 1) circle length.get(length.size() - 1) length.get length length.size() - 1 length.size() length.size length 1 length.get(0) length.get length 0 Collections.sort(diff, Collections.reverseOrder()) Collections.sort Collections diff Collections.reverseOrder() Collections.reverseOrder Collections System.out.println(circle - diff.get(0)) System.out.println System.out System System.out circle - diff.get(0) circle diff.get(0) diff.get diff 0 String[] args args public class Main {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);    
    int circle = scan.nextInt();
    int num = scan.nextInt();
    
    List<Integer> length = new ArrayList<>();
    for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    }
    scan.close();

    List<Integer> diff = new ArrayList<>();
    
    for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    }
      diff.add(circle - length.get(length.size() - 1) + length.get(0));
    
    Collections.sort(diff, Collections.reverseOrder());
    
    System.out.println(circle - diff.get(0));
    
  }
} public class Main {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);    
    int circle = scan.nextInt();
    int num = scan.nextInt();
    
    List<Integer> length = new ArrayList<>();
    for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    }
    scan.close();

    List<Integer> diff = new ArrayList<>();
    
    for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    }
      diff.add(circle - length.get(length.size() - 1) + length.get(0));
    
    Collections.sort(diff, Collections.reverseOrder());
    
    System.out.println(circle - diff.get(0));
    
  }
} Main
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);    
    int circle = scan.nextInt();
    int num = scan.nextInt();
    
    List<Integer> length = new ArrayList<>();
    for(int i = 0; i < num; i++) {
      length.add(scan.nextInt());
    }
    scan.close();

    List<Integer> diff = new ArrayList<>();
    
    for(int i = 0; i < length.size() - 1; i++) {
      diff.add(length.get(i + 1) - length.get(i));
    }
      diff.add(circle - length.get(length.size() - 1) + length.get(0));
    
    Collections.sort(diff, Collections.reverseOrder());
    
    System.out.println(circle - diff.get(0));
    
  }
}