#include <bits/stdc++.h>
using namespace std;
int n, a[1000005], b[1000005];
int m, t[1000005], p[1000005];
bool seq(int i, int j, int n) {
  int k = (j + 1) % n;
  if (k == i || p[k] < 0) return false;
  if (p[i] <= p[j] && (p[j] < p[k] || p[k] < p[i])) return true;
  if (p[i] >= p[j] && (p[j] < p[k] && p[k] < p[i])) return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  fill_n(t, 1000005, -1);
  for (int i = 0; i < m; i++) {
    scanf("%d", b + i);
    t[b[i]] = i;
  }
  for (int i = 0; i < n; i++) p[i] = t[a[i]];
  int ans = 0;
  for (int i = 0, j = 0; i < n; i++) {
    if (p[i] < 0) continue;
    if (j <= i) j = i;
    while (seq(i, j % n, n)) j++;
    ans = max(ans, j - i + 1);
  }
  printf("%d\n", ans);
}
