#include <bits/stdc++.h>
using namespace std;
namespace {
const int kMaxN = 1000000;
int la, lb, a[kMaxN + 1], b[kMaxN + 1], c[kMaxN * 2 + 1], lc;
int idx[kMaxN + 1], pos[kMaxN + 1], f[kMaxN * 2 + 1], ans;
int Pre(int x, int s) { return (x - 1 - s + la) % la + 1; }
void Solve(FILE *fin, FILE *fout) {
  fscanf(fin, "%d%d", &la, &lb);
  for (int i = 1; i <= la; ++i) {
    fscanf(fin, "%d", &a[i]);
    idx[a[i]] = i;
  }
  for (int i = 1; i <= lb; ++i) {
    fscanf(fin, "%d", &b[i]);
    if (idx[b[i]]) c[++lc] = idx[b[i]];
  }
  for (int i = 1; i <= lc; ++i) c[i + lc] = c[i];
  for (int i = 1; i <= lc * 2; ++i) {
    f[i] = 1;
    pos[c[i]] = i;
    int pre = Pre(c[i], 1);
    if (pos[pre] && pre != c[i]) {
      int pos_pre = pos[pre];
      while (f[pos_pre] && pos[Pre(pre, f[pos_pre] - 1)] > lc) --f[pos_pre];
      f[i] += f[pos_pre];
    }
    ans = max(ans, f[i]);
  }
  fprintf(fout, "%d\n", ans);
}
}  // namespace
int main() {
  ::Solve(stdin, stdout);
  return 0;
}
