
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author yoshizaki
 *
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        int lake = scanner.nextInt();
        int house = scanner.nextInt();
        int[] position = new int[house];
        for(int i = 0; i < house; i++) position[i] = scanner.nextInt();
        int[] distance = new int[house];
        distance[0] = lake - position[house - 1] + position[0];
        for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        }
        int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt();
        System.out.println(ans);
    }

} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        int lake = scanner.nextInt();
        int house = scanner.nextInt();
        int[] position = new int[house];
        for(int i = 0; i < house; i++) position[i] = scanner.nextInt();
        int[] distance = new int[house];
        distance[0] = lake - position[house - 1] + position[0];
        for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        }
        int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt();
        System.out.println(ans);
    }

} Main public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        int lake = scanner.nextInt();
        int house = scanner.nextInt();
        int[] position = new int[house];
        for(int i = 0; i < house; i++) position[i] = scanner.nextInt();
        int[] distance = new int[house];
        distance[0] = lake - position[house - 1] + position[0];
        for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        }
        int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt();
        System.out.println(ans);
    } main {
        Scanner scanner= new Scanner(System.in);
        int lake = scanner.nextInt();
        int house = scanner.nextInt();
        int[] position = new int[house];
        for(int i = 0; i < house; i++) position[i] = scanner.nextInt();
        int[] distance = new int[house];
        distance[0] = lake - position[house - 1] + position[0];
        for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        }
        int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt();
        System.out.println(ans);
    } Scanner scanner= new Scanner(System.in); scanner new Scanner(System.in) int lake = scanner.nextInt(); lake scanner.nextInt() scanner.nextInt scanner int house = scanner.nextInt(); house scanner.nextInt() scanner.nextInt scanner int[] position = new int[house]; position new int[house] house for(int i = 0; i < house; i++) position[i] = scanner.nextInt(); int i = 0; int i = 0; i 0 i < house i house i++ i++ i position[i] = scanner.nextInt(); position[i] = scanner.nextInt() position[i] position i scanner.nextInt() scanner.nextInt scanner int[] distance = new int[house]; distance new int[house] house distance[0] = lake - position[house - 1] + position[0] distance[0] distance 0 lake - position[house - 1] + position[0] lake - position[house - 1] lake position[house - 1] position house - 1 house 1 position[0] position 0 for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        } int i = 1; int i = 1; i 1 i < house i house i++ i++ i {
        	distance[i] = position[i] - position[i - 1];
        } {
        	distance[i] = position[i] - position[i - 1];
        } distance[i] = position[i] - position[i - 1] distance[i] distance i position[i] - position[i - 1] position[i] position i position[i - 1] position i - 1 i 1 int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt(); ans Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt() Arrays.stream(distance).sum() Arrays.stream(distance).sum Arrays.stream(distance) Arrays.stream Arrays distance Arrays.stream(distance).max().getAsInt() Arrays.stream(distance).max().getAsInt Arrays.stream(distance).max() Arrays.stream(distance).max Arrays.stream(distance) Arrays.stream Arrays distance System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        int lake = scanner.nextInt();
        int house = scanner.nextInt();
        int[] position = new int[house];
        for(int i = 0; i < house; i++) position[i] = scanner.nextInt();
        int[] distance = new int[house];
        distance[0] = lake - position[house - 1] + position[0];
        for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        }
        int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt();
        System.out.println(ans);
    }

} public class Main {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        int lake = scanner.nextInt();
        int house = scanner.nextInt();
        int[] position = new int[house];
        for(int i = 0; i < house; i++) position[i] = scanner.nextInt();
        int[] distance = new int[house];
        distance[0] = lake - position[house - 1] + position[0];
        for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        }
        int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt();
        System.out.println(ans);
    }

} Main

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author yoshizaki
 *
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        int lake = scanner.nextInt();
        int house = scanner.nextInt();
        int[] position = new int[house];
        for(int i = 0; i < house; i++) position[i] = scanner.nextInt();
        int[] distance = new int[house];
        distance[0] = lake - position[house - 1] + position[0];
        for(int i = 1; i < house; i++) {
        	distance[i] = position[i] - position[i - 1];
        }
        int ans = Arrays.stream(distance).sum() - Arrays.stream(distance).max().getAsInt();
        System.out.println(ans);
    }

}
