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 INF = 1000000007;
const int mod = 31;
void solve();
int main() {
  int t = 1;
  while (t--) solve();
  return 0;
}
vector<int> t;
int bin(int l, int r, int val) {
  if (r - l <= 1) {
    if (t[l] <= val) return r;
    return l;
  }
  int cur = (l + r) / 2;
  if (t[cur] > val) return bin(l, cur, val);
  return bin(cur, r, val);
}
void solve() {
  int n, m;
  cin >> m >> n;
  vector<int> a(m), b(n);
  vector<int> pos(1000006, -1);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    pos[b[i]] = i;
  }
  for (int i = 0; i < m; i++) t.push_back(pos[a[i]]);
  for (int i = 0; i < m; i++) t.push_back(pos[a[i]]);
  for (int i = 1; i < t.size(); i++) {
    if (t[i] + 1) {
      if (t[i] > t[i - 1]) continue;
      t[i] = (t[i - 1] / n) * n + t[i];
      if (t[i] <= t[i - 1]) {
        t[i] += n;
      }
    } else {
      t[i] = t[i - 1] + n + 1;
      if (i + 1 != t.size() && t[i + 1] + 1) {
        t[i + 1] = t[i] % n + 2 * n;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < t.size(); i++) {
    int cur = bin(0, i, t[i] - n - 1);
    if (i - cur + 1 > ans) ans = i - cur + 1;
  }
  printf("%d", ans);
}
