#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000001;
int a[maxn], b[maxn];
int no[maxn];
int n, m;
int f[2], ff[2];
int res;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[i + n] = a[i];
  for (int i = 1; i <= m; ++i) {
    scanf("%d", &b[i]), b[i + m] = b[i];
    no[b[i]] = i;
  }
  for (int i = 1; i <= n * 2; ++i)
    if (!no[a[i]])
      f[0] = f[1] = 0;
    else {
      ff[0] = ff[1] = 1;
      if (no[a[i]] > no[a[i - 1]]) {
        ff[0] = ((ff[0]) > (f[0] + 1) ? (ff[0]) : (f[0] + 1));
        ff[1] = ((ff[1]) > (f[1] + 1) ? (ff[1]) : (f[1] + 1));
      }
      ff[1] = ((ff[1]) > (f[0] + 1) ? (ff[1]) : (f[0] + 1));
      while (ff[0] != ff[1] && no[a[i]] >= no[a[i - ff[1] + 1]]) ff[1]--;
      f[0] = ff[0], f[1] = ff[1];
      res = ((res) > (f[0]) ? (res) : (f[0]));
      res = ((res) > (f[1]) ? (res) : (f[1]));
    }
  printf("%d\n", res);
  return 0;
}
