import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k  = Integer.parseInt(scanner.next());
        int n  = Integer.parseInt(scanner.next());
        int[] a = new int[n];
        int cost;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        }
        cost = k - Math.abs(k - a[n-1] + a[0]);
        for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        }
        System.out.println(cost);
        
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k  = Integer.parseInt(scanner.next());
        int n  = Integer.parseInt(scanner.next());
        int[] a = new int[n];
        int cost;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        }
        cost = k - Math.abs(k - a[n-1] + a[0]);
        for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        }
        System.out.println(cost);
        
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k  = Integer.parseInt(scanner.next());
        int n  = Integer.parseInt(scanner.next());
        int[] a = new int[n];
        int cost;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        }
        cost = k - Math.abs(k - a[n-1] + a[0]);
        for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        }
        System.out.println(cost);
        
    } main {
        Scanner scanner = new Scanner(System.in);
        int k  = Integer.parseInt(scanner.next());
        int n  = Integer.parseInt(scanner.next());
        int[] a = new int[n];
        int cost;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        }
        cost = k - Math.abs(k - a[n-1] + a[0]);
        for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        }
        System.out.println(cost);
        
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int k  = Integer.parseInt(scanner.next()); k Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner int n  = Integer.parseInt(scanner.next()); n Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner int[] a = new int[n]; a new int[n] n int cost; cost for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = Integer.parseInt(scanner.next());
        } {
            a[i] = Integer.parseInt(scanner.next());
        } a[i] = Integer.parseInt(scanner.next()) a[i] a i Integer.parseInt(scanner.next()) Integer.parseInt Integer scanner.next() scanner.next scanner cost = k - Math.abs(k - a[n-1] + a[0]) cost k - Math.abs(k - a[n-1] + a[0]) k Math.abs(k - a[n-1] + a[0]) Math.abs Math k - a[n-1] + a[0] k - a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        } {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        } cost = Math.min(cost, k - Math.abs(a[i] - a[i-1])) cost Math.min(cost, k - Math.abs(a[i] - a[i-1])) Math.min Math cost k - Math.abs(a[i] - a[i-1]) k Math.abs(a[i] - a[i-1]) Math.abs Math a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(cost) System.out.println System.out System System.out cost String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k  = Integer.parseInt(scanner.next());
        int n  = Integer.parseInt(scanner.next());
        int[] a = new int[n];
        int cost;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        }
        cost = k - Math.abs(k - a[n-1] + a[0]);
        for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        }
        System.out.println(cost);
        
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k  = Integer.parseInt(scanner.next());
        int n  = Integer.parseInt(scanner.next());
        int[] a = new int[n];
        int cost;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        }
        cost = k - Math.abs(k - a[n-1] + a[0]);
        for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        }
        System.out.println(cost);
        
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k  = Integer.parseInt(scanner.next());
        int n  = Integer.parseInt(scanner.next());
        int[] a = new int[n];
        int cost;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(scanner.next());
        }
        cost = k - Math.abs(k - a[n-1] + a[0]);
        for (int i = 1; i < n; i++) {
            cost = Math.min(cost, k - Math.abs(a[i] - a[i-1]));
        }
        System.out.println(cost);
        
    }
}
