import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.lang.Integer;
import java.util.HashMap;

public class Main {
    // driver function to test the above functions
    public static void main(String args[]) throws IOException {
        Reader.init(System.in);

        int k = Reader.nextInt();
        int n = Reader.nextInt();
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        }

        Collections.sort(lis);
        int ans=0;
        int prev = lis.get(0);
        int curr = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        }
        int ans_min=Integer.MAX_VALUE;
        for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        }

        System.out.println(ans_min);


    }
}

class Node{
    char val;
    Node nxt = null;
    Node prev=null;


}



class solu{

}

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }

    static String line() throws IOException {
        return reader.readLine();
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.InputStreamReader; InputStreamReader java.io import java.lang.reflect.Array; Array java.lang.reflect import java.util.StringTokenizer; StringTokenizer java.util import java.util.Scanner; Scanner java.util import java.lang.*; lang.* java import java.io.*; io.* java import java.util.*; util.* java import java.lang.Integer; Integer java.lang import java.util.HashMap; HashMap java.util public class Main {
    // driver function to test the above functions
    public static void main(String args[]) throws IOException {
        Reader.init(System.in);

        int k = Reader.nextInt();
        int n = Reader.nextInt();
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        }

        Collections.sort(lis);
        int ans=0;
        int prev = lis.get(0);
        int curr = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        }
        int ans_min=Integer.MAX_VALUE;
        for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        }

        System.out.println(ans_min);


    }
} Main // driver function to test the above functions
    public static void main(String args[]) throws IOException {
        Reader.init(System.in);

        int k = Reader.nextInt();
        int n = Reader.nextInt();
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        }

        Collections.sort(lis);
        int ans=0;
        int prev = lis.get(0);
        int curr = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        }
        int ans_min=Integer.MAX_VALUE;
        for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        }

        System.out.println(ans_min);


    } main {
        Reader.init(System.in);

        int k = Reader.nextInt();
        int n = Reader.nextInt();
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        }

        Collections.sort(lis);
        int ans=0;
        int prev = lis.get(0);
        int curr = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        }
        int ans_min=Integer.MAX_VALUE;
        for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        }

        System.out.println(ans_min);


    } Reader.init(System.in) Reader.init Reader System.in System System.in int k = Reader.nextInt(); k Reader.nextInt() Reader.nextInt Reader int n = Reader.nextInt(); n Reader.nextInt() Reader.nextInt Reader ArrayList<Integer> lis = new ArrayList<Integer>(); lis new ArrayList<Integer>() for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        } {
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        } int val = Reader.nextInt(); val Reader.nextInt() Reader.nextInt Reader lis.add(val) lis.add lis val if (val!=0){
                lis.add(val-k);
            } val!=0 val 0 {
                lis.add(val-k);
            } lis.add(val-k) lis.add lis val-k val k Collections.sort(lis) Collections.sort Collections lis int ans=0; ans 0 int prev = lis.get(0); prev lis.get(0) lis.get lis 0 int curr = 0; curr 0 ArrayList<Integer> arr = new ArrayList<Integer>(); arr new ArrayList<Integer>() for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        } int i=1; int i=1; i 1 i<lis.size() i lis.size() lis.size lis i++ i++ i {
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        } {
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        } curr = lis.get(i) curr lis.get(i) lis.get lis i arr.add(Math.abs(curr-prev)) arr.add arr Math.abs(curr-prev) Math.abs Math curr-prev curr prev prev = curr prev curr int ans_min=Integer.MAX_VALUE; ans_min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        } int i=0; int i=0; i 0 i<arr.size() i arr.size() arr.size arr i++ i++ i {
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        } {
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        } if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            } i<n-1 i n-1 n 1 {
                ans=ans+arr.get(i);
            } ans=ans+arr.get(i) ans ans+arr.get(i) ans arr.get(i) arr.get arr i {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            } int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i); temp_1 ans - (arr.get(i-(n-1))) + arr.get(i) ans - (arr.get(i-(n-1))) ans (arr.get(i-(n-1))) arr.get arr i-(n-1) i (n-1) n 1 arr.get(i) arr.get arr i if (temp_1<ans_min){
                    ans_min=temp_1;
                } temp_1<ans_min temp_1 ans_min {
                    ans_min=temp_1;
                } ans_min=temp_1 ans_min temp_1 ans=temp_1 ans temp_1 System.out.println(ans_min) System.out.println System.out System System.out ans_min String args[] args class Node{
    char val;
    Node nxt = null;
    Node prev=null;


} Node char val; val Node nxt = null; nxt null Node prev=null; prev null class solu{

} solu class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }

    static String line() throws IOException {
        return reader.readLine();
    }
} Reader static BufferedReader reader; reader static StringTokenizer tokenizer; tokenizer /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    } init {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    } reader = new BufferedReader(
                new InputStreamReader(input) ) reader new BufferedReader(
                new InputStreamReader(input) ) tokenizer = new StringTokenizer("") tokenizer new StringTokenizer("") InputStream input input /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    } next {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    } while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        } ! tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        } tokenizer = new StringTokenizer(
                    reader.readLine() ) tokenizer new StringTokenizer(
                    reader.readLine() ) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    } nextInt {
        return Integer.parseInt( next() );
    } return Integer.parseInt( next() ); Integer.parseInt( next() ) Integer.parseInt Integer next() next static long nextLong() throws IOException {
        return Long.parseLong( next() );
    } nextLong {
        return Long.parseLong( next() );
    } return Long.parseLong( next() ); Long.parseLong( next() ) Long.parseLong Long next() next static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    } nextDouble {
        return Double.parseDouble( next() );
    } return Double.parseDouble( next() ); Double.parseDouble( next() ) Double.parseDouble Double next() next static String line() throws IOException {
        return reader.readLine();
    } line {
        return reader.readLine();
    } return reader.readLine(); reader.readLine() reader.readLine reader public class Main {
    // driver function to test the above functions
    public static void main(String args[]) throws IOException {
        Reader.init(System.in);

        int k = Reader.nextInt();
        int n = Reader.nextInt();
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        }

        Collections.sort(lis);
        int ans=0;
        int prev = lis.get(0);
        int curr = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        }
        int ans_min=Integer.MAX_VALUE;
        for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        }

        System.out.println(ans_min);


    }
} public class Main {
    // driver function to test the above functions
    public static void main(String args[]) throws IOException {
        Reader.init(System.in);

        int k = Reader.nextInt();
        int n = Reader.nextInt();
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        }

        Collections.sort(lis);
        int ans=0;
        int prev = lis.get(0);
        int curr = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        }
        int ans_min=Integer.MAX_VALUE;
        for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        }

        System.out.println(ans_min);


    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.lang.Integer;
import java.util.HashMap;

public class Main {
    // driver function to test the above functions
    public static void main(String args[]) throws IOException {
        Reader.init(System.in);

        int k = Reader.nextInt();
        int n = Reader.nextInt();
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int i=0; i<n; i++){
            int val = Reader.nextInt();
            lis.add(val);
            if (val!=0){
                lis.add(val-k);
            }

        }

        Collections.sort(lis);
        int ans=0;
        int prev = lis.get(0);
        int curr = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i=1; i<lis.size(); i++){
            curr = lis.get(i);
            arr.add(Math.abs(curr-prev));
            prev = curr;
        }
        int ans_min=Integer.MAX_VALUE;
        for (int i=0; i<arr.size(); i++){
            if (i<n-1){
                ans=ans+arr.get(i);
            }
            else {
                int temp_1 = ans - (arr.get(i-(n-1))) + arr.get(i);
                if (temp_1<ans_min){
                    ans_min=temp_1;
                }
                ans=temp_1;
            }
        }

        System.out.println(ans_min);


    }
}

class Node{
    char val;
    Node nxt = null;
    Node prev=null;


}



class solu{

}

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }

    static String line() throws IOException {
        return reader.readLine();
    }
}