#include <bits/stdc++.h>
using namespace std;
const int nmax = 2e6;
int n, m;
int a[nmax];
int b[nmax];
int second[nmax];
int dist(int i, int j) {
  if (i <= j)
    return j - i;
  else
    return m - (i - j);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i + n] = a[i];
  }
  for (int j = 0; j < m; j++) {
    scanf("%d", &b[j]);
    second[b[j]] = j + 1;
  }
  int ans = 0;
  int curans = 0;
  int last = 0;
  for (int i = 0; i < n; i++) {
    int x = second[a[i]] - 1;
    if (x == -1) {
      curans = 0;
      continue;
    }
    if (curans == 0) last = x;
    curans = max(curans, 1);
    int _new;
    while ((_new = second[a[i + curans]]) != 0 && curans < n) {
      _new--;
      if (dist(x, last) + dist(last, _new) < m) {
        curans++;
        last = _new;
      } else
        break;
    }
    ans = max(curans, ans);
    curans = max(curans - 1, 0);
  }
  cout << ans << endl;
  return 0;
};
