#include <bits/stdc++.h>
using namespace std;
int m1[2000005];
int m2[2000005];
int pos1[1000005];
int pos2[1000005];
int ans[2000005];
int parent[2000005];
int n1, n2;
int main() {
  scanf("%d%d", &n1, &n2);
  int i;
  for (i = 1; i <= n1; i++) {
    scanf("%d", m1 + i);
    m1[n1 + i] = m1[i];
    pos1[m1[i]] = i;
  }
  for (i = 1; i <= n2; i++) {
    scanf("%d", m2 + i);
    m2[n2 + i] = m2[i];
    pos2[m2[i]] = i;
  }
  int res = 0;
  int st = 1;
  for (i = 1; i <= n1 + n1; i++) {
    int p2 = pos2[m1[i]];
    int t = 0;
    if (p2) {
      if (ans[i - 1]) {
        p2 += (parent[i - 1] - 1) / n2 * n2;
        if (parent[i - 1] >= p2) {
          p2 += n2;
        }
        while (st < i && p2 - parent[st] + 1 > n2) st++;
        t = i - st + 1;
        parent[i] = p2;
      } else {
        t = 1;
        st = i;
        parent[i] = p2;
      }
    }
    ans[i] = t;
    if (ans[i] > res) res = ans[i];
  }
  printf("%d\n", res);
  return 0;
}
