#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int N = 2e6 + 10;
int n, m, x, a[N], fi[N];
long long pos[N];
void prepare() {
  scanf("%d%d", &n, &m);
  for (auto i = 0; i < n; i++) scanf("%d", a + i), a[n + i] = a[i];
  for (auto i = 1; i <= m; i++) scanf("%d", &x), fi[x] = i;
}
int solve() {
  int ans = 0;
  for (int r = 0, l = 0; r < 2 * n; r++) {
    if (!fi[a[r]]) {
      l = r + 1;
      continue;
    }
    if (l < r)
      pos[r] = fi[a[r]] + (pos[r - 1] - fi[a[r]] + m - 1) / m * m;
    else
      pos[r] = fi[a[r]];
    while (r - l >= n || pos[r] - pos[l] >= m) l++;
    ans = max(ans, r - l + 1);
  }
  return ans;
}
int main() {
  prepare();
  cout << solve();
}
