#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int A[1111111], B[1111111];
int where[1111111];
int main() {
  int N, M;
  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]);
  for (int i = 0; i < (1111111); ++i) where[i] = -1;
  for (int i = 0; i < (M); ++i) where[B[i]] = i;
  vector<int> V;
  for (int i = 0; i < (N); ++i) V.push_back(where[A[i]]);
  int K = V.size();
  for (int i = 0; i < (K); ++i) V.push_back(V[i]);
  deque<int> D;
  int result = 0;
  long long spread = 0;
  for (int i = 0; i < (2 * K); ++i) {
    if (V[i] == -1) {
      spread = 0;
      D.clear();
      continue;
    }
    D.push_back(V[i]);
    if (D.size() > 1) {
      spread += D[D.size() - 1] - D[D.size() - 2];
      if (D[D.size() - 1] - D[D.size() - 2] <= 0) spread += M;
    }
    while (spread > M - 1) {
      spread -= D[1] - D[0];
      if (D[1] - D[0] <= 0) spread -= M;
      D.pop_front();
    }
    result = max(result, (int)D.size());
  }
  printf("%d\n", result);
}
