#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smn(T &a, const T &b) {
  if (b < a) a = b;
}
template <class T>
inline void smx(T &a, const T &b) {
  if (b > a) a = b;
}
template <class T>
inline T rev(const T &a) {
  T _ = a;
  reverse(_.begin(), _.end());
  return _;
}
const double eps = 1e-9;
const long double leps = 1e-14;
const int MN = 2 * 1000 * 1000 + 1000;
int n, m;
int f[MN], s[MN];
int p[MN];
int p2[MN];
pair<int, int> baze[MN];
int t, res;
set<int> st;
set<int>::iterator it;
void chk(int a) {
  st.clear();
  for (int i = baze[a].first; i <= baze[a].second; i++)
    st.insert(p[f[i]]), p2[p[f[i]]] = i;
  for (int i = baze[a + 1].first; i <= baze[a + 1].second; i++) {
    it = st.upper_bound(p[f[i]]);
    if (it == st.end()) continue;
    int t = p2[*it];
    int l = baze[a].second - t + 1;
    l += i - baze[a + 1].first + 1;
    smx(res, l);
  }
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> f[i];
  for (int i = 0; i < m; i++) cin >> s[i];
  for (int i = 0; i < n - 1; i++) f[n + i] = f[i];
  f[2 * n] = 1000001;
  memset(p, -1, sizeof(p));
  for (int i = 0; i < m; i++) p[s[i]] = i;
  int last = -1;
  for (int i = 0; i < 2 * n; i++)
    if (p[f[i]] == -1) {
      if (last != -1) baze[t++] = pair<int, int>(last, i - 1);
      last = -1;
      continue;
    } else {
      if (last == -1)
        last = i;
      else {
        if (p[f[i]] > p[f[i - 1]]) continue;
        baze[t++] = pair<int, int>(last, i - 1);
        last = i;
      }
    }
  res = 0;
  for (int i = 0; i < t; i++) smx(res, baze[i].second - baze[i].first + 1);
  for (int i = 0; i < t - 1; i++)
    if (baze[i].second + 1 == baze[i + 1].first) chk(i);
  cout << res << '\n';
  return 0;
}
