import java.io.*;

class Main{
	public static void main(String []args)throws IOException{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int K, N, i, j, minDist = Integer.MAX_VALUE;
      String str = br.readLine();
      
      K = Integer.parseInt(str.substring(0, str.indexOf(" ")));
      N = Integer.parseInt(str.substring(str.indexOf(" ") + 1));
      
      int A[] = new int[N];
      str = br.readLine();
      String s[] = str.split(" ");
      
      for(i = 0; i < s.length; i++)
      {
        A[i] = Integer.parseInt(s[i]);
      }
      int dist[] = new int[N];
      int max = 0;
      for(i = 0; i < N - 1; i++)
      {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      }
      dist[i] = K - A[N - 1] + A[0];
      if(dist[i] > dist[max])
        max = i;
      minDist = 0;
      for(i = 0; i < N; i++)
      {
          minDist = minDist + dist[i];
      }
      System.out.println(minDist - dist[max]);
    }
} import java.io.*; io.* java class Main{
	public static void main(String []args)throws IOException{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int K, N, i, j, minDist = Integer.MAX_VALUE;
      String str = br.readLine();
      
      K = Integer.parseInt(str.substring(0, str.indexOf(" ")));
      N = Integer.parseInt(str.substring(str.indexOf(" ") + 1));
      
      int A[] = new int[N];
      str = br.readLine();
      String s[] = str.split(" ");
      
      for(i = 0; i < s.length; i++)
      {
        A[i] = Integer.parseInt(s[i]);
      }
      int dist[] = new int[N];
      int max = 0;
      for(i = 0; i < N - 1; i++)
      {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      }
      dist[i] = K - A[N - 1] + A[0];
      if(dist[i] > dist[max])
        max = i;
      minDist = 0;
      for(i = 0; i < N; i++)
      {
          minDist = minDist + dist[i];
      }
      System.out.println(minDist - dist[max]);
    }
} Main public static void main(String []args)throws IOException{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int K, N, i, j, minDist = Integer.MAX_VALUE;
      String str = br.readLine();
      
      K = Integer.parseInt(str.substring(0, str.indexOf(" ")));
      N = Integer.parseInt(str.substring(str.indexOf(" ") + 1));
      
      int A[] = new int[N];
      str = br.readLine();
      String s[] = str.split(" ");
      
      for(i = 0; i < s.length; i++)
      {
        A[i] = Integer.parseInt(s[i]);
      }
      int dist[] = new int[N];
      int max = 0;
      for(i = 0; i < N - 1; i++)
      {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      }
      dist[i] = K - A[N - 1] + A[0];
      if(dist[i] > dist[max])
        max = i;
      minDist = 0;
      for(i = 0; i < N; i++)
      {
          minDist = minDist + dist[i];
      }
      System.out.println(minDist - dist[max]);
    } main {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int K, N, i, j, minDist = Integer.MAX_VALUE;
      String str = br.readLine();
      
      K = Integer.parseInt(str.substring(0, str.indexOf(" ")));
      N = Integer.parseInt(str.substring(str.indexOf(" ") + 1));
      
      int A[] = new int[N];
      str = br.readLine();
      String s[] = str.split(" ");
      
      for(i = 0; i < s.length; i++)
      {
        A[i] = Integer.parseInt(s[i]);
      }
      int dist[] = new int[N];
      int max = 0;
      for(i = 0; i < N - 1; i++)
      {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      }
      dist[i] = K - A[N - 1] + A[0];
      if(dist[i] > dist[max])
        max = i;
      minDist = 0;
      for(i = 0; i < N; i++)
      {
          minDist = minDist + dist[i];
      }
      System.out.println(minDist - dist[max]);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int K K N N i i j j minDist = Integer.MAX_VALUE; minDist Integer.MAX_VALUE Integer Integer.MAX_VALUE String str = br.readLine(); str br.readLine() br.readLine br K = Integer.parseInt(str.substring(0, str.indexOf(" "))) K Integer.parseInt(str.substring(0, str.indexOf(" "))) Integer.parseInt Integer str.substring(0, str.indexOf(" ")) str.substring str 0 str.indexOf(" ") str.indexOf str " " N = Integer.parseInt(str.substring(str.indexOf(" ") + 1)) N Integer.parseInt(str.substring(str.indexOf(" ") + 1)) Integer.parseInt Integer str.substring(str.indexOf(" ") + 1) str.substring str str.indexOf(" ") + 1 str.indexOf(" ") str.indexOf str " " 1 int A[] = new int[N]; A new int[N] N str = br.readLine() str br.readLine() br.readLine br String s[] = str.split(" "); s str.split(" ") str.split str " " for(i = 0; i < s.length; i++)
      {
        A[i] = Integer.parseInt(s[i]);
      } i = 0; i = 0 i 0 i < s.length i s.length s s.length i++ i++ i {
        A[i] = Integer.parseInt(s[i]);
      } {
        A[i] = Integer.parseInt(s[i]);
      } A[i] = Integer.parseInt(s[i]) A[i] A i Integer.parseInt(s[i]) Integer.parseInt Integer s[i] s i int dist[] = new int[N]; dist new int[N] N int max = 0; max 0 for(i = 0; i < N - 1; i++)
      {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      } i = 0; i = 0 i 0 i < N - 1 i N - 1 N 1 i++ i++ i {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      } {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      } dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]) dist[i] dist i Math.abs(A[(i + 1) % N] - A[i % N]) Math.abs Math A[(i + 1) % N] - A[i % N] A[(i + 1) % N] A (i + 1) % N (i + 1) i 1 N A[i % N] A i % N i N if(dist[i] > dist[max])
          max = i; dist[i] > dist[max] dist[i] dist i dist[max] dist max max = i max i dist[i] = K - A[N - 1] + A[0] dist[i] dist i K - A[N - 1] + A[0] K - A[N - 1] K A[N - 1] A N - 1 N 1 A[0] A 0 if(dist[i] > dist[max])
        max = i; dist[i] > dist[max] dist[i] dist i dist[max] dist max max = i max i minDist = 0 minDist 0 for(i = 0; i < N; i++)
      {
          minDist = minDist + dist[i];
      } i = 0; i = 0 i 0 i < N i N i++ i++ i {
          minDist = minDist + dist[i];
      } {
          minDist = minDist + dist[i];
      } minDist = minDist + dist[i] minDist minDist + dist[i] minDist dist[i] dist i System.out.println(minDist - dist[max]) System.out.println System.out System System.out minDist - dist[max] minDist dist[max] dist max String []args args
import java.io.*;

class Main{
	public static void main(String []args)throws IOException{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int K, N, i, j, minDist = Integer.MAX_VALUE;
      String str = br.readLine();
      
      K = Integer.parseInt(str.substring(0, str.indexOf(" ")));
      N = Integer.parseInt(str.substring(str.indexOf(" ") + 1));
      
      int A[] = new int[N];
      str = br.readLine();
      String s[] = str.split(" ");
      
      for(i = 0; i < s.length; i++)
      {
        A[i] = Integer.parseInt(s[i]);
      }
      int dist[] = new int[N];
      int max = 0;
      for(i = 0; i < N - 1; i++)
      {
        	dist[i] = Math.abs(A[(i + 1) % N] - A[i % N]);
        if(dist[i] > dist[max])
          max = i;
      }
      dist[i] = K - A[N - 1] + A[0];
      if(dist[i] > dist[max])
        max = i;
      minDist = 0;
      for(i = 0; i < N; i++)
      {
          minDist = minDist + dist[i];
      }
      System.out.println(minDist - dist[max]);
    }
}