#include <bits/stdc++.h>
int a[1000001];
int p[1000001];
int main() {
  int l, r, i, c, v;
  scanf("%d%d", &l, &r);
  for (i = 0; i < l; ++i) scanf("%d", &a[i]);
  for (i = 1; i <= r; ++i) scanf("%d", &c), p[c] = i;
  std::deque<int> q;
  for (i = r = 0; i < 2 * l; ++i) {
    c = p[a[i % l]];
    if (c) {
      while (q.size() >= 2 && !(q[0] < v && (v < c || c < q[0]) ||
                                q[0] > v && v < c && c < q[0]))
        q.pop_front();
      q.push_back(v = c);
    } else
      q.clear();
    if (q.size() > r) r = q.size();
  }
  printf("%d", l < r ? l : r);
}
