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 maxn = 3000050;
int n, m;
int a[maxn], b[maxn], db[maxn];
bool can(int a, int b, int x) {
  return (a <= b && !(a <= x && x < b)) || (a > b && (x >= b && x < a));
}
int main(void) {
  memset(db, -1, sizeof(b));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (n); ++i) scanf("%d", a + i), a[i + n] = a[i];
  for (int i = 0; i < (m); ++i) scanf("%d", b + i), db[b[i]] = i;
  for (int i = 1; i <= (n + n - 1); ++i) a[i] = db[a[i]];
  int x = 1, y = 0;
  int ans = 0;
  while (y < n + n - 1) {
    ++y;
    if (x == y) {
      if (a[x] == -1)
        x = y + 1;
      else
        ans = max(ans, 1);
      continue;
    }
    if (a[y] == -1) {
      x = y + 1;
      continue;
    }
    while (!can(a[x], a[y - 1], a[y]) && x < y) ++x;
    if (x == y) {
      if (a[x] == -1)
        x = y + 1;
      else
        ans = max(ans, 1);
    }
    ans = max(ans, y - x + 1);
  }
  cout << min(ans, min(m, n)) << endl;
}
