import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;


public class Main {

    static void solve() {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //arr[n] = arr[0];
        //long min = Long.MAX_VALUE;
        Arrays.sort(arr);
        int sum = 0;
        int maxDiff = 0;
        for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        }
        int finalDiff = k-arr[n-1]+arr[0];
        maxDiff = Math.max(maxDiff, finalDiff);
        sum+=k-arr[n-1] + arr[0];

        int res = sum - maxDiff;
        System.out.println(res);
    }


    public static void main(String[] args) {

        solve();

        //        Main m = new Main();
        //        Scanner scan = new Scanner(System.in);

        //        long n = scan.nextLong();
        //        long r = scan.nextLong();
        //        List<Long> list = new ArrayList<>();
        ////        int[] arr = new int[n];
        //        for (long i=0;i<n;i++){
        //            list.add(scan.nextLong());
        //        }
//            System.out.println(ch);
        //        solve(n, arr);
        //        scan.close();
    }


    class Counter extends HashMap<Integer, Integer> {
        public int get(int k) {
            return containsKey(k) ? super.get(k) : 0;
        }

        public void add(int k, int v) {
            put(k, get(k) + v);
        }
    }



    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

//    static class Node {
//        int key;
//        int data;
//        Node next;
//        Node prev;
//
//        Node(int key, int data) {
//            this.key = key;
//            this.data = data;
//        }
//    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Trie {
        void add(String word) {

        }

        boolean search(String w) {
            return false;
        }
    }

    static class Node1 {
        public int val;
        public List<Node1> children;

        public Node1() {
        }

        public Node1(int _val, List<Node1> _children) {
            val = _val;
            children = _children;
        }
    }

    ;

    static class TrieNode {

    }

    static class KvNode implements Comparable<KvNode> {
        String key;
        int freq;

        public KvNode(String key, int freq) {
            this.key = key;
            this.freq = freq;
        }

        @Override
        public int compareTo(KvNode o) {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        }
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
} import java.util.Arrays; Arrays java.util import java.util.HashMap; HashMap java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main {

    static void solve() {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //arr[n] = arr[0];
        //long min = Long.MAX_VALUE;
        Arrays.sort(arr);
        int sum = 0;
        int maxDiff = 0;
        for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        }
        int finalDiff = k-arr[n-1]+arr[0];
        maxDiff = Math.max(maxDiff, finalDiff);
        sum+=k-arr[n-1] + arr[0];

        int res = sum - maxDiff;
        System.out.println(res);
    }


    public static void main(String[] args) {

        solve();

        //        Main m = new Main();
        //        Scanner scan = new Scanner(System.in);

        //        long n = scan.nextLong();
        //        long r = scan.nextLong();
        //        List<Long> list = new ArrayList<>();
        ////        int[] arr = new int[n];
        //        for (long i=0;i<n;i++){
        //            list.add(scan.nextLong());
        //        }
//            System.out.println(ch);
        //        solve(n, arr);
        //        scan.close();
    }


    class Counter extends HashMap<Integer, Integer> {
        public int get(int k) {
            return containsKey(k) ? super.get(k) : 0;
        }

        public void add(int k, int v) {
            put(k, get(k) + v);
        }
    }



    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

//    static class Node {
//        int key;
//        int data;
//        Node next;
//        Node prev;
//
//        Node(int key, int data) {
//            this.key = key;
//            this.data = data;
//        }
//    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Trie {
        void add(String word) {

        }

        boolean search(String w) {
            return false;
        }
    }

    static class Node1 {
        public int val;
        public List<Node1> children;

        public Node1() {
        }

        public Node1(int _val, List<Node1> _children) {
            val = _val;
            children = _children;
        }
    }

    ;

    static class TrieNode {

    }

    static class KvNode implements Comparable<KvNode> {
        String key;
        int freq;

        public KvNode(String key, int freq) {
            this.key = key;
            this.freq = freq;
        }

        @Override
        public int compareTo(KvNode o) {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        }
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
} Main static void solve() {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //arr[n] = arr[0];
        //long min = Long.MAX_VALUE;
        Arrays.sort(arr);
        int sum = 0;
        int maxDiff = 0;
        for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        }
        int finalDiff = k-arr[n-1]+arr[0];
        maxDiff = Math.max(maxDiff, finalDiff);
        sum+=k-arr[n-1] + arr[0];

        int res = sum - maxDiff;
        System.out.println(res);
    } solve {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //arr[n] = arr[0];
        //long min = Long.MAX_VALUE;
        Arrays.sort(arr);
        int sum = 0;
        int maxDiff = 0;
        for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        }
        int finalDiff = k-arr[n-1]+arr[0];
        maxDiff = Math.max(maxDiff, finalDiff);
        sum+=k-arr[n-1] + arr[0];

        int res = sum - maxDiff;
        System.out.println(res);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int[] arr = new int[n]; arr new int[n] n for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            arr[i] = scan.nextInt();
        } {
            arr[i] = scan.nextInt();
        } arr[i] = scan.nextInt() arr[i] arr i scan.nextInt() scan.nextInt scan Arrays.sort(arr) Arrays.sort Arrays arr int sum = 0; sum 0 int maxDiff = 0; maxDiff 0 for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        } {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        } int diff = arr[i]-arr[i-1]; diff arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 maxDiff = Math.max(maxDiff, diff) maxDiff Math.max(maxDiff, diff) Math.max Math maxDiff diff sum += diff sum diff int finalDiff = k-arr[n-1]+arr[0]; finalDiff k-arr[n-1]+arr[0] k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 maxDiff = Math.max(maxDiff, finalDiff) maxDiff Math.max(maxDiff, finalDiff) Math.max Math maxDiff finalDiff sum+=k-arr[n-1] + arr[0] sum k-arr[n-1] + arr[0] k-arr[n-1] k arr[n-1] arr n-1 n 1 arr[0] arr 0 int res = sum - maxDiff; res sum - maxDiff sum maxDiff System.out.println(res) System.out.println System.out System System.out res public static void main(String[] args) {

        solve();

        //        Main m = new Main();
        //        Scanner scan = new Scanner(System.in);

        //        long n = scan.nextLong();
        //        long r = scan.nextLong();
        //        List<Long> list = new ArrayList<>();
        ////        int[] arr = new int[n];
        //        for (long i=0;i<n;i++){
        //            list.add(scan.nextLong());
        //        }
//            System.out.println(ch);
        //        solve(n, arr);
        //        scan.close();
    } main {

        solve();

        //        Main m = new Main();
        //        Scanner scan = new Scanner(System.in);

        //        long n = scan.nextLong();
        //        long r = scan.nextLong();
        //        List<Long> list = new ArrayList<>();
        ////        int[] arr = new int[n];
        //        for (long i=0;i<n;i++){
        //            list.add(scan.nextLong());
        //        }
//            System.out.println(ch);
        //        solve(n, arr);
        //        scan.close();
    } solve() solve String[] args args class Counter extends HashMap<Integer, Integer> {
        public int get(int k) {
            return containsKey(k) ? super.get(k) : 0;
        }

        public void add(int k, int v) {
            put(k, get(k) + v);
        }
    } Counter public int get(int k) {
            return containsKey(k) ? super.get(k) : 0;
        } get {
            return containsKey(k) ? super.get(k) : 0;
        } return containsKey(k) ? super.get(k) : 0; containsKey(k) ? super.get(k) : 0 containsKey(k) containsKey k super.get(k) super.get super k 0 int k k public void add(int k, int v) {
            put(k, get(k) + v);
        } add {
            put(k, get(k) + v);
        } put(k, get(k) + v) put k get(k) + v get(k) get k v int k k int v v HashMap static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    } Node int val; val Node next; next Node random; random public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        } Node {
            this.val = val;
            this.next = null;
            this.random = null;
        } this.val = val this.val this this.val val this.next = null this.next this this.next null this.random = null this.random this this.random null int val val static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    } TreeNode int val; val TreeNode left; left TreeNode right; right TreeNode(int x) {
            val = x;
        } TreeNode {
            val = x;
        } val = x val x int x x static class Trie {
        void add(String word) {

        }

        boolean search(String w) {
            return false;
        }
    } Trie void add(String word) {

        } add {

        } String word word boolean search(String w) {
            return false;
        } search {
            return false;
        } return false; false String w w static class Node1 {
        public int val;
        public List<Node1> children;

        public Node1() {
        }

        public Node1(int _val, List<Node1> _children) {
            val = _val;
            children = _children;
        }
    } Node1 public int val; val public List<Node1> children; children public Node1() {
        } Node1 {
        } public Node1(int _val, List<Node1> _children) {
            val = _val;
            children = _children;
        } Node1 {
            val = _val;
            children = _children;
        } val = _val val _val children = _children children _children int _val _val List<Node1> _children _children static class TrieNode {

    } TrieNode static class KvNode implements Comparable<KvNode> {
        String key;
        int freq;

        public KvNode(String key, int freq) {
            this.key = key;
            this.freq = freq;
        }

        @Override
        public int compareTo(KvNode o) {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        }
    } KvNode String key; key int freq; freq public KvNode(String key, int freq) {
            this.key = key;
            this.freq = freq;
        } KvNode {
            this.key = key;
            this.freq = freq;
        } this.key = key this.key this this.key key this.freq = freq this.freq this this.freq freq String key key int freq freq @Override
        public int compareTo(KvNode o) {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        } compareTo {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        } if (this.key.equals(o.key)) {
                return 0;
            } this.key.equals(o.key) this.key.equals this.key this this.key o.key o o.key {
                return 0;
            } return 0; 0 return Integer.compare(o.freq, this.freq); Integer.compare(o.freq, this.freq) Integer.compare Integer o.freq o o.freq this.freq this this.freq KvNode o o Comparable static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    } ListNode int val; val ListNode next; next ListNode(int x) {
            val = x;
        } ListNode {
            val = x;
        } val = x val x int x x public class Main {

    static void solve() {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //arr[n] = arr[0];
        //long min = Long.MAX_VALUE;
        Arrays.sort(arr);
        int sum = 0;
        int maxDiff = 0;
        for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        }
        int finalDiff = k-arr[n-1]+arr[0];
        maxDiff = Math.max(maxDiff, finalDiff);
        sum+=k-arr[n-1] + arr[0];

        int res = sum - maxDiff;
        System.out.println(res);
    }


    public static void main(String[] args) {

        solve();

        //        Main m = new Main();
        //        Scanner scan = new Scanner(System.in);

        //        long n = scan.nextLong();
        //        long r = scan.nextLong();
        //        List<Long> list = new ArrayList<>();
        ////        int[] arr = new int[n];
        //        for (long i=0;i<n;i++){
        //            list.add(scan.nextLong());
        //        }
//            System.out.println(ch);
        //        solve(n, arr);
        //        scan.close();
    }


    class Counter extends HashMap<Integer, Integer> {
        public int get(int k) {
            return containsKey(k) ? super.get(k) : 0;
        }

        public void add(int k, int v) {
            put(k, get(k) + v);
        }
    }



    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

//    static class Node {
//        int key;
//        int data;
//        Node next;
//        Node prev;
//
//        Node(int key, int data) {
//            this.key = key;
//            this.data = data;
//        }
//    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Trie {
        void add(String word) {

        }

        boolean search(String w) {
            return false;
        }
    }

    static class Node1 {
        public int val;
        public List<Node1> children;

        public Node1() {
        }

        public Node1(int _val, List<Node1> _children) {
            val = _val;
            children = _children;
        }
    }

    ;

    static class TrieNode {

    }

    static class KvNode implements Comparable<KvNode> {
        String key;
        int freq;

        public KvNode(String key, int freq) {
            this.key = key;
            this.freq = freq;
        }

        @Override
        public int compareTo(KvNode o) {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        }
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
} public class Main {

    static void solve() {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //arr[n] = arr[0];
        //long min = Long.MAX_VALUE;
        Arrays.sort(arr);
        int sum = 0;
        int maxDiff = 0;
        for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        }
        int finalDiff = k-arr[n-1]+arr[0];
        maxDiff = Math.max(maxDiff, finalDiff);
        sum+=k-arr[n-1] + arr[0];

        int res = sum - maxDiff;
        System.out.println(res);
    }


    public static void main(String[] args) {

        solve();

        //        Main m = new Main();
        //        Scanner scan = new Scanner(System.in);

        //        long n = scan.nextLong();
        //        long r = scan.nextLong();
        //        List<Long> list = new ArrayList<>();
        ////        int[] arr = new int[n];
        //        for (long i=0;i<n;i++){
        //            list.add(scan.nextLong());
        //        }
//            System.out.println(ch);
        //        solve(n, arr);
        //        scan.close();
    }


    class Counter extends HashMap<Integer, Integer> {
        public int get(int k) {
            return containsKey(k) ? super.get(k) : 0;
        }

        public void add(int k, int v) {
            put(k, get(k) + v);
        }
    }



    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

//    static class Node {
//        int key;
//        int data;
//        Node next;
//        Node prev;
//
//        Node(int key, int data) {
//            this.key = key;
//            this.data = data;
//        }
//    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Trie {
        void add(String word) {

        }

        boolean search(String w) {
            return false;
        }
    }

    static class Node1 {
        public int val;
        public List<Node1> children;

        public Node1() {
        }

        public Node1(int _val, List<Node1> _children) {
            val = _val;
            children = _children;
        }
    }

    ;

    static class TrieNode {

    }

    static class KvNode implements Comparable<KvNode> {
        String key;
        int freq;

        public KvNode(String key, int freq) {
            this.key = key;
            this.freq = freq;
        }

        @Override
        public int compareTo(KvNode o) {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        }
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
} Main
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;


public class Main {

    static void solve() {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int n = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        //arr[n] = arr[0];
        //long min = Long.MAX_VALUE;
        Arrays.sort(arr);
        int sum = 0;
        int maxDiff = 0;
        for(int i=1;i<n;i++) {
            int diff = arr[i]-arr[i-1];
            maxDiff = Math.max(maxDiff, diff);
            sum += diff;

        }
        int finalDiff = k-arr[n-1]+arr[0];
        maxDiff = Math.max(maxDiff, finalDiff);
        sum+=k-arr[n-1] + arr[0];

        int res = sum - maxDiff;
        System.out.println(res);
    }


    public static void main(String[] args) {

        solve();

        //        Main m = new Main();
        //        Scanner scan = new Scanner(System.in);

        //        long n = scan.nextLong();
        //        long r = scan.nextLong();
        //        List<Long> list = new ArrayList<>();
        ////        int[] arr = new int[n];
        //        for (long i=0;i<n;i++){
        //            list.add(scan.nextLong());
        //        }
//            System.out.println(ch);
        //        solve(n, arr);
        //        scan.close();
    }


    class Counter extends HashMap<Integer, Integer> {
        public int get(int k) {
            return containsKey(k) ? super.get(k) : 0;
        }

        public void add(int k, int v) {
            put(k, get(k) + v);
        }
    }



    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

//    static class Node {
//        int key;
//        int data;
//        Node next;
//        Node prev;
//
//        Node(int key, int data) {
//            this.key = key;
//            this.data = data;
//        }
//    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Trie {
        void add(String word) {

        }

        boolean search(String w) {
            return false;
        }
    }

    static class Node1 {
        public int val;
        public List<Node1> children;

        public Node1() {
        }

        public Node1(int _val, List<Node1> _children) {
            val = _val;
            children = _children;
        }
    }

    ;

    static class TrieNode {

    }

    static class KvNode implements Comparable<KvNode> {
        String key;
        int freq;

        public KvNode(String key, int freq) {
            this.key = key;
            this.freq = freq;
        }

        @Override
        public int compareTo(KvNode o) {
            if (this.key.equals(o.key)) {
                return 0;
            }
            return Integer.compare(o.freq, this.freq);
        }
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
