#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16000000")
using namespace std;
const double pi = acos(-1.0);
int n1, n2, a1[1024000], a2[1024000], id1[1024000], id2[1024000];
int main() {
  scanf("%d%d", &n1, &n2);
  memset(id1, -1, sizeof id1);
  ;
  memset(id2, -1, sizeof id2);
  ;
  for (int i = 0, _n = (n1); i < _n; i++) {
    scanf("%d", a1 + i);
    id1[a1[i]] = i;
  }
  for (int i = 0, _n = (n2); i < _n; i++) {
    scanf("%d", a2 + i);
    id2[a2[i]] = i;
  }
  deque<long long> q;
  int res = 0;
  for (int i = 0, _n = (n1 * 2); i < _n; i++) {
    int c = a1[i % n1];
    long long x = id2[c];
    if (x == -1)
      q.clear();
    else {
      if (!q.empty()) {
        x += q.front() / n2 * n2;
        while (x <= q.front()) x += n2;
        while (!q.empty() && x - q.back() >= n2) q.pop_back();
      }
      q.push_front(x);
      while ((int)(q).size() > n1) q.pop_back();
      res = max(res, (int)(q).size());
    }
  }
  printf("%d\n", res);
  return 0;
}
