#include <bits/stdc++.h>
using namespace std;
int wei[2100000], dl[2100000], x[2100000], y[2100000];
int n, m, i, ans, j, l, r;
bool can(int a) {
  if (i == j) return true;
  if (wei[dl[j]] > wei[dl[i]]) {
    if (a > wei[dl[j]] || a < wei[dl[i]]) return true;
    return false;
  }
  if (a > wei[dl[j]] && a < wei[dl[i]]) return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%d", &x[i]);
  for (i = n + 1; i < 2 * n; i++) x[i] = x[i - n];
  for (i = 1; i <= m; i++) scanf("%d", &y[i]), wei[y[i]] = i;
  l = 1;
  while (l <= n) {
    while (l <= n && wei[x[l]] == 0) l++;
    if (l > n) break;
    i = 1;
    j = 1;
    ans = max(ans, 1);
    dl[1] = x[l];
    r = l + 1;
    while (r < 2 * n && wei[x[r]] != 0) {
      if (can(wei[x[r]])) {
        dl[++j] = x[r];
        r++;
        ans = max(ans, j - i + 1);
        continue;
      }
      while (!can(wei[x[r]])) i++;
    }
    l = r;
  }
  printf("%d\n", ans);
}
