#include <bits/stdc++.h>
using namespace std;
int n, m, ans, l, r;
int a[2000005 * 2], b[2000005], c[2000005];
int q[2000005 * 2];
bool check(int x) {
  if (q[l] > q[r]) {
    return x > q[l] || x < q[r];
  }
  return x > q[l] && x < q[r];
}
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);
    c[b[i]] = i;
  }
  l = 1;
  for (int i = 1; i <= 2 * n; ++i) {
    int x = c[a[i]];
    if (!x) {
      l = 1;
      r = 0;
      continue;
    }
    while (l <= r && check(x)) ++l;
    q[++r] = x;
    ans = ((r - l + 1) > (ans) ? (r - l + 1) : (ans));
    if (ans == n) break;
  }
  printf("%d\n", ans);
  return 0;
}
