#include <bits/stdc++.h>
using namespace std;
int a[1000001];
int b[2000000];
int main() {
  int n, m, g;
  scanf("%d%d", &n, &m);
  memset(a, -1, sizeof(a));
  memset(b, -1, sizeof(b));
  for (int i = (0); i < (n); ++i) {
    scanf("%d", &g);
    a[g] = i;
  }
  for (int i = (0); i < (m); ++i) {
    scanf("%d", &g);
    if (a[g] != -1) b[a[g]] = i;
  }
  for (int i = (0); i < (n); ++i) b[n + i] = b[i];
  int j = 0;
  int cycle = -1;
  int res = 0;
  for (int i = (0); i < (n); ++i)
    if (b[i] != -1) {
      if (j < i) j = i;
      if (i == cycle) cycle = -1;
    ulala:
      while (b[j + 1] != -1 && b[j + 1] > b[j] &&
             (cycle == -1 || b[j + 1] < b[i])) {
        ++j;
      }
      if (cycle == -1 && b[j + 1] != -1 && b[j + 1] < b[i]) {
        cycle = j + 1;
        ++j;
        goto ulala;
      }
      res = max(res, j - i + 1);
    }
  cout << res << endl;
  return 0;
}
