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();
        Integer[]home = new Integer[N];
        for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        }
        
        int[]distance = new int[N];
        int ans=0;
        for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        }
        distance[N-1]=K-home[N-1]+home[0];
        ans+=distance[N-1];
        
        Arrays.sort(distance);
        ans-=distance[N-1];
        System.out.println(ans);
        
    }
} 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();
        Integer[]home = new Integer[N];
        for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        }
        
        int[]distance = new int[N];
        int ans=0;
        for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        }
        distance[N-1]=K-home[N-1]+home[0];
        ans+=distance[N-1];
        
        Arrays.sort(distance);
        ans-=distance[N-1];
        System.out.println(ans);
        
    }
} Main public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
    
        int K =sc.nextInt();
        int N = sc.nextInt();
        Integer[]home = new Integer[N];
        for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        }
        
        int[]distance = new int[N];
        int ans=0;
        for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        }
        distance[N-1]=K-home[N-1]+home[0];
        ans+=distance[N-1];
        
        Arrays.sort(distance);
        ans-=distance[N-1];
        System.out.println(ans);
        
    } main {
        Scanner sc = new Scanner(System.in);
    
        int K =sc.nextInt();
        int N = sc.nextInt();
        Integer[]home = new Integer[N];
        for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        }
        
        int[]distance = new int[N];
        int ans=0;
        for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        }
        distance[N-1]=K-home[N-1]+home[0];
        ans+=distance[N-1];
        
        Arrays.sort(distance);
        ans-=distance[N-1];
        System.out.println(ans);
        
    } 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 Integer[]home = new Integer[N]; home new Integer[N] N for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        } int i=0; int i=0; i 0 i<home.length i home.length home home.length i++ i++ i {
            home[i]=sc.nextInt();
        } {
            home[i]=sc.nextInt();
        } home[i]=sc.nextInt() home[i] home i sc.nextInt() sc.nextInt sc int[]distance = new int[N]; distance new int[N] N int ans=0; ans 0 for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        } {
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        } distance[i]=home[i+1]-home[i] distance[i] distance i home[i+1]-home[i] home[i+1] home i+1 i 1 home[i] home i ans+=distance[i] ans distance[i] distance i distance[N-1]=K-home[N-1]+home[0] distance[N-1] distance N-1 N 1 K-home[N-1]+home[0] K-home[N-1] K home[N-1] home N-1 N 1 home[0] home 0 ans+=distance[N-1] ans distance[N-1] distance N-1 N 1 Arrays.sort(distance) Arrays.sort Arrays distance ans-=distance[N-1] ans distance[N-1] distance N-1 N 1 System.out.println(ans) System.out.println System.out System System.out ans 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();
        Integer[]home = new Integer[N];
        for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        }
        
        int[]distance = new int[N];
        int ans=0;
        for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        }
        distance[N-1]=K-home[N-1]+home[0];
        ans+=distance[N-1];
        
        Arrays.sort(distance);
        ans-=distance[N-1];
        System.out.println(ans);
        
    }
} public class Main{
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
    
        int K =sc.nextInt();
        int N = sc.nextInt();
        Integer[]home = new Integer[N];
        for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        }
        
        int[]distance = new int[N];
        int ans=0;
        for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        }
        distance[N-1]=K-home[N-1]+home[0];
        ans+=distance[N-1];
        
        Arrays.sort(distance);
        ans-=distance[N-1];
        System.out.println(ans);
        
    }
} 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();
        Integer[]home = new Integer[N];
        for(int i=0;i<home.length;i++){
            home[i]=sc.nextInt();
        }
        
        int[]distance = new int[N];
        int ans=0;
        for(int i=0;i<N-1;i++){
            distance[i]=home[i+1]-home[i];
            ans+=distance[i];
        }
        distance[N-1]=K-home[N-1]+home[0];
        ans+=distance[N-1];
        
        Arrays.sort(distance);
        ans-=distance[N-1];
        System.out.println(ans);
        
    }
}