#include <bits/stdc++.h>
using namespace std;
int nextint() {
  int t;
  scanf("%d", &t);
  return t;
}
int s1[1000001];
int s2[1000001];
int p[1000001];
int m[1000001];
int l1, l2;
int getlen(int l, int r) {
  if (l <= r)
    return (r - l + 1);
  else
    return getlen(l, r + l1);
}
int main() {
  l1 = nextint();
  l2 = nextint();
  for (int i = 0; i < (l1); i++) s1[i] = nextint();
  for (int i = 0; i < (l2); i++) s2[i] = nextint();
  memset(m, -1, sizeof(m));
  for (int i = 0; i < (l2); i++) m[s2[i]] = i;
  for (int i = 0; i < (l1); i++) p[i] = m[s1[i]];
  int l = -1, r = 0;
  int res = 0;
  bool empty = false;
  for (int i = 0; i < (l1); i++) {
    l = (l + 1) % l1;
    if (empty) r = l;
    empty = false;
    if (p[l] == -1) {
      empty = true;
      continue;
    }
    while (1) {
      if ((r + 1) % l1 == l) break;
      int np = p[(r + 1) % l1];
      if (np == -1) break;
      if (p[l] <= np && np <= p[r]) break;
      if (p[l] > p[r] && (!(p[r] < np && np < p[l]))) break;
      r = (r + 1) % l1;
    }
    res = max(res, getlen(l, r));
  }
  printf("%d\n", res);
  return 0;
}
