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 N = 1e6 + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const double Pi = acos(-1.0);
const double EPS = 1e-8;
int n, m, a[N << 1];
map<int, int> R;
int get(int v) {
  if (R.find(v) == R.end()) return -1;
  return R[v];
}
void clear(queue<int> &que) {
  while (!que.empty()) que.pop();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); ++i) scanf("%d", a + i);
  for (int i = (0); i < (m); ++i) {
    int x;
    scanf("%d", &x);
    R[x] = i;
  }
  for (int i = (0); i < (n); ++i) a[i] = a[n + i] = get(a[i]);
  a[n << 1] = -1;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] == -1) continue;
    int j, t = 0;
    queue<int> q[2];
    q[t].push(a[i]), ans = max(ans, 1);
    for (j = i + 1; ~a[j]; ++j) {
      if (a[j] < q[t].back()) {
        if (t == 1) {
          swap(q[0], q[1]);
          clear(q[1]);
          t = 0;
        }
        t = 1;
        while (!q[0].empty() && q[0].front() < a[j]) q[0].pop();
        q[t].push(a[j]);
      } else {
        if (t == 1)
          while (!q[0].empty() && q[0].front() < a[j]) q[0].pop();
        q[t].push(a[j]);
      }
      ans = max(ans, ((int)(q[0]).size()) + ((int)(q[1]).size()));
    }
    i = j - 1;
  }
  printf("%d", ans);
  return 0;
}
