import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    int[] A = new int[N];

    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    int max = 0 ;

    for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      }
    int tmp2 = (K - A[N-1]) + A[0];
    if(tmp2 > max){
      max = tmp2;
    }
    System.out.println(K - max);   
    return;
  }
}
 import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    int[] A = new int[N];

    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    int max = 0 ;

    for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      }
    int tmp2 = (K - A[N-1]) + A[0];
    if(tmp2 > max){
      max = tmp2;
    }
    System.out.println(K - max);   
    return;
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    int[] A = new int[N];

    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    int max = 0 ;

    for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      }
    int tmp2 = (K - A[N-1]) + A[0];
    if(tmp2 > max){
      max = tmp2;
    }
    System.out.println(K - max);   
    return;
  } main {
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    int[] A = new int[N];

    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    int max = 0 ;

    for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      }
    int tmp2 = (K - A[N-1]) + A[0];
    if(tmp2 > max){
      max = tmp2;
    }
    System.out.println(K - max);   
    return;
  } 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[] A = new int[N]; A new int[N] N for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      A[i] = sc.nextInt();
    } {
      A[i] = sc.nextInt();
    } A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc int max = 0 ; max 0 for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      } int i = 0; int i = 0; i 0 i < N -1 i N -1 N 1 i++ i++ i {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      } {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      } int tmp = (A[i +1] - A[i]); tmp (A[i +1] - A[i]) A[i +1] A i +1 i 1 A[i] A i if(tmp >= max){
        max = tmp;
        } tmp >= max tmp max {
        max = tmp;
        } max = tmp max tmp int tmp2 = (K - A[N-1]) + A[0]; tmp2 (K - A[N-1]) + A[0] (K - A[N-1]) K A[N-1] A N-1 N 1 A[0] A 0 if(tmp2 > max){
      max = tmp2;
    } tmp2 > max tmp2 max {
      max = tmp2;
    } max = tmp2 max tmp2 System.out.println(K - max) System.out.println System.out System System.out K - max K max return; String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    int[] A = new int[N];

    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    int max = 0 ;

    for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      }
    int tmp2 = (K - A[N-1]) + A[0];
    if(tmp2 > max){
      max = tmp2;
    }
    System.out.println(K - max);   
    return;
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    int[] A = new int[N];

    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    int max = 0 ;

    for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      }
    int tmp2 = (K - A[N-1]) + A[0];
    if(tmp2 > max){
      max = tmp2;
    }
    System.out.println(K - max);   
    return;
  }
} Main
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int N = sc.nextInt();
    int[] A = new int[N];

    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    int max = 0 ;

    for (int i = 0; i < N -1 ; i++) {
      int tmp = (A[i +1] - A[i]);
      // System.out.println(A[i]); 
      if(tmp >= max){
        max = tmp;
        }
      // System.out.println(max); 
      }
    int tmp2 = (K - A[N-1]) + A[0];
    if(tmp2 > max){
      max = tmp2;
    }
    System.out.println(K - max);   
    return;
  }
}

