#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1100000;
int n, m, a[MaxN * 2], b[MaxN], p[MaxN], s[MaxN * 2], f[MaxN][2], Ans, Count;
inline void Out(const int i, int &j) {
  if (s[j + 1] < s[j]) --Count;
  ++j;
}
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", &b[i]);
    p[b[i]] = i;
  }
  for (int i = 1; i <= 2 * n; ++i) s[i] = p[a[i]];
  for (int i = 1, j = 1; i <= n * 2; ++i) {
    if (s[i] == 0) {
      Count = 0;
      j = i + 1;
      continue;
    }
    if (i - j + 1 > n) Out(i, j);
    if (j < i && s[i] < s[i - 1]) {
      ++Count;
      while (Count > 1) Out(i, j);
    }
    while (Count == 1 && s[i] > s[j]) Out(i, j);
    Ans = (Ans > i - j + 1 ? Ans : i - j + 1);
  }
  cout << Ans << endl;
  return 0;
}
