import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int K = scan.nextInt();
    int N = scan.nextInt();
    int[] ie = new int[N];
    for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    }
   int x,y,z,sa;
    z = 0;
    for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    }
    x = ie[N-1];
    y = ie[0];
    sa = K - x + y;
    if(sa > z){
      z = sa;
    }
    System.out.println(K-z);
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int K = scan.nextInt();
    int N = scan.nextInt();
    int[] ie = new int[N];
    for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    }
   int x,y,z,sa;
    z = 0;
    for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    }
    x = ie[N-1];
    y = ie[0];
    sa = K - x + y;
    if(sa > z){
      z = sa;
    }
    System.out.println(K-z);
  }
} Main public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int K = scan.nextInt();
    int N = scan.nextInt();
    int[] ie = new int[N];
    for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    }
   int x,y,z,sa;
    z = 0;
    for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    }
    x = ie[N-1];
    y = ie[0];
    sa = K - x + y;
    if(sa > z){
      z = sa;
    }
    System.out.println(K-z);
  } main {
    Scanner scan = new Scanner(System.in);
    int K = scan.nextInt();
    int N = scan.nextInt();
    int[] ie = new int[N];
    for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    }
   int x,y,z,sa;
    z = 0;
    for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    }
    x = ie[N-1];
    y = ie[0];
    sa = K - x + y;
    if(sa > z){
      z = sa;
    }
    System.out.println(K-z);
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan int[] ie = new int[N]; ie new int[N] N for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      ie[i] = scan.nextInt();
    } {
      ie[i] = scan.nextInt();
    } ie[i] = scan.nextInt() ie[i] ie i scan.nextInt() scan.nextInt scan int x x y y z z sa; sa z = 0 z 0 for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    } int i = 0; int i = 0; i 0 i < N - 1 i N - 1 N 1 i++ i++ i {
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    } {
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    } x = ie[i] x ie[i] ie i y = ie[i+1] y ie[i+1] ie i+1 i 1 sa = y - x sa y - x y x if(sa > z){
         z = sa;
       } sa > z sa z {
         z = sa;
       } z = sa z sa x = ie[N-1] x ie[N-1] ie N-1 N 1 y = ie[0] y ie[0] ie 0 sa = K - x + y sa K - x + y K - x K x y if(sa > z){
      z = sa;
    } sa > z sa z {
      z = sa;
    } z = sa z sa System.out.println(K-z) System.out.println System.out System System.out K-z K z String[] args args public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int K = scan.nextInt();
    int N = scan.nextInt();
    int[] ie = new int[N];
    for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    }
   int x,y,z,sa;
    z = 0;
    for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    }
    x = ie[N-1];
    y = ie[0];
    sa = K - x + y;
    if(sa > z){
      z = sa;
    }
    System.out.println(K-z);
  }
} public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int K = scan.nextInt();
    int N = scan.nextInt();
    int[] ie = new int[N];
    for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    }
   int x,y,z,sa;
    z = 0;
    for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    }
    x = ie[N-1];
    y = ie[0];
    sa = K - x + y;
    if(sa > z){
      z = sa;
    }
    System.out.println(K-z);
  }
} Main
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int K = scan.nextInt();
    int N = scan.nextInt();
    int[] ie = new int[N];
    for(int i = 0;i < N;i++){
      ie[i] = scan.nextInt();
    }
   int x,y,z,sa;
    z = 0;
    for(int i = 0;i < N - 1;i++){
       x = ie[i];
       y = ie[i+1];
       sa = y - x;
       if(sa > z){
         z = sa;
       }
    }
    x = ie[N-1];
    y = ie[0];
    sa = K - x + y;
    if(sa > z){
      z = sa;
    }
    System.out.println(K-z);
  }
}
