
import java.util.Scanner;

class Main {

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        int[] arr2 = new int[N];
        for (int i = 1; i < N; i++) {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        }
        arr2[N-1] = K;

        int sum = 0;
        for (int i = 0; i < N-1; i++) {
            sum += arr2[i];
        }
        
        int ans = sum;
        for (int i = 1; i < N; i++) {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        }

        System.out.println(ans);

        sc.close();
	}
} import java.util.Scanner; Scanner java.util class Main {

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        int[] arr2 = new int[N];
        for (int i = 1; i < N; i++) {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        }
        arr2[N-1] = K;

        int sum = 0;
        for (int i = 0; i < N-1; i++) {
            sum += arr2[i];
        }
        
        int ans = sum;
        for (int i = 1; i < N; i++) {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        }

        System.out.println(ans);

        sc.close();
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        int[] arr2 = new int[N];
        for (int i = 1; i < N; i++) {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        }
        arr2[N-1] = K;

        int sum = 0;
        for (int i = 0; i < N-1; i++) {
            sum += arr2[i];
        }
        
        int ans = sum;
        for (int i = 1; i < N; i++) {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        }

        System.out.println(ans);

        sc.close();
	} main {
		Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        int[] arr2 = new int[N];
        for (int i = 1; i < N; i++) {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        }
        arr2[N-1] = K;

        int sum = 0;
        for (int i = 0; i < N-1; i++) {
            sum += arr2[i];
        }
        
        int ans = sum;
        for (int i = 1; i < N; i++) {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        }

        System.out.println(ans);

        sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] arr = new int[N]; arr new int[N] N for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc int[] arr2 = new int[N]; arr2 new int[N] N for (int i = 1; i < N; i++) {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        } {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        } arr2[i-1] = arr[i]-arr[i-1] arr2[i-1] arr2 i-1 i 1 arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 K -= arr2[i-1] K arr2[i-1] arr2 i-1 i 1 arr2[N-1] = K arr2[N-1] arr2 N-1 N 1 K int sum = 0; sum 0 for (int i = 0; i < N-1; i++) {
            sum += arr2[i];
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            sum += arr2[i];
        } {
            sum += arr2[i];
        } sum += arr2[i] sum arr2[i] arr2 i int ans = sum; ans sum for (int i = 1; i < N; i++) {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        } {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        } sum -= arr2[i-1] sum arr2[i-1] arr2 i-1 i 1 sum += arr2[(N-2+i)%N] sum arr2[(N-2+i)%N] arr2 (N-2+i)%N (N-2+i) N-2 N 2 i N ans = Math.min(ans, sum) ans Math.min(ans, sum) Math.min Math ans sum System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc String[] args args

import java.util.Scanner;

class Main {

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

        int K = sc.nextInt();
        int N = sc.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = sc.nextInt();
        }
        int[] arr2 = new int[N];
        for (int i = 1; i < N; i++) {
            arr2[i-1] = arr[i]-arr[i-1];
            K -= arr2[i-1];
        }
        arr2[N-1] = K;

        int sum = 0;
        for (int i = 0; i < N-1; i++) {
            sum += arr2[i];
        }
        
        int ans = sum;
        for (int i = 1; i < N; i++) {
            sum -= arr2[i-1];
            sum += arr2[(N-2+i)%N];
            ans = Math.min(ans, sum);
        }

        System.out.println(ans);

        sc.close();
	}
}