n,m = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))
if n < m:
    Sh, Lo = S,T
else:
    Sh, Lo = T,S
p = len(Sh) #短い
q = len(Lo) #長い

Li = [[] for i in range(q)]
Li2 = []
for i in range(p):
    a = Sh[i]
    for j in range(i,q):
        if a == Lo[j]:
            Li[i].append((i,j))
    if i < q:
        b = Lo[i]
        for j in range(i+1,p):
            if b == Sh[j]:
                Li[i].append((j,i))
    Li2.append(len(Li[i]))
#print(Li)
ans = 0
for i in range(len(Li2)):
    ans += 2^(sum(Li2[i:])+1)+2
print(ans)

