#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T checkmin(T &a, T b) {
  return (a < b) ? a : a = b;
}
template <class T>
inline T checkmax(T &a, T b) {
  return (a > b) ? a : a = b;
}
template <class T>
T GCD(T a, T b) {
  if (a < 0) return GCD(-a, b);
  if (b < 0) return GCD(a, -b);
  return (a == 0) ? b : GCD(b % a, a);
}
template <class T>
T LCM(T a, T b) {
  if (a < 0) return LCM(-a, b);
  if (b < 0) return LCM(a, -b);
  return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b;
}
template <class T>
inline T sqr(T X) {
  return X * X;
}
namespace Poor {
const int MaxiN = 1000005;
int N, M, Ans;
int A[MaxiN], B[MaxiN];
int P[MaxiN];
bool In(int X, int L, int R) {
  if (L <= R)
    return L <= X && X <= R;
  else
    return L <= X || X <= R;
}
void Run() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; ++i) scanf("%d", A + i);
  for (int i = 0; i < M; ++i) scanf("%d", B + i);
  fill(P, P + MaxiN, -1);
  for (int i = 0; i < M; ++i) P[B[i]] = i;
  Ans = 0;
  deque<int> Q;
  for (int i = 0; i < N * 2; ++i) {
    if (P[A[i % N]] == -1) {
      Q.clear();
      continue;
    }
    while (!Q.empty() && In(P[A[i % N]], Q.front(), Q.back())) Q.pop_front();
    Q.push_back(P[A[i % N]]);
    checkmax(Ans, (int)Q.size());
  }
  printf("%d\n", Ans);
}
}  // namespace Poor
int main() {
  Poor::Run();
  return 0;
}
