#include <bits/stdc++.h>
using namespace std;
int la, lb;
int a[1000000];
int b[1000000];
int p[1000001];
int pa[1000000];
int main() {
  memset(p, -1, sizeof p);
  scanf("%d %d", &la, &lb);
  for (int i = 0; i < la; ++i) scanf("%d", a + i);
  for (int i = 0; i < lb; ++i) {
    scanf("%d", b + i);
    p[b[i]] = i;
  }
  for (int i = 0; i < la; ++i) pa[i] = p[a[i]];
  int ans = 0;
  int j = 0, l = 0;
  while (j < la && ans < la) {
    int k = (j + l) % la;
    int kprev = (j + l + la - 1) % la;
    if (pa[k] == -1) {
      j += l + 1;
      l = 0;
    } else if (l == 0) {
      l++;
    } else {
      int diff = (pa[kprev] - pa[j] + 1000001) % 1000001;
      int diffNext = (pa[k] - pa[j] + 1000001) % 1000001;
      if (diff < diffNext)
        l++;
      else {
        j++;
        l--;
      }
    }
    ans = max(ans, l);
  }
  cout << ans << endl;
}
