#include <bits/stdc++.h>
using namespace std;
int n, m;
int v1[1000005];
int v2[1000005];
int id[1000005];
int w[1000005];
int x, y;
int q[2000005];
int res;
bool check(int a, int b, int c) { return ((b + m - a) % m >= (c + m - a) % m); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", v1 + i);
  for (int i = 0; i < m; i++) scanf("%d", v2 + i);
  for (int i = 0; i < 1000005; i++) id[i] = -1;
  for (int i = 0; i < m; i++) id[v2[i]] = i;
  for (int i = 0; i < 2 * n; i++) w[i] = id[v1[i % n]];
  x = -1;
  for (int i = 0; i < 2 * n; i++) {
    if (w[i] == -1) {
      x = -1;
      y = i + 1;
    } else {
      while (x >= y && check(w[y], w[x], w[i])) y++;
      x = i;
      res = max(res, x - y + 1);
    }
  }
  cout << res << endl;
  return 0;
}
