#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXM = 300010;
const int INF = 0x7fffffff;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const long double pi = acos(-1.0);
int a[2000010], b[1000010];
bool checkin(int x, int l, int r) {
  if (l <= r) {
    if (x >= l && x <= r) return 1;
  } else {
    if (x <= r || x >= l) return 1;
  }
  return 0;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    a[i + n] = a[i];
  }
  memset(b, 0, sizeof b);
  for (int i = 1; i <= m; ++i) {
    int x;
    scanf("%d", &x);
    b[x] = i;
  }
  deque<int> dq;
  long long ans = 0;
  for (int i = 1; i <= n * 2; ++i) {
    if (b[a[i]] > 0) {
      while (!dq.empty() && checkin(b[a[i]], dq.front(), dq.back())) {
        dq.pop_front();
      }
      dq.push_back(b[a[i]]);
      if (dq.size() > ans) ans = dq.size();
    } else {
      dq.clear();
    }
  }
  printf("%I64d\n", ans);
}
