import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc  = new Scanner(System.in);
        
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int []A = new int[N];
        int []leng = new int[N];
        
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        }
        
        Arrays.sort(leng);
        
        int total = 0;
        for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        }
        
        System.out.println(total);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc  = new Scanner(System.in);
        
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int []A = new int[N];
        int []leng = new int[N];
        
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        }
        
        Arrays.sort(leng);
        
        int total = 0;
        for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        }
        
        System.out.println(total);
    }
} Main public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc  = new Scanner(System.in);
        
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int []A = new int[N];
        int []leng = new int[N];
        
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        }
        
        Arrays.sort(leng);
        
        int total = 0;
        for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        }
        
        System.out.println(total);
    } main {
        // Your code here!
        Scanner sc  = new Scanner(System.in);
        
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int []A = new int[N];
        int []leng = new int[N];
        
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        }
        
        Arrays.sort(leng);
        
        int total = 0;
        for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        }
        
        System.out.println(total);
    } 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 int []leng = new int[N]; leng 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 for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        } {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        } if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            } (i+1) == N (i+1) i 1 N {
                leng[i] = A[0] + (K - A[i]); 
                break;
            } leng[i] = A[0] + (K - A[i]) leng[i] leng i A[0] + (K - A[i]) A[0] A 0 (K - A[i]) K A[i] A i break; leng[i] = A[i+1] - A[i] leng[i] leng i A[i+1] - A[i] A[i+1] A i+1 i 1 A[i] A i Arrays.sort(leng) Arrays.sort Arrays leng int total = 0; total 0 for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            total += leng[i];
        } {
            total += leng[i];
        } total += leng[i] total leng[i] leng i System.out.println(total) System.out.println System.out System System.out total String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc  = new Scanner(System.in);
        
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int []A = new int[N];
        int []leng = new int[N];
        
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        }
        
        Arrays.sort(leng);
        
        int total = 0;
        for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        }
        
        System.out.println(total);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc  = new Scanner(System.in);
        
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int []A = new int[N];
        int []leng = new int[N];
        
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        }
        
        Arrays.sort(leng);
        
        int total = 0;
        for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        }
        
        System.out.println(total);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc  = new Scanner(System.in);
        
        int K = sc.nextInt();
        int N = sc.nextInt();
        
        int []A = new int[N];
        int []leng = new int[N];
        
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.nextInt();
        }
        
        for (int i = 0; i < N; i++)
        {
            if ((i+1) == N)
            {
                leng[i] = A[0] + (K - A[i]); 
                break;
            }
            
            leng[i] = A[i+1] - A[i];
        }
        
        Arrays.sort(leng);
        
        int total = 0;
        for (int i = 0; i < N-1; i++)
        {
            total += leng[i];
        }
        
        System.out.println(total);
    }
}
