#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
deque<long long> Q;
int a[2000007], b[2000007];
int where[2000007];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = n; i < 2 * n; ++i) a[i] = a[i - n];
  memset(where, -1, sizeof(where));
  for (int i = 0; i < m; i++) {
    scanf("%d", &b[i]);
    where[b[i]] = i;
  }
  int cnt = 0, ans = 0;
  for (int i = 0; i < 2 * n; ++i) {
    long long pos = where[a[i]];
    if (-1 == pos) {
      Q.clear();
      continue;
    }
    if (Q.size() >= n) Q.pop_front();
    if (!Q.size())
      Q.push_back(pos);
    else {
      long long bk = Q.back();
      if (bk >= pos) {
        long long div = (bk - pos) / m;
        pos = pos + 1ll * (div + 1) * m;
      }
      Q.push_back(pos);
      while (Q.back() - Q.front() >= m) Q.pop_front();
    }
    ans = max(ans, (int)Q.size());
  }
  cout << ans << endl;
  return 0;
}
