import java.io.*;
import java.util.*;


public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        int minDistance=k-(a[n-1]-a[0]);
        for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]);
        System.out.println(k-minDistance);
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        int minDistance=k-(a[n-1]-a[0]);
        for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]);
        System.out.println(k-minDistance);
    }
} Main public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        int minDistance=k-(a[n-1]-a[0]);
        for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]);
        System.out.println(k-minDistance);
    } main {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        int minDistance=k-(a[n-1]-a[0]);
        for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]);
        System.out.println(k-minDistance);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = scanner.nextInt();
        } {
            a[i] = scanner.nextInt();
        } a[i] = scanner.nextInt() a[i] a i scanner.nextInt() scanner.nextInt scanner int minDistance=k-(a[n-1]-a[0]); minDistance k-(a[n-1]-a[0]) k (a[n-1]-a[0]) a[n-1] a n-1 n 1 a[0] a 0 for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]); int i = 1; int i = 1; i 1 i < n i n i++ i++ i minDistance = Math.max(minDistance, a[i] - a[i-1]); minDistance = Math.max(minDistance, a[i] - a[i-1]) minDistance Math.max(minDistance, a[i] - a[i-1]) Math.max Math minDistance a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(k-minDistance) System.out.println System.out System System.out k-minDistance k minDistance String[]args args public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        int minDistance=k-(a[n-1]-a[0]);
        for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]);
        System.out.println(k-minDistance);
    }
} public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        int minDistance=k-(a[n-1]-a[0]);
        for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]);
        System.out.println(k-minDistance);
    }
} Main
import java.io.*;
import java.util.*;


public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        int minDistance=k-(a[n-1]-a[0]);
        for (int i = 1; i < n; i++)
            minDistance = Math.max(minDistance, a[i] - a[i-1]);
        System.out.println(k-minDistance);
    }
}
