import java.util.*;
import java.io.*;
 
public class Main {
  public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] num = new int[n];
    for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    }
    new Solution(n, k, num);
  }
}

class Solution {
  public Solution(int n, int k, int[] num) {
    int res = 0;
    Arrays.sort(num);
    for(int i = 1; i < n; ++i) {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    }
    if(res < k + num[0] - num[n - 1]) res = k + num[0] - num[n - 1];
    System.out.println(k - res);
      // System.out.println(res);
  }
}

class Scanner {
  StringTokenizer st;
  BufferedReader br;

  public Scanner(InputStream system) {
    br = new BufferedReader(new InputStreamReader(system));
  }

  public Scanner(String file) throws Exception {
    br = new BufferedReader(new FileReader(file));
  }

  public String next() throws IOException {
    while (st == null || !st.hasMoreTokens())
      st = new StringTokenizer(br.readLine());
    return st.nextToken();
  }

  public String nextLine() throws IOException {
    return br.readLine();
  }

  public int nextInt() throws IOException {
    return Integer.parseInt(next());
  }

  public double nextDouble() throws IOException {
    return Double.parseDouble(next());
  }

  public char nextChar() throws IOException {
    return next().charAt(0);
  }

  public Long nextLong() throws IOException {
    return Long.parseLong(next());
  }

  public int[] nextArrInt(int n) throws IOException {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  }

  public long[] nextArrLong(int n) throws IOException {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  }

  public boolean ready() throws IOException {
    return br.ready();
  }

  public void waitForInput() throws InterruptedException {
    Thread.sleep(3000);
  }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
  public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] num = new int[n];
    for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    }
    new Solution(n, k, num);
  }
} Main public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] num = new int[n];
    for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    }
    new Solution(n, k, num);
  } main {
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] num = new int[n];
    for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    }
    new Solution(n, k, num);
  } 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[] num = new int[n]; num new int[n] n for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    } int i = 0; int i = 0; i 0 i < n i n ++i ++i i {
      num[i] = sc.nextInt();
    } {
      num[i] = sc.nextInt();
    } num[i] = sc.nextInt() num[i] num i sc.nextInt() sc.nextInt sc new Solution(n, k, num) String[] args args class Solution {
  public Solution(int n, int k, int[] num) {
    int res = 0;
    Arrays.sort(num);
    for(int i = 1; i < n; ++i) {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    }
    if(res < k + num[0] - num[n - 1]) res = k + num[0] - num[n - 1];
    System.out.println(k - res);
      // System.out.println(res);
  }
} Solution public Solution(int n, int k, int[] num) {
    int res = 0;
    Arrays.sort(num);
    for(int i = 1; i < n; ++i) {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    }
    if(res < k + num[0] - num[n - 1]) res = k + num[0] - num[n - 1];
    System.out.println(k - res);
      // System.out.println(res);
  } Solution {
    int res = 0;
    Arrays.sort(num);
    for(int i = 1; i < n; ++i) {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    }
    if(res < k + num[0] - num[n - 1]) res = k + num[0] - num[n - 1];
    System.out.println(k - res);
      // System.out.println(res);
  } int res = 0; res 0 Arrays.sort(num) Arrays.sort Arrays num for(int i = 1; i < n; ++i) {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    } int i = 1; int i = 1; i 1 i < n i n ++i ++i i {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    } {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    } if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1]; res < num[i] - num[i - 1] res num[i] - num[i - 1] num[i] num i num[i - 1] num i - 1 i 1 res = num[i] - num[i - 1] res num[i] - num[i - 1] num[i] num i num[i - 1] num i - 1 i 1 if(res < k + num[0] - num[n - 1]) res = k + num[0] - num[n - 1]; res < k + num[0] - num[n - 1] res k + num[0] - num[n - 1] k + num[0] k num[0] num 0 num[n - 1] num n - 1 n 1 res = k + num[0] - num[n - 1] res k + num[0] - num[n - 1] k + num[0] k num[0] num 0 num[n - 1] num n - 1 n 1 System.out.println(k - res) System.out.println System.out System System.out k - res k res int n n int k k int[] num num class Scanner {
  StringTokenizer st;
  BufferedReader br;

  public Scanner(InputStream system) {
    br = new BufferedReader(new InputStreamReader(system));
  }

  public Scanner(String file) throws Exception {
    br = new BufferedReader(new FileReader(file));
  }

  public String next() throws IOException {
    while (st == null || !st.hasMoreTokens())
      st = new StringTokenizer(br.readLine());
    return st.nextToken();
  }

  public String nextLine() throws IOException {
    return br.readLine();
  }

  public int nextInt() throws IOException {
    return Integer.parseInt(next());
  }

  public double nextDouble() throws IOException {
    return Double.parseDouble(next());
  }

  public char nextChar() throws IOException {
    return next().charAt(0);
  }

  public Long nextLong() throws IOException {
    return Long.parseLong(next());
  }

  public int[] nextArrInt(int n) throws IOException {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  }

  public long[] nextArrLong(int n) throws IOException {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  }

  public boolean ready() throws IOException {
    return br.ready();
  }

  public void waitForInput() throws InterruptedException {
    Thread.sleep(3000);
  }
} Scanner StringTokenizer st; st BufferedReader br; br public Scanner(InputStream system) {
    br = new BufferedReader(new InputStreamReader(system));
  } Scanner {
    br = new BufferedReader(new InputStreamReader(system));
  } br = new BufferedReader(new InputStreamReader(system)) br new BufferedReader(new InputStreamReader(system)) InputStream system system public Scanner(String file) throws Exception {
    br = new BufferedReader(new FileReader(file));
  } Scanner {
    br = new BufferedReader(new FileReader(file));
  } br = new BufferedReader(new FileReader(file)) br new BufferedReader(new FileReader(file)) String file file public String next() throws IOException {
    while (st == null || !st.hasMoreTokens())
      st = new StringTokenizer(br.readLine());
    return st.nextToken();
  } next {
    while (st == null || !st.hasMoreTokens())
      st = new StringTokenizer(br.readLine());
    return st.nextToken();
  } while (st == null || !st.hasMoreTokens())
      st = new StringTokenizer(br.readLine()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st public String nextLine() throws IOException {
    return br.readLine();
  } nextLine {
    return br.readLine();
  } return br.readLine(); br.readLine() br.readLine br public int nextInt() throws IOException {
    return Integer.parseInt(next());
  } nextInt {
    return Integer.parseInt(next());
  } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public double nextDouble() throws IOException {
    return Double.parseDouble(next());
  } nextDouble {
    return Double.parseDouble(next());
  } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next public char nextChar() throws IOException {
    return next().charAt(0);
  } nextChar {
    return next().charAt(0);
  } return next().charAt(0); next().charAt(0) next().charAt next() next 0 public Long nextLong() throws IOException {
    return Long.parseLong(next());
  } nextLong {
    return Long.parseLong(next());
  } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public int[] nextArrInt(int n) throws IOException {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  } nextArrInt {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  } int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++)
      a[i] = nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = nextInt(); a[i] = nextInt() a[i] a i nextInt() nextInt return a; a int n n public long[] nextArrLong(int n) throws IOException {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  } nextArrLong {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  } long[] a = new long[n]; a new long[n] n for (int i = 0; i < n; i++)
      a[i] = nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = nextInt(); a[i] = nextInt() a[i] a i nextInt() nextInt return a; a int n n public boolean ready() throws IOException {
    return br.ready();
  } ready {
    return br.ready();
  } return br.ready(); br.ready() br.ready br public void waitForInput() throws InterruptedException {
    Thread.sleep(3000);
  } waitForInput {
    Thread.sleep(3000);
  } Thread.sleep(3000) Thread.sleep Thread 3000 public class Main {
  public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] num = new int[n];
    for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    }
    new Solution(n, k, num);
  }
} public class Main {
  public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] num = new int[n];
    for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    }
    new Solution(n, k, num);
  }
} Main
import java.util.*;
import java.io.*;
 
public class Main {
  public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int n = sc.nextInt();
    int[] num = new int[n];
    for(int i = 0; i < n; ++i) {
      num[i] = sc.nextInt();
    }
    new Solution(n, k, num);
  }
}

class Solution {
  public Solution(int n, int k, int[] num) {
    int res = 0;
    Arrays.sort(num);
    for(int i = 1; i < n; ++i) {
      if(res < num[i] - num[i - 1]) res = num[i] - num[i - 1];
    }
    if(res < k + num[0] - num[n - 1]) res = k + num[0] - num[n - 1];
    System.out.println(k - res);
      // System.out.println(res);
  }
}

class Scanner {
  StringTokenizer st;
  BufferedReader br;

  public Scanner(InputStream system) {
    br = new BufferedReader(new InputStreamReader(system));
  }

  public Scanner(String file) throws Exception {
    br = new BufferedReader(new FileReader(file));
  }

  public String next() throws IOException {
    while (st == null || !st.hasMoreTokens())
      st = new StringTokenizer(br.readLine());
    return st.nextToken();
  }

  public String nextLine() throws IOException {
    return br.readLine();
  }

  public int nextInt() throws IOException {
    return Integer.parseInt(next());
  }

  public double nextDouble() throws IOException {
    return Double.parseDouble(next());
  }

  public char nextChar() throws IOException {
    return next().charAt(0);
  }

  public Long nextLong() throws IOException {
    return Long.parseLong(next());
  }

  public int[] nextArrInt(int n) throws IOException {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  }

  public long[] nextArrLong(int n) throws IOException {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nextInt();
    return a;
  }

  public boolean ready() throws IOException {
    return br.ready();
  }

  public void waitForInput() throws InterruptedException {
    Thread.sleep(3000);
  }
}