#include <bits/stdc++.h>
using namespace std;
const int MAX = 2000010;
const int INF = 1000000001;
int t[MAX];
int next[MAX];
int where[MAX];
vector<int> V;
int n, m;
long long gnext(long long k, long long x) {
  long long ret = where[x % n];
  if (ret == -1) return (long long)INF * INF;
  for (int i = 30; i >= 0; --i)
    if (ret + (1LL << i) * n <= k) ret += (1LL << i) * n;
  while (ret <= k) ret += n;
  return ret;
}
int main(int argc, char *argv[]) {
  memset(t, -1, sizeof(t));
  memset(where, -1, sizeof(where));
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    --a;
    t[a] = i;
  }
  for (int i = 0; i < m; ++i) {
    int a;
    scanf("%d", &a);
    if (t[a - 1] != -1) V.push_back(t[a - 1]);
  }
  for (int i = 0; i < V.size(); ++i) where[V[i]] = i;
  int res = 0;
  deque<long long> D;
  for (int i = 0; i < n; ++i) {
    if (where[i] == -1) continue;
    if (D.empty()) {
      D.push_back(where[i]);
      long long p;
      for (int j = i + 1; (p = gnext(D.back(), j)) - D.front() < n; ++j)
        D.push_back(p);
      res = max(res, (int)D.size());
    } else {
      if (D.empty()) {
        D.push_back(where[i]);
        long long p;
        for (int j = i + 1; (p = gnext(D.back(), j)) - D.front() < n; ++j)
          D.push_back(p);
        res = max(res, (int)D.size());
      } else {
        long long p;
        for (int j = i + D.size(); (p = gnext(D.back(), j)) - D.front() < n;
             ++j)
          D.push_back(p);
        res = max(res, (int)D.size());
      }
    }
    D.pop_front();
  }
  printf("%d\n", res);
  return 0;
}
