#include <bits/stdc++.h>
using namespace std;
int sum[2010][2010];
int dp[2010][2010];
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> S(N);
  vector<int> T(M);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  for (int j = 0; j < M; j++) {
    cin >> T[j];
  }
  for (int i = 0; i < 2010; i++) {
    for (int j = 0; j < 2010; j++) {
      sum[i][j] = 0;
      dp[i][j] = 0;
    }
  }
  sum[0][0] = 1;
  dp[0][0] = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < M; j++) {
      if (S[i - 1] == T[j - 1]) {
        sum[i][j] = max(sum[i][j], sum[i - 1][j] + sum[i][j - 1] + 1);
      } else {
        sum[i][j] =
            max(sum[i][j], sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]);
      }
    }
  }
  cout << sum[N - 1][M - 1] << endl;
  return 0;
}
