#include <bits/stdc++.h>
using namespace std;
int NA, A[2000010];
int NB, B[1000010];
int revB[1000010];
int main() {
  while (2 == scanf("%d%d", &NA, &NB)) {
    for (int i = 0; i < (int)(NA); i++) {
      scanf("%d", &A[i]);
      A[NA + i] = A[i];
    }
    memset(revB, -1, sizeof(revB));
    for (int i = 0; i < (int)(NB); i++) {
      scanf("%d", &B[i]);
      revB[B[i]] = i;
    }
    deque<long long> que;
    int ri = 0, ans = 0;
    for (int li = 0; li < (int)(NA); li++) {
      ri = max(ri, li);
      while (ri < li + NA) {
        long long p = revB[A[ri]];
        if (p == -1) break;
        long long b = que.empty() ? -1 : que.back();
        if (p <= b) {
          long long s = b - p;
          long long k = s / NB + (s % NB ? 1 : 0);
          p += k * NB;
        }
        if (!que.empty() && p - que.front() >= NB) break;
        ++ri;
        que.push_back(p);
      }
      ans = max(ans, ri - li);
      if (revB[A[li]] != -1) que.pop_front();
    }
    printf("%d\n", ans);
  }
  return 0;
}
