1 1
1
2
 6 3
1 2 3 4 5 6
3 5 1
 5 5
5 6 3 2 1
4 3 1 5 6
 1 1
1
1
 10 10
1 2 3 4 5 6 7 8 9 10
6 7 8 9 10 1 2 3 4 5
 1 2
1
1 2
 10 10
9 12 15 2 4 14 1 5 6 13
12 11 13 1 4 3 8 2 14 15
 9 9
1 2 3 4 5 6 7 8 9
7 8 9 1 2 4 5 6 3
 5 5
9 2 5 1 3
1 7 2 3 9

0
 1
 4
 1
 10
 1
 3
 8
 3

#include <bits/stdc++.h>
const int maxn = 1000003;
int a[maxn * 2];
int b[maxn];
int c[maxn];
int d[maxn * 2];
int bin(int l, int r, int aim) {
  int mid, best = l - 1;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (c[a[mid]] < aim) {
      best = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  return best;
}
int main() {
  int n, m, i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i + n] = a[i];
  }
  for (i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
    c[b[i]] = i;
  }
  d[2 * n] = c[a[2 * n]] != 0;
  for (i = 2 * n - 1; i >= 1; i--) {
    if (c[a[i]] == 0) continue;
    if (c[a[i + 1]] == 0 || c[a[i]] > c[a[i + 1]])
      d[i] = 1;
    else
      d[i] = d[i + 1] + 1;
  }
  int ans = 0;
  for (i = 1; i <= n; i++)
    if (d[i]) {
      j = i + d[i];
      int t = d[i] + bin(j, j + d[j] - 1, c[a[i]]) - j + 1;
      ans = ans > t ? ans : t;
    }
  printf("%d\n", ans);
  return 0;
}
