#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int prime = 9241;
const long double pi = acos(-1.);
int a[1000005], b[1000005], bpos[2000005];
int s[2000005];
int q[2000005];
int q2[2000005];
inline int subq(int l, int r) {
  if (r < l) return 0;
  if (l == 0) return q[r];
  return q[r] - q[l - 1];
}
inline int subq2(int l, int r) {
  if (r < l) return 0;
  if (l == 0) return q2[r];
  return q2[r] - q2[l - 1];
}
int main() {
  int n1, n2;
  scanf("%d%d", &n1, &n2);
  for (int i = 0; i < n1; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n2; i++) scanf("%d", &b[i]);
  for (int i = 0; i <= 1000000; i++) bpos[i] = -1;
  for (int j = 0; j < n2; j++) bpos[b[j]] = j;
  for (int j = 0; j < n1; j++) s[j] = bpos[a[j]];
  for (int j = n1; j < n1 * 2; j++) s[j] = s[j - n1];
  for (int j = 0; j < n1 * 2; j++) q2[j] = s[j] == -1 ? 1 : 0;
  for (int j = 1; j < n1 * 2; j++) q2[j] += q2[j - 1];
  for (int j = 0; j < n1 * 2 - 1; j++) {
    q[j] = s[j] > s[j + 1];
  }
  for (int j = 1; j < n1 * 2 - 1; j++) q[j] += q[j - 1];
  int mans = 0;
  for (int i = 0; i < n1; i++) {
    if (s[i] == -1) continue;
    int l = 0;
    int r = n1;
    int ans1 = -1;
    int m;
    while (l <= r) {
      m = (l + r) >> 1;
      if (subq(i, i + m - 2) <= 1 && subq2(i, i + m - 1) == 0 &&
          (s[i + m - 1] < s[i] || subq(i, i + m - 2) == 0)) {
        ans1 = m;
        l = m + 1;
      } else
        r = m - 1;
    }
    mans = max(mans, ans1);
  }
  cout << mans << endl;
  return 0;
}
