n, m = map(int, input().split())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
dp =[[0]*m for i in range(n)]
sum_ = [[0]*m for i in range(n)]

for i in range(len(s)):
    for j in range(len(t)):
        if s[i]==t[j]:
            dp[i][j] = 1
            if i!=0 and j !=0:
                dp[i][j] = dp[i][j] +sum_[i-1][j-1]
        else:
            dp[i][j] = 0
        if i!=0 and j !=0:
            sum_[i][j] = dp[i][j] + sum_[i-1][j]+sum_[i][j-1]-sum_[i-1][j-1]
        elif i==0 and j == 0:
            sum_[i][j]= dp[i][j]
        elif i==0:
            sum_[i][j] = dp[i][j] + sum_[i][j-1]
        else:
            sum_[i][j] = dp[i][j] + sum_[i-1][j]

print(sum_[n-1][m-1]+1)