#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
int n;
int m;
int x[2000000], f[1000000];
int get(int a, int b) {
  if (a > b) b += m;
  return b - a;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < 1000000; i++) f[i] = -1;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    x[i]--;
    x[n + i] = x[i];
  }
  for (int i = 0; i < m; i++) {
    int y;
    scanf("%d", &y);
    y--;
    f[y] = i;
  }
  int l = 0, r = 0, y = -1, len = 0, cnt = 0;
  int ans = 0;
  while (r < 2 * n) {
    if (f[x[r]] == -1) {
      l = r + 1;
      y = -1;
      len = 0;
      cnt = 0;
    } else {
      if (y == -1) {
        y = f[x[r]];
        len = 0;
        cnt = 1;
      } else {
        len += get(y, f[x[r]]);
        y = f[x[r]];
        cnt++;
        while (len >= m) {
          len -= get(f[x[l]], f[x[l + 1]]);
          cnt--;
          l++;
        }
      }
    }
    ans = max(ans, cnt);
    r++;
  }
  printf("%d\n", min(ans, n));
  return 0;
}
