#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
long long arr1[1000000], arr2[1000000], mp[2000000];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> arr1[i];
  for (int i = 1; i <= 1000000; i++) mp[i] = -1;
  for (long long i = 0; i < m; i++) {
    cin >> arr2[i];
    mp[arr2[i]] = i;
  }
  deque<long long> di;
  long long hi = 0, maxi = 0, r = -1;
  for (long long i = 0; i < n; i++) {
    r = max(r, i - 1);
    long long asdf = 0;
    while (1) {
      if (mp[arr1[(r + 1) % n]] == -1) break;
      if (di.empty())
        di.push_back(mp[arr1[(r + 1) % n]] + hi), asdf++;
      else {
        if (mp[arr1[(r + 1) % n]] + hi > di[di.size() - 1])
          di.push_back(mp[arr1[(r + 1) % n]] + hi), asdf++;
        else if (mp[arr1[(r + 1) % n]] + m + hi > di[di.size() - 1] &&
                 mp[arr1[(r + 1) % n]] + hi < di[0])
          di.push_back(mp[arr1[(r + 1) % n]] + m + hi), asdf++;
        else
          break;
      }
      r++;
    }
    long long f = di.size();
    maxi = max(maxi, f);
    if (!di.empty()) {
      di.pop_front();
      if (!di.empty()) {
        if (di[0] >= hi + m) hi += m;
      }
    }
  }
  cout << maxi;
  return 0;
}
