#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
long l1, l2;
long s1[2 * 1000007 + 7];
long Ind[1000007 + 7];
bool Inside(long l, long r, long i) {
  if (l <= r) {
    return i >= l && i <= r;
  } else {
    return i >= l || i <= r;
  }
}
int main(void) {
  long i, j, v;
  ios_base::sync_with_stdio(0);
  cin >> l1 >> l2;
  for (i = 0; i < l1; i++) {
    cin >> s1[i];
    s1[l1 + i] = s1[i];
  }
  memset(Ind, -1, sizeof(Ind));
  for (i = 0; i < l2; i++) {
    cin >> v;
    Ind[v] = i;
  }
  long Lim = 2 * l1;
  deque<long> q;
  long L = 0, R = l2 - 1;
  long Ans = 0;
  for (i = 0; i < Lim; i++) {
    if (Ind[s1[i]] == -1) {
      q.clear();
    } else if (q.size() == 0) {
      q.push_back(s1[i]);
    } else if (q.size() < l1 && Inside(L, R, Ind[s1[i]])) {
      q.push_back(s1[i]);
    } else {
      q.pop_front();
      i--;
    }
    if (q.size()) {
      L = (Ind[q.back()] + 1) % l2;
      R = (Ind[q.front()] + l2) % l2;
    }
    Ans = max(Ans, (long)q.size());
  }
  Ans = max(Ans, (long)q.size());
  cout << Ans << endl;
  return 0;
}
