#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1000006, MaxC = 0x3F3F3F3F, NA = -1;
int a[MaxN], b[MaxN];
int p[MaxN], q[MaxN];
int c[MaxN << 1], d[MaxN << 1];
int k, m, n;
int main(void) {
  int cur, i, j, res;
  while (scanf(" %d %d", &m, &n) != EOF) {
    for (i = 0; i < m; i++) scanf(" %d", &a[i]);
    for (i = 0; i < n; i++) scanf(" %d", &b[i]);
    memset(p, NA, sizeof(p));
    for (i = 0; i < m; i++) p[a[i]] = i;
    memset(q, NA, sizeof(q));
    for (i = 0; i < n; i++) q[b[i]] = i;
    for (i = 0; i < m; i++) c[i] = q[a[i]];
    k = m;
    for (i = 0; i < k; i++) assert(true);
    for (i = 0; i < k; i++) c[k + i] = c[i];
    k <<= 1;
    for (i = 0; i < k - 1; i++) d[i] = (c[i + 1] - c[i] + n - 1) % n + 1;
    res = 0;
    cur = 0;
    for (i = j = 0; j < k; j++) {
      if (c[j] == NA) {
        i = j + 1;
        cur = 0;
        continue;
      }
      if (j > i) {
        cur += d[j - 1];
        while (cur >= n) {
          cur -= d[i];
          i++;
        }
      }
      res = max(res, j - i + 1);
    }
    printf("%d\n", res);
  }
  return 0;
}
