using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, q) = (c[0], c[1]); var a = NList; var b = NList; var op = new SegOp(); var lseg = new SegmentTree(n, op); var rseg = new SegmentTree(n, op); lseg[0] = n - 1; rseg[0] = 0; for (var i = 0; i < q; ++i) { var nlseg = new SegmentTree(n, op); var nrseg = new SegmentTree(n, op); for (var j = 0; j < n; ++j) { if (a[j] == b[i]) { var min = Math.Min(lseg.Prod(0, j) - (n - 1 - j), rseg.Prod(j, n) - j); nlseg[j] = min + n - 1 - j; nrseg[j] = min + j; } } lseg = nlseg; rseg = nrseg; } var ans = int.MaxValue; for (var i = 0; i < n; ++i) ans = Math.Min(ans, rseg[i] - i); WriteLine(ans); } struct SegOp : ISegmentTreeOperator { public int Identity => int.MaxValue / 2; public int Operate(int x, int y) { return Math.Min(x, y); } } interface ISegmentTreeOperator { T Identity { get; } T Operate(T x, T y); } class SegmentTree { int size; int log; T[] d; ISegmentTreeOperator op; void Update(int k) { d[k] = op.Operate(d[2 * k], d[2 * k + 1]); } public SegmentTree(int n, ISegmentTreeOperator op) { this.op = op; size = 1; while (size < n) size <<= 1; log = CountRZero(size); d = new T[2 * size]; for (var i = 0; i < d.Length; ++i) d[i] = op.Identity; } public SegmentTree(T[] v, ISegmentTreeOperator op) { this.op = op; size = 1; while (size < v.Length) size <<= 1; log = CountRZero(size); d = new T[2 * size]; for (var i = 0; i < size; ++i) d[i] = op.Identity; for (var i = 0; i < v.Length; ++i) d[size + i] = v[i]; for (var i = size - 1; i >= 1; --i) Update(i); } int CountRZero(int n) { var ans = 0; while (n % 2 == 0) { ++ans; n >>= 1; } return ans; } public T this[int p] { get { return d[p + size]; } set { p += size; d[p] = value; for (var i = 1; i <= log; ++i) Update(p >> i); } } public T Prod(int l, int r) { var sml = op.Identity; var smr = op.Identity; l += size; r += size; while (l < r) { if ((l & 1) != 0) sml = op.Operate(sml, d[l++]); if ((r & 1) != 0) smr = op.Operate(d[--r], smr); l >>= 1; r >>= 1; } return op.Operate(sml, smr); } T AllProd() => d[1]; } }