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;
int main() {
  ios_base::sync_with_stdio(false);
  int la, lb;
  cin >> la >> lb;
  int a[la * 2], b[lb];
  vector<int> pos(1000 * 1000 + 1, -1);
  for (int i = 0; i < la; ++i) {
    cin >> a[i];
    a[la + i] = a[i];
  }
  for (int i = 0; i < lb; ++i) {
    cin >> b[i];
    pos[b[i]] = i;
  }
  int res = 0, prev = -1, cur = 0;
  for (int i = 0; i < la * 2; ++i) {
    if (i > cur) prev = -1, cur = i;
    int start = pos[a[i]];
    for (;; ++cur) {
      if (cur >= la * 2) break;
      if (pos[a[cur]] == -1) break;
      if (pos[a[cur]] > start)
        if (prev != -1 && prev < start || prev > pos[a[cur]]) break;
      if (pos[a[cur]] < start)
        if (prev != -1 && prev < start && prev > pos[a[cur]]) break;
      prev = pos[a[cur]];
    }
    res = max(res, cur - i);
  }
  cout << min(res, min(la, lb));
  cin >> la;
  return 0;
}
