#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3fffffff;
const int maxN = 1000100;
int A[maxN], B[maxN];
int wb[maxN];
int d[maxN * 2];
long long ds[maxN * 2 + 1];
int n, m;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
    A[i]--;
  }
  fill(wb, wb + maxN, -1);
  for (int i = 0; i < m; ++i) {
    scanf("%d", &B[i]);
    B[i]--;
    wb[B[i]] = i;
  }
  for (int i = 0; i < n; ++i) {
    int ii = i + 1;
    if (ii >= n) ii = 0;
    if (wb[A[i]] >= 0 && wb[A[ii]] >= 0) {
      d[i] = wb[A[ii]] - wb[A[i]];
      if (d[i] < 0) d[i] += m;
    } else
      d[i] = inf;
  }
  copy(d, d + n, d + n);
  for (int i = 1; i <= n * 2; ++i) ds[i] = ds[i - 1] + d[i - 1];
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (wb[A[i]] < 0) continue;
    int left = 0, right = n;
    while (right - left > 1) {
      int mid = (left + right) / 2;
      long long q = ds[i + mid] - ds[i];
      if (q < m)
        left = mid;
      else
        right = mid;
    }
    ans = max(ans, left + 1);
  }
  cout << ans << '\n';
  return 0;
};
