x, y = gets.split().map(&:to_i)
S = gets.split().map(&:to_i)
T = gets.split().map(&:to_i)
  
array = Array.new(x + 1) { Array.new(y + 1, 1) }

x.times do |n|
  y.times do |m|
    if S[n] == T[m]
      array[n+1][m+1] = array[n][m+1] + array [n+1][m]
    else
      array[n+1][m+1] = array[n][m+1] + array [n+1][m] - array[n][m]
    end
  end
end

puts array[-1][-1]