#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000009;
int a[maxn], b[maxn];
int pos_in_b[maxn];
int Q[2 * maxn];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  for (int i = 0; i < m; i++) scanf("%d", b + i);
  memset(pos_in_b, -1, sizeof pos_in_b);
  for (int i = 0; i < m; i++) pos_in_b[b[i]] = i;
  int c = 0;
  int ql = 0, qr = 0;
  int ans = 0;
  for (int r = 0, l = 0; r < 2 * n - 1; r++) {
    int pos = pos_in_b[a[r % n]];
    if (pos == -1) {
      l = r + 1;
      ql = qr = 0;
      continue;
    }
    if (qr > ql && Q[qr - 1] >= pos + c) c += m;
    Q[qr++] = pos + c;
    while (r - l + 1 > n || Q[qr - 1] - Q[ql] + 1 > m) {
      l++;
      ql++;
    }
    ans = max(ans, r - l + 1);
  }
  printf("%d", ans);
}
