#include <bits/stdc++.h>
int a[2 * 1100000], b[2 * 1100000], n, m, Ans, f[1100000];
long long Q[2 * 1100000];
char Chr;
void read(int& x) {
  for (; (Chr = getchar()) < '0';)
    ;
  for (x = Chr - '0'; (Chr = getchar()) >= '0'; x = x * 10 + Chr - '0')
    ;
}
int main() {
  int i, k, l;
  long long j;
  read(n);
  read(m);
  for (i = 0; i < n; i++) read(a[i]);
  for (i = 0; i < n; i++) a[i + n] = a[i];
  for (i = 0; i < m; i++) read(b[i]);
  memset(f, -1, sizeof f);
  for (i = 0; i < m; i++) f[b[i]] = i;
  for (i = k = 0; i < n; i++)
    if (f[a[i]] != -1) {
      if (k <= i) {
        k = i + 1;
        Q[i] = f[a[i]];
      }
      for (; k < i + n && f[a[k]] != -1; k++) {
        j = (Q[k - 1] / m) * m + f[a[k]];
        if (j <= Q[k - 1]) j += m;
        Q[k] = j;
        if (Q[k] >= Q[i] + m) break;
      }
      if (k - i > Ans) Ans = k - i;
    }
  printf("%d\n", Ans);
  return 0;
}
