#include <bits/stdc++.h>
using namespace std;
const int MX = 1000006;
int a[MX], as;
int b[MX], bs;
int p[MX];
long long make_greater(long long x, long long i, long long what) {
  if (x > what) return x;
  what -= x;
  long long t = ((what / i) + 1LL) * i;
  long long x2 = t + x;
  return t + x;
}
int main() {
  scanf("%d%d", &as, &bs);
  for (int i = 0; i < as; ++i) scanf("%d", a + i);
  for (int i = 0; i < MX; ++i) p[i] = -1;
  for (int i = 0; i < bs; ++i) {
    scanf("%d", b + i);
    p[b[i]] = i;
  }
  long long la = 0, ra = 0, lb = -1, rb = -1;
  int best = 0;
  while (ra < 2 * as) {
    if (lb == -1) {
      if (p[a[ra % as]] != -1) {
        la = ra;
        lb = p[a[ra % as]];
        rb = lb;
      }
    } else {
      if (p[a[ra % as]] != -1) {
        long long poz = p[a[ra % as]];
        poz = make_greater(poz, bs, rb);
        rb = poz;
        while (rb - lb >= bs || ra - la >= as) {
          ++la;
          poz = p[a[la % as]];
          poz = make_greater(poz, bs, lb);
          lb = poz;
        }
      } else {
        lb = rb = -1;
      }
    }
    if (lb != -1) best = max(best, (int)(ra - la + 1LL));
    ++ra;
  }
  printf("%d\n", best);
}
