#include <bits/stdc++.h>
using namespace std;
int A[1111111];
int B[1111111];
int main(void) {
  int alen = 0;
  int blen = 0;
  scanf("%d %d", &alen, &blen);
  for (int i = 0; i < alen; i++) scanf("%d", &A[i]);
  memset(B, -1, sizeof(B));
  for (int i = 0; i < blen; i++) {
    int x = 0;
    scanf("%d", &x);
    B[x] = i;
  }
  deque<int> Queue;
  int ans = 0;
  for (int i = 0; i < 2 * alen; i++) {
    int where = B[A[i % alen]];
    if (where != -1) {
      while (Queue.size() >= 2 &&
             !((Queue.front() < Queue.back() &&
                (Queue.back() < where || where < Queue.front())) ||
               (Queue.front() > Queue.back() && Queue.back() < where &&
                where < Queue.front())))
        Queue.pop_front();
      Queue.push_back(where);
    } else
      Queue.clear();
    ans = max<int>(ans, Queue.size());
  }
  ans = min(ans, alen);
  printf("%d\n", ans);
  return 0;
}
