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>
using namespace std;
const int L = 1000000 + 5;
int la, lb;
int a[L * 2], b[L];
int bp[L];
int main() {
  scanf("%d%d", &la, &lb);
  for (int i = 1; i <= la; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= lb; ++i) scanf("%d", &b[i]);
  for (int i = 1; i <= la; ++i) a[la + i] = a[i];
  for (int i = 1; i <= lb; ++i) bp[b[i]] = i;
  int ans = 0;
  for (int j = 1, i = 1, turned = false; i <= 2 * la; ++i) {
    if (!bp[a[i]]) {
      j = i + 1;
      turned = false;
      continue;
    }
    while (i - j + 1 > la ||
           ((turned || bp[a[i]] < bp[a[i - 1]]) && bp[a[i]] > bp[a[j]]) ||
           (turned && bp[a[i]] < bp[a[i - 1]])) {
      if (turned && i - j >= 2 && bp[a[j]] > bp[a[j + 1]]) turned = false;
      ++j;
    }
    if (bp[a[i]] < bp[a[i - 1]]) turned = true;
    ans = max(ans, i - j + 1);
  }
  printf("%d\n", ans);
  return 0;
}
