import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    int k = in.nextInt();
    int n = in.nextInt();
    int[] a = new int[n];
    int maxDist = Integer.MIN_VALUE;
    a[0] = in.nextInt();
    for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    }
    if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    }
    System.out.println(k - maxDist);
  }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    int k = in.nextInt();
    int n = in.nextInt();
    int[] a = new int[n];
    int maxDist = Integer.MIN_VALUE;
    a[0] = in.nextInt();
    for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    }
    if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    }
    System.out.println(k - maxDist);
  }
} Main public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    int k = in.nextInt();
    int n = in.nextInt();
    int[] a = new int[n];
    int maxDist = Integer.MIN_VALUE;
    a[0] = in.nextInt();
    for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    }
    if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    }
    System.out.println(k - maxDist);
  } main {
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    int k = in.nextInt();
    int n = in.nextInt();
    int[] a = new int[n];
    int maxDist = Integer.MIN_VALUE;
    a[0] = in.nextInt();
    for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    }
    if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    }
    System.out.println(k - maxDist);
  } Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); in new Scanner(new BufferedReader(new InputStreamReader(System.in))) int k = in.nextInt(); k in.nextInt() in.nextInt in int n = in.nextInt(); n in.nextInt() in.nextInt in int[] a = new int[n]; a new int[n] n int maxDist = Integer.MIN_VALUE; maxDist Integer.MIN_VALUE Integer Integer.MIN_VALUE a[0] = in.nextInt() a[0] a 0 in.nextInt() in.nextInt in for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    } int i = 1; int i = 1; i 1 i < n i n ++i ++i i {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    } {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    } a[i] = in.nextInt() a[i] a i in.nextInt() in.nextInt in int maxDist1 = a[i] - a[i - 1]; maxDist1 a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      } maxDist1 > maxDist maxDist1 maxDist {
        maxDist = maxDist1;
      } maxDist = maxDist1 maxDist maxDist1 if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    } k - a[n - 1] + a[0] > maxDist k - a[n - 1] + a[0] k - a[n - 1] k a[n - 1] a n - 1 n 1 a[0] a 0 maxDist {
      maxDist = k - a[n - 1] + a[0];
    } maxDist = k - a[n - 1] + a[0] maxDist k - a[n - 1] + a[0] k - a[n - 1] k a[n - 1] a n - 1 n 1 a[0] a 0 System.out.println(k - maxDist) System.out.println System.out System System.out k - maxDist k maxDist String[] args args public class Main {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    int k = in.nextInt();
    int n = in.nextInt();
    int[] a = new int[n];
    int maxDist = Integer.MIN_VALUE;
    a[0] = in.nextInt();
    for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    }
    if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    }
    System.out.println(k - maxDist);
  }
} public class Main {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    int k = in.nextInt();
    int n = in.nextInt();
    int[] a = new int[n];
    int maxDist = Integer.MIN_VALUE;
    a[0] = in.nextInt();
    for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    }
    if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    }
    System.out.println(k - maxDist);
  }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    int k = in.nextInt();
    int n = in.nextInt();
    int[] a = new int[n];
    int maxDist = Integer.MIN_VALUE;
    a[0] = in.nextInt();
    for (int i = 1; i < n; ++i) {
      a[i] = in.nextInt();
      int maxDist1 = a[i] - a[i - 1];
      if (maxDist1 > maxDist) {
        maxDist = maxDist1;
      }
    }
    if (k - a[n - 1] + a[0] > maxDist) {
      maxDist = k - a[n - 1] + a[0];
    }
    System.out.println(k - maxDist);
  }
}
