#include <bits/stdc++.h>
using namespace std;
int main() {
  if (0) freopen("in.txt", "r", stdin);
  int n1, n2;
  int i, j, r1, r2, p, best;
  static int s1[2000002], p2[2000002];
  queue<int> Q;
  memset(p2, -1, sizeof(p2));
  scanf("%d%d", &n1, &n2);
  for (i = 0; i < n1; i++) {
    scanf("%d", &s1[i]);
    s1[i + n1] = s1[i];
  }
  for (i = 0; i < n2; i++) {
    scanf("%d", &j);
    p2[j] = i;
  }
  best = 0;
  for (r1 = r2 = 0; r1 < n1; r1++) {
    r2 = max(r2, r1);
    for (;;) {
      best = max(best, r2 - r1);
      if ((best == n1) || (best == n2)) goto Out;
      j = p2[s1[r2]];
      if (j < 0) break;
      if (Q.size() > 1)
        if (p > Q.front()) {
          if ((j > Q.front()) && (j < p)) break;
        } else if ((j < p) || (j > Q.front()))
          break;
      Q.push(j);
      p = j;
      r2++;
    }
    if (!Q.empty()) Q.pop();
  }
Out:;
  printf("%d\n", best);
  return (0);
}
