#include <bits/stdc++.h>
using namespace std;
int a[3000010], b[3000010], an, bn;
int l[3000010], w, r;
int sub(int x, int y) {
  x -= y;
  if (x < 0) x += bn;
  return x;
}
int main() {
  int i, x, ans, ar, w, r;
  scanf("%d%d", &an, &bn);
  memset(b, -1, sizeof(b));
  for (i = 0; i < an; i++) {
    scanf("%d", &a[i]);
    a[an + i] = a[i];
  }
  for (i = 0; i < bn; i++) {
    scanf("%d", &x);
    b[x] = i;
  }
  ar = 0;
  ans = 0;
  w = r = 0;
  for (i = 0; i < an; i++) {
    while (ar < i + an) {
      if (b[a[ar]] < 0) break;
      x = b[a[ar]];
      l[w++] = x;
      if (w > r + 1) {
        if (sub(l[w - 1], l[r]) < sub(l[w - 2], l[r])) {
          w--;
          break;
        }
      }
      ar++;
    }
    ans = max(ans, w - r);
    if (ar == i) {
      ar++;
      w++;
    }
    r++;
  }
  printf("%d\n", ans);
  return 0;
}
