#include <bits/stdc++.h>
using namespace std;
const int maxn = 6000008;
int n, m;
int a[maxn], b[maxn], be[maxn];
long long h[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i + n] = a[i];
  for (int i = 1; i <= m; i++) scanf("%d", &b[i]), be[b[i]] = i;
  int l = 1, r = 0, tmp = 0;
  for (int i = n; i >= 1; i--) {
    if (be[a[i]] == 0) break;
    if (!r)
      h[r = 1] = be[a[i]];
    else {
      ++r;
      if (be[a[i]] < be[a[i + 1]])
        h[r] = h[r - 1] - be[a[i + 1]] + be[a[i]];
      else
        h[r] = h[r - 1] - be[a[i + 1]] + be[a[i]] - m;
    }
    if (h[1] - h[r] >= m) {
      r--;
      break;
    }
  }
  int ans = 0;
  ans = (r > ans ? r : ans);
  for (int i = l; i <= (r + l) >> 1; i++) swap(h[i], h[r - i + 1]);
  for (int i = n + 1; i <= 2 * n - 1; i++) {
    if (be[a[i]] == 0) {
      r = l - 1;
      continue;
    }
    ++r;
    if (r == l)
      h[r] = be[a[i]];
    else {
      if (be[a[i]] > be[a[i - 1]])
        h[r] = h[r - 1] + be[a[i]] - be[a[i - 1]];
      else
        h[r] = h[r - 1] + be[a[i]] - be[a[i - 1]] + m;
    }
    while (h[r] - h[l] >= m) l++;
    ans = (ans > r - l + 1 ? ans : r - l + 1);
  }
  printf("%d\n", ans);
  return 0;
}
