1 1
1
2
 6 3
1 2 3 4 5 6
3 5 1
 5 5
5 6 3 2 1
4 3 1 5 6
 1 1
1
1
 10 10
1 2 3 4 5 6 7 8 9 10
6 7 8 9 10 1 2 3 4 5
 1 2
1
1 2
 10 10
9 12 15 2 4 14 1 5 6 13
12 11 13 1 4 3 8 2 14 15
 9 9
1 2 3 4 5 6 7 8 9
7 8 9 1 2 4 5 6 3
 5 5
9 2 5 1 3
1 7 2 3 9

0
 1
 4
 1
 10
 1
 3
 8
 3

#include <bits/stdc++.h>
using namespace std;
int lenA, lenB;
int A[1000004];
int B[1000004];
int idxB[1000004];
int main() {
  scanf("%d %d", &lenA, &lenB);
  for (int i = 0, _n = (lenA); i < _n; ++i) scanf("%d", A + i);
  memset(idxB, -1, sizeof(idxB));
  for (int j = 0, _n = (lenB); j < _n; ++j) {
    scanf("%d", B + j);
    idxB[B[j]] = j;
  }
  vector<long long> V;
  long long offset = 0;
  int res = 0;
  for (int k = 0, _n = (lenA * 2); k < _n; ++k) {
    long long p = idxB[A[k % lenA]];
    if (p < 0) {
      V.clear();
      continue;
    }
    p += offset;
    if (!V.empty() && V.back() >= p) {
      p += lenB;
      offset += lenB;
    }
    V.push_back(p);
    int len = int((V).size()) -
              (lower_bound(V.begin(), V.end(), p - lenB + 1) - V.begin());
    res = max(res, len);
  }
  printf("%d\n", res);
  return 0;
}
