#include <bits/stdc++.h>
using namespace std;
int L1, L2;
int v[2 * 1000000 + 1], pos[1000000 + 1];
bool valid(int p, int q, int nq) {
  if (nq == -1)
    return 0;
  else {
    if (p > q) q += L2;
    if (q > nq) nq += L2;
    return p <= q && q <= nq && nq - p < L2;
  }
}
int main() {
  while (scanf("%d %d", &L1, &L2) == 2) {
    for (int i = 0; i < L1; i++) {
      scanf("%d", &v[i]);
      v[i + L1] = v[i];
    }
    memset(pos, -1, sizeof(pos));
    for (int i = 0; i < L2; i++) {
      int tmp;
      scanf("%d", &tmp);
      pos[tmp] = i;
    }
    int i = 0, j = 0, maxN = 0;
    while (i < L1) {
      if (pos[v[i]] != -1) {
        if (j < i) j = i;
        while (j + 1 - i < L1 && valid(pos[v[i]], pos[v[j]], pos[v[j + 1]]))
          j++;
        maxN = max(maxN, j - i + 1);
      }
      i++;
    }
    printf("%d\n", maxN);
  }
  return 0;
}
