import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int K = Integer.parseInt(scan.next());
        int N = Integer.parseInt(scan.next());
        int min = 0;

        List<Integer> A = new ArrayList<>();
        List<Integer> dist = new ArrayList<>();

        for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        }

        scan.close();
        
        for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        }

        Collections.sort(dist);
        //System.out.println(dist);

        for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        }
        System.out.println(min);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int K = Integer.parseInt(scan.next());
        int N = Integer.parseInt(scan.next());
        int min = 0;

        List<Integer> A = new ArrayList<>();
        List<Integer> dist = new ArrayList<>();

        for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        }

        scan.close();
        
        for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        }

        Collections.sort(dist);
        //System.out.println(dist);

        for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        }
        System.out.println(min);
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int K = Integer.parseInt(scan.next());
        int N = Integer.parseInt(scan.next());
        int min = 0;

        List<Integer> A = new ArrayList<>();
        List<Integer> dist = new ArrayList<>();

        for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        }

        scan.close();
        
        for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        }

        Collections.sort(dist);
        //System.out.println(dist);

        for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        }
        System.out.println(min);
    } main {
        Scanner scan = new Scanner(System.in);

        int K = Integer.parseInt(scan.next());
        int N = Integer.parseInt(scan.next());
        int min = 0;

        List<Integer> A = new ArrayList<>();
        List<Integer> dist = new ArrayList<>();

        for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        }

        scan.close();
        
        for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        }

        Collections.sort(dist);
        //System.out.println(dist);

        for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        }
        System.out.println(min);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int K = Integer.parseInt(scan.next()); K Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int N = Integer.parseInt(scan.next()); N Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int min = 0; min 0 List<Integer> A = new ArrayList<>(); A new ArrayList<>() List<Integer> dist = new ArrayList<>(); dist new ArrayList<>() for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            A.add(Integer.parseInt(scan.next()));
        } {
            A.add(Integer.parseInt(scan.next()));
        } A.add(Integer.parseInt(scan.next())) A.add A Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan scan.close() scan.close scan for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        } {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        } if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            } i<N-1 i N-1 N 1 {
                dist.add(A.get(i+1) - A.get(i));
            } dist.add(A.get(i+1) - A.get(i)) dist.add dist A.get(i+1) - A.get(i) A.get(i+1) A.get A i+1 i 1 A.get(i) A.get A i {
                dist.add(K-A.get(i) + A.get(0));
            } dist.add(K-A.get(i) + A.get(0)) dist.add dist K-A.get(i) + A.get(0) K-A.get(i) K A.get(i) A.get A i A.get(0) A.get A 0 Collections.sort(dist) Collections.sort Collections dist for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
            min += dist.get(i);
        } {
            min += dist.get(i);
        } min += dist.get(i) min dist.get(i) dist.get dist i System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int K = Integer.parseInt(scan.next());
        int N = Integer.parseInt(scan.next());
        int min = 0;

        List<Integer> A = new ArrayList<>();
        List<Integer> dist = new ArrayList<>();

        for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        }

        scan.close();
        
        for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        }

        Collections.sort(dist);
        //System.out.println(dist);

        for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        }
        System.out.println(min);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int K = Integer.parseInt(scan.next());
        int N = Integer.parseInt(scan.next());
        int min = 0;

        List<Integer> A = new ArrayList<>();
        List<Integer> dist = new ArrayList<>();

        for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        }

        scan.close();
        
        for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        }

        Collections.sort(dist);
        //System.out.println(dist);

        for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        }
        System.out.println(min);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int K = Integer.parseInt(scan.next());
        int N = Integer.parseInt(scan.next());
        int min = 0;

        List<Integer> A = new ArrayList<>();
        List<Integer> dist = new ArrayList<>();

        for(int i=0; i<N; i++)
        {
            A.add(Integer.parseInt(scan.next()));
        }

        scan.close();
        
        for(int i=0; i<N; i++)
        {
            if(i<N-1)
            {
                dist.add(A.get(i+1) - A.get(i));
            }else{
                dist.add(K-A.get(i) + A.get(0));
            }
        }

        Collections.sort(dist);
        //System.out.println(dist);

        for(int i=0; i<N-1; i++)
        {
            min += dist.get(i);
        }
        System.out.println(min);
    }
}