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

public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final Integer K = scanner.nextInt();
    final Integer N = scanner.nextInt();

    final List<Integer> As = new ArrayList<>();
    for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    }

    int max = K - (As.get(As.size() - 1) - As.get(0));
    for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    }
      System.out.println(K - max);
  }
} 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) {
    final Scanner scanner = new Scanner(System.in);

    final Integer K = scanner.nextInt();
    final Integer N = scanner.nextInt();

    final List<Integer> As = new ArrayList<>();
    for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    }

    int max = K - (As.get(As.size() - 1) - As.get(0));
    for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    }
      System.out.println(K - max);
  }
} Main public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final Integer K = scanner.nextInt();
    final Integer N = scanner.nextInt();

    final List<Integer> As = new ArrayList<>();
    for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    }

    int max = K - (As.get(As.size() - 1) - As.get(0));
    for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    }
      System.out.println(K - max);
  } main {
    final Scanner scanner = new Scanner(System.in);

    final Integer K = scanner.nextInt();
    final Integer N = scanner.nextInt();

    final List<Integer> As = new ArrayList<>();
    for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    }

    int max = K - (As.get(As.size() - 1) - As.get(0));
    for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    }
      System.out.println(K - max);
  } final Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) final Integer K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner final Integer N = scanner.nextInt(); N scanner.nextInt() scanner.nextInt scanner final List<Integer> As = new ArrayList<>(); As new ArrayList<>() for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    } Integer i = 0; Integer i = 0; i 0 i < N i N i++ i++ i {
      As.add(scanner.nextInt());
    } {
      As.add(scanner.nextInt());
    } As.add(scanner.nextInt()) As.add As scanner.nextInt() scanner.nextInt scanner int max = K - (As.get(As.size() - 1) - As.get(0)); max K - (As.get(As.size() - 1) - As.get(0)) K (As.get(As.size() - 1) - As.get(0)) As.get(As.size() - 1) As.get As As.size() - 1 As.size() As.size As 1 As.get(0) As.get As 0 for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    } int i = 0; int i = 0; i 0 i < As.size() - 1 i As.size() - 1 As.size() As.size As 1 i++ i++ i {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    } {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    } final int diff = As.get(i + 1) - As.get(i); diff As.get(i + 1) - As.get(i) As.get(i + 1) As.get As i + 1 i 1 As.get(i) As.get As i max = Math.max(diff, max) max Math.max(diff, max) Math.max Math diff max System.out.println(K - max) System.out.println System.out System System.out K - max K max String[] args args public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final Integer K = scanner.nextInt();
    final Integer N = scanner.nextInt();

    final List<Integer> As = new ArrayList<>();
    for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    }

    int max = K - (As.get(As.size() - 1) - As.get(0));
    for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    }
      System.out.println(K - max);
  }
} public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final Integer K = scanner.nextInt();
    final Integer N = scanner.nextInt();

    final List<Integer> As = new ArrayList<>();
    for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    }

    int max = K - (As.get(As.size() - 1) - As.get(0));
    for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    }
      System.out.println(K - max);
  }
} Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    final Scanner scanner = new Scanner(System.in);

    final Integer K = scanner.nextInt();
    final Integer N = scanner.nextInt();

    final List<Integer> As = new ArrayList<>();
    for (Integer i = 0; i < N; i++) {
      As.add(scanner.nextInt());
    }

    int max = K - (As.get(As.size() - 1) - As.get(0));
    for (int i = 0; i < As.size() - 1; i++) {
      final int diff = As.get(i + 1) - As.get(i);
      max = Math.max(diff, max);
    }
      System.out.println(K - max);
  }
}
