import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int K, N;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        N = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        }
        int max = 0;
        Arrays.sort(a);
        for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        }
        max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0]));
        System.out.println(K - max);
    }
} import java.sql.SQLOutput; SQLOutput java.sql import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        int K, N;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        N = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        }
        int max = 0;
        Arrays.sort(a);
        for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        }
        max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0]));
        System.out.println(K - max);
    }
} Main public static void main(String[] args) {
        int K, N;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        N = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        }
        int max = 0;
        Arrays.sort(a);
        for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        }
        max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0]));
        System.out.println(K - max);
    } main {
        int K, N;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        N = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        }
        int max = 0;
        Arrays.sort(a);
        for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        }
        max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0]));
        System.out.println(K - max);
    } int K K N; N Scanner sc = new Scanner(System.in); sc new Scanner(System.in) K = sc.nextInt() K sc.nextInt() sc.nextInt sc N = sc.nextInt() N sc.nextInt() sc.nextInt sc int[] a = new int[N]; a new int[N] N for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        } int i = 0 ; int i = 0 ; i 0 i < N i N i++ i++ i {
            a[i] = sc.nextInt();
        } {
            a[i] = sc.nextInt();
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int max = 0; max 0 Arrays.sort(a) Arrays.sort Arrays a for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        } int i = 0 ; int i = 0 ; i 0 i < a.length - 1 i a.length - 1 a.length a a.length 1 i++ i++ i {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        } {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        } max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) ) max Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) ) Math.max Math max Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) Math.min Math a[i+1]-a[i] a[i+1] a i+1 i 1 a[i] a i K-a[i+1]+a[i] K-a[i+1] K a[i+1] a i+1 i 1 a[i] a i max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0])) max Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0])) Math.max Math max Math.min(a[N-1]-a[0],K-a[N-1]+a[0]) Math.min Math a[N-1]-a[0] a[N-1] a N-1 N 1 a[0] a 0 K-a[N-1]+a[0] K-a[N-1] K a[N-1] a N-1 N 1 a[0] a 0 System.out.println(K - max) System.out.println System.out System System.out K - max K max String[] args args public class Main {
    public static void main(String[] args) {
        int K, N;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        N = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        }
        int max = 0;
        Arrays.sort(a);
        for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        }
        max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0]));
        System.out.println(K - max);
    }
} public class Main {
    public static void main(String[] args) {
        int K, N;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        N = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        }
        int max = 0;
        Arrays.sort(a);
        for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        }
        max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0]));
        System.out.println(K - max);
    }
} Main
import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int K, N;
        Scanner sc = new Scanner(System.in);
        K = sc.nextInt();
        N = sc.nextInt();
        int[] a = new int[N];
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
        }
        int max = 0;
        Arrays.sort(a);
        for(int i = 0 ; i < a.length - 1; i++)
        {
            max = Math.max(max,Math.min(a[i+1]-a[i],K-a[i+1]+a[i]) );

        }
        max=Math.max(max,Math.min(a[N-1]-a[0],K-a[N-1]+a[0]));
        System.out.println(K - max);
    }
}
