import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    int n = s.nextInt();
    int[] a = new int[n];
    int answer = k;
    for(int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    }
    if(n == 2) {
      answer = a[1]-a[0];
    } else {
      for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      }
      int x = a[n-1]-a[0];
      if(answer > x) {
        answer = x;
      }
    }

    System.out.println(answer);
  }
} import java.util.Scanner; Scanner java.util class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    int n = s.nextInt();
    int[] a = new int[n];
    int answer = k;
    for(int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    }
    if(n == 2) {
      answer = a[1]-a[0];
    } else {
      for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      }
      int x = a[n-1]-a[0];
      if(answer > x) {
        answer = x;
      }
    }

    System.out.println(answer);
  }
} Main public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    int n = s.nextInt();
    int[] a = new int[n];
    int answer = k;
    for(int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    }
    if(n == 2) {
      answer = a[1]-a[0];
    } else {
      for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      }
      int x = a[n-1]-a[0];
      if(answer > x) {
        answer = x;
      }
    }

    System.out.println(answer);
  } main {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    int n = s.nextInt();
    int[] a = new int[n];
    int answer = k;
    for(int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    }
    if(n == 2) {
      answer = a[1]-a[0];
    } else {
      for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      }
      int x = a[n-1]-a[0];
      if(answer > x) {
        answer = x;
      }
    }

    System.out.println(answer);
  } Scanner s = new Scanner(System.in); s new Scanner(System.in) int k = s.nextInt(); k s.nextInt() s.nextInt s int n = s.nextInt(); n s.nextInt() s.nextInt s int[] a = new int[n]; a new int[n] n int answer = k; answer k for(int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      a[i] = s.nextInt();
    } {
      a[i] = s.nextInt();
    } a[i] = s.nextInt() a[i] a i s.nextInt() s.nextInt s if(n == 2) {
      answer = a[1]-a[0];
    } else {
      for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      }
      int x = a[n-1]-a[0];
      if(answer > x) {
        answer = x;
      }
    } n == 2 n 2 {
      answer = a[1]-a[0];
    } answer = a[1]-a[0] answer a[1]-a[0] a[1] a 1 a[0] a 0 {
      for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      }
      int x = a[n-1]-a[0];
      if(answer > x) {
        answer = x;
      }
    } for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      } {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      } int y = a[i-1]+k-a[i]; y a[i-1]+k-a[i] a[i-1]+k a[i-1] a i-1 i 1 k a[i] a i if(answer > y) {
          answer = y;
        } answer > y answer y {
          answer = y;
        } answer = y answer y int x = a[n-1]-a[0]; x a[n-1]-a[0] a[n-1] a n-1 n 1 a[0] a 0 if(answer > x) {
        answer = x;
      } answer > x answer x {
        answer = x;
      } answer = x answer x System.out.println(answer) System.out.println System.out System System.out answer String[] args args
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int k = s.nextInt();
    int n = s.nextInt();
    int[] a = new int[n];
    int answer = k;
    for(int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    }
    if(n == 2) {
      answer = a[1]-a[0];
    } else {
      for(int i = 1; i < n; i++) {
        int y = a[i-1]+k-a[i];
        if(answer > y) {
          answer = y;
        }
      }
      int x = a[n-1]-a[0];
      if(answer > x) {
        answer = x;
      }
    }

    System.out.println(answer);
  }
}