#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
int aabs(int a) {
  if (a < 0) return -a;
  return a;
}
int gcd(int a, int b) {
  while (a > 0 && b > 0) {
    if (a > b) {
      a %= b;
    } else {
      b %= a;
    }
  }
  return a + b;
}
int a[1000013], b[1000013];
int aa[2000013];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  memset(b, 255, sizeof(b));
  for (int i = 0; i < m; i++) {
    int t1;
    scanf("%d", &t1);
    b[t1] = i;
  }
  int ans = 0;
  int left = 0, right = 0;
  for (int i = 0; i < n; i++) {
    if (b[a[i]] < 0) {
      left = right;
    } else {
      aa[right] = b[a[i]];
      while (left < right &&
             ((aa[right] == aa[left]) ||
              (aa[right - 1] < aa[left] && aa[left] < aa[right]) ||
              (aa[right] < aa[right - 1] &&
               (aa[left] < aa[right] || aa[right - 1] < aa[left])))) {
        left++;
      }
      right++;
      ans = max(ans, right - left);
    }
  }
  for (int i = 0; i < n; i++) {
    if (b[a[i]] < 0) {
      left = right;
    } else {
      aa[right] = b[a[i]];
      while (left < right &&
             ((aa[right] == aa[left]) ||
              (aa[right - 1] < aa[left] && aa[left] < aa[right]) ||
              (aa[right] < aa[right - 1] &&
               (aa[left] < aa[right] || aa[right - 1] < aa[left])))) {
        left++;
      }
      right++;
      ans = max(ans, right - left);
    }
  }
  printf("%d", ans);
  return 0;
}
