#include <bits/stdc++.h>
using namespace std;
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, vector<T> t) {
  s << "{";
  for (int i = 0; i < t.size(); i++)
    s << t[i] << (i == t.size() - 1 ? "" : ",");
  return s << "}" << endl;
}
int A, B;
vector<int> a, b;
int m[1000002];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> A >> B;
  a.resize(A);
  b.resize(B);
  for (int i = 0; i < 1000002; i++) m[i] = -1;
  for (int i = 0; i < A; i++) cin >> a[i];
  for (int i = 0; i < B; i++) cin >> b[i];
  for (int i = 0; i < B; i++) m[b[i]] = i;
  for (int i = 0; i < A; i++) a.push_back(a[i]);
  int j = 0;
  deque<long long> Q;
  int wyn = 0;
  int ile = 0;
  for (int i = 0; i < A; i++) {
    j = Q.size();
    while (j < min(A, B)) {
      ile++;
      if (ile > 5000000) return 1;
      if (m[a[i + j]] == -1) {
        break;
      }
      int pos = m[a[i + j]];
      if (Q.size() == 0)
        Q.push_front(pos);
      else {
        long long pr = Q.front();
        long long tyl = Q.back();
        long long q = (tyl) / B;
        long long moj = pos + q * B;
        while (moj <= tyl) moj += B;
        if (moj - pr < B)
          Q.push_back(moj);
        else {
          break;
        }
      }
      j++;
    }
    (wyn) = max((wyn), (j));
    if (Q.size()) Q.pop_front();
  }
  cout << wyn;
  return 0;
}
