#include <bits/stdc++.h>
using namespace std;
int n, m, st, ed, x, ret;
int a[2000005], b[2000005], p[2000005], q[2000005];
int bad(int a, int b, int c) {
  b = (b + m - a) % m;
  c = (c + m - a) % m;
  return b >= c;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); i++) scanf("%d", &a[i]), a[n + i] = a[i];
  memset(p, -1, sizeof(p));
  for (int i = (0); i < (m); i++) scanf("%d", &b[i]), p[b[i]] = i;
  ret = 0;
  st = 0, ed = -1;
  for (int i = (0); i < (n << 1); i++) {
    if (p[a[i]] == -1) {
      st = 0, ed = -1;
      continue;
    }
    x = p[a[i]];
    while (ed >= st && bad(q[st], q[ed], x)) st++;
    q[++ed] = x;
    ret = max(ret, ed - st + 1);
  }
  printf("%d\n", ret);
  return 0;
}
