#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000002;
int a[maxn * 2], v[maxn];
int main() {
  int n, m, tmp;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]), a[i + n] = a[i];
  for (int i = 0; i < m; ++i) scanf("%d", &tmp), v[tmp] = i + 1;
  int p = 0, t = 0, ans = 0;
  for (int i = 0; i < n; ++i) {
    if (p <= i || i >= t) p = i, t = 0;
    if (t == 0) {
      while (p - i + 1 <= n && v[a[p + 1]] > v[a[p]] &&
             v[a[p + 1]] - v[a[i]] + 1 <= m)
        ++p;
      if (p - i + 1 <= n && v[a[p + 1]] > 0 && v[a[p + 1]] < v[a[p]] &&
          v[a[p + 1]] - v[a[i]] + 1 <= 0)
        t = p + 1, p++;
    }
    if (t > 0) {
      while (p - i + 1 <= n && v[a[p + 1]] > v[a[p]] &&
             v[a[p + 1]] - v[a[i]] + 1 <= 0)
        ++p;
    }
    if (v[a[i]] > 0) ans = max(ans, p - i + 1);
  }
  cout << ans;
  return 0;
}
