#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void umax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void umin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
inline T gcd(T a, T b) {
  return __gcd(a, b);
}
template <class T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
const int inf = 1e9 + 143;
const long long longinf = 1e18 + 143;
inline int read() {
  int x;
  scanf(" %d", &x);
  return x;
}
const int N = 1 << 21;
int n, m;
int a[N], pos[N];
int match[N];
bool good(int a, int b, int c) {
  if (a <= b) {
    return c < a || c > b;
  } else {
    return c > b && c < a;
  }
}
int main() {
  n = read();
  m = read();
  for (int i = 0; i < n; i++) a[i] = read();
  memset(pos, -1, sizeof pos);
  for (int i = 0; i < m; i++) {
    int x = read();
    pos[x] = i;
  }
  for (int i = 0; i < n; i++) {
    a[i] = pos[a[i]];
  }
  for (int i = n; i < n + n; i++) a[i] = a[i - n];
  int ans = 0;
  int R = -1;
  for (int L = 0; L < n; L++) {
    if (a[L] == -1) {
      R = L;
      continue;
    }
    if (R < L) {
      R = L;
    }
    while (R + 1 < L + n && a[R + 1] != -1 && good(a[L], a[R], a[R + 1])) {
      ++R;
    }
    0;
    umax(ans, R - L + 1);
  }
  printf("%d\n", ans);
  return 0;
}
