/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int K = Integer.parseInt(st.nextToken());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //sort(arr);
         int res = K+arr[0]-arr[N-1];
         for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]);
         System.out.println(K-res);
      }
      public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      }
   } import java.util.*; util.* java import java.io.*; io.* java import java.math.*; math.* java public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int K = Integer.parseInt(st.nextToken());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //sort(arr);
         int res = K+arr[0]-arr[N-1];
         for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]);
         System.out.println(K-res);
      }
      public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      }
   } Main public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int K = Integer.parseInt(st.nextToken());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //sort(arr);
         int res = K+arr[0]-arr[N-1];
         for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]);
         System.out.println(K-res);
      } main {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int K = Integer.parseInt(st.nextToken());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //sort(arr);
         int res = K+arr[0]-arr[N-1];
         for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]);
         System.out.println(K-res);
      } BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); infile new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(infile.readLine()); st new StringTokenizer(infile.readLine()) int K = Integer.parseInt(st.nextToken()); K Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int N = Integer.parseInt(st.nextToken()); N Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int[] arr = new int[N]; arr new int[N] N st = new StringTokenizer(infile.readLine()) st new StringTokenizer(infile.readLine()) for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken()); int i=0; int i=0; i 0 i < N i N i++ i++ i arr[i] = Integer.parseInt(st.nextToken()); arr[i] = Integer.parseInt(st.nextToken()) arr[i] arr i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int res = K+arr[0]-arr[N-1]; res K+arr[0]-arr[N-1] K+arr[0] K arr[0] arr 0 arr[N-1] arr N-1 N 1 for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]); int i=0; int i=0; i 0 i < N-1 i N-1 N 1 i++ i++ i res = Math.max(res, arr[i+1]-arr[i]); res = Math.max(res, arr[i+1]-arr[i]) res Math.max(res, arr[i+1]-arr[i]) Math.max Math res arr[i+1]-arr[i] arr[i+1] arr i+1 i 1 arr[i] arr i System.out.println(K-res) System.out.println System.out System System.out K-res K res String omkar[] omkar public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      } sort {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      } PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); pq new PriorityQueue<Integer>() for(int a: arr)
            pq.add(a); int a arr pq.add(a); pq.add(a) pq.add pq a for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll(); int i=0; int i=0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i arr[i] = pq.poll(); arr[i] = pq.poll() arr[i] arr i pq.poll() pq.poll pq int[] arr arr public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int K = Integer.parseInt(st.nextToken());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //sort(arr);
         int res = K+arr[0]-arr[N-1];
         for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]);
         System.out.println(K-res);
      }
      public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      }
   } public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int K = Integer.parseInt(st.nextToken());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //sort(arr);
         int res = K+arr[0]-arr[N-1];
         for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]);
         System.out.println(K-res);
      }
      public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      }
   } Main
/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         int K = Integer.parseInt(st.nextToken());
         int N = Integer.parseInt(st.nextToken());
         int[] arr = new int[N];
         st = new StringTokenizer(infile.readLine());
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(st.nextToken());
         //sort(arr);
         int res = K+arr[0]-arr[N-1];
         for(int i=0; i < N-1; i++)
            res = Math.max(res, arr[i+1]-arr[i]);
         System.out.println(K-res);
      }
      public static void sort(int[] arr)
      {
         //stable heap sort
         PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
         for(int a: arr)
            pq.add(a);
         for(int i=0; i < arr.length; i++)
            arr[i] = pq.poll();
      }
   }