import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int p = scan.nextInt();
        int n = scan.nextInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        }
 
        System.out.println(Math.min(ar[n-1]-ar[0],min));
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int p = scan.nextInt();
        int n = scan.nextInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        }
 
        System.out.println(Math.min(ar[n-1]-ar[0],min));
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int p = scan.nextInt();
        int n = scan.nextInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        }
 
        System.out.println(Math.min(ar[n-1]-ar[0],min));
    } main {
        Scanner scan = new Scanner(System.in);
        int p = scan.nextInt();
        int n = scan.nextInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        }
 
        System.out.println(Math.min(ar[n-1]-ar[0],min));
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int p = scan.nextInt(); p scan.nextInt() scan.nextInt scan int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int[] ar = new int[n]; ar new int[n] n for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int i1 = scan.nextInt();
            ar[i] = i1;
        } {
            int i1 = scan.nextInt();
            ar[i] = i1;
        } int i1 = scan.nextInt(); i1 scan.nextInt() scan.nextInt scan ar[i] = i1 ar[i] ar i i1 int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        } int i = 0; int i = 0; i 0 i < ar.length i ar.length ar ar.length i++ i++ i {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        } {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        } int x = i == 0 ? n-1:i-1; x i == 0 ? n-1:i-1 i == 0 i 0 n-1 n 1 i-1 i 1 int y = i+1 == ar.length ? 0:i+1; y i+1 == ar.length ? 0:i+1 i+1 == ar.length i+1 i 1 ar.length ar ar.length 0 i+1 i 1 min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min) min Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min) Math.min Math Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]) Math.min Math p + (ar[x]-ar[i]) p (ar[x]-ar[i]) ar[x] ar x ar[i] ar i p - ar[y] + ar[i] p - ar[y] p ar[y] ar y ar[i] ar i min System.out.println(Math.min(ar[n-1]-ar[0],min)) System.out.println System.out System System.out Math.min(ar[n-1]-ar[0],min) Math.min Math ar[n-1]-ar[0] ar[n-1] ar n-1 n 1 ar[0] ar 0 min String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int p = scan.nextInt();
        int n = scan.nextInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        }
 
        System.out.println(Math.min(ar[n-1]-ar[0],min));
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int p = scan.nextInt();
        int n = scan.nextInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        }
 
        System.out.println(Math.min(ar[n-1]-ar[0],min));
    }
} Main
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int p = scan.nextInt();
        int n = scan.nextInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) {
            int i1 = scan.nextInt();
            ar[i] = i1;
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < ar.length; i++) {
            int x = i == 0 ? n-1:i-1;
            int y = i+1 == ar.length ? 0:i+1;
            min = Math.min(Math.min(p + (ar[x]-ar[i]),p - ar[y] + ar[i]),min);
        }
 
        System.out.println(Math.min(ar[n-1]-ar[0],min));
    }
}