#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
int a[N], p[N], n, m, dl[N], x;
bool in(int x, int l, int r) {
  if (l <= r)
    return l <= x && x <= r;
  else
    return x <= r || l <= x;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[n + i] = a[i];
  for (int i = 1; i <= m; i++) scanf("%d", &x), p[x] = i;
  int he = 2, ta = 1, ans = 0;
  for (int i = 1; i <= n + n; i++) {
    if (p[a[i]] == 0) {
      he = 2, ta = 1;
      continue;
    }
    while ((he <= ta) && in(p[a[i]], dl[he], dl[ta])) he = (he + 1) % 2000010;
    dl[(ta = (ta + 1) % 2000010)] = p[a[i]];
    if ((ta - he + 1) > ans) ans = ta - he + 1;
  }
  printf("%d", ans);
}
