from functools import lru_cache

N, M = [int(i) for i in input().split()]
S = [int(i) for i in input().split()]
T = [int(i) for i in input().split()]


def m():
    for s in range(len(S)):
        for t in range(len(T)):
            solve(s, t)
    res = solve(N, M)

    print(res)


@lru_cache(None)
def solve(s, t):
    if s == 0 or t == 0:
        return 1
    if s == 1:
        return T.count(S[s-1]) + 1
    if t == 1:
        return S.count(T[t-1]) + 1
    acc = 0
    acc += solve(s-1, t)
    a = [i for i, e in enumerate(T[:t]) if e == S[s-1]]
    for t2 in [i for i, e in enumerate(T[:t]) if e == S[s-1]]:
        acc += solve(s-1, t2)
    return acc

m()
