N, Q = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) from collections import defaultdict dic = defaultdict(list) for i, a in enumerate(A): dic[a].append(i) INF = 10 ** 6 from atcoder.segtree import SegTree base = [INF] * N ST1a = SegTree(min, INF, base) ST2a = SegTree(min, INF, base) ST1a.set(0, 0) ST2a.set(0, 0) for b in B: ST1b = SegTree(min, INF, base) ST2b = SegTree(min, INF, base) BB = [-1] * N for c in dic[b]: a = ST1a.prod(c, N) - c a = min(a, ST2a.prod(0, c) + c) #BB[c] = a ST1b.set(c, a + c) ST2b.set(c, a - c) #print(BB) ST1a = ST1b ST2a = ST2b ans = INF for i in range(N): ans = min(ans, ST1a.get(i) - i, ST2a.get(i) + i) print(ans)