#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
const double PI = acos(-1.0);
template <class T>
void debug(T a, T b) {
  for (T i = a; i != b; ++i) cout << *i << " ";
  cout << endl;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
int a[1000005], b[1000005];
int match[2000005];
pair<int, int> sa[1000005], sb[1000005];
long long int place[2000005];
int n, m;
long long int getoverK(long long int k, int i) {
  if (k % m < match[i])
    return match[i] + k / m * m;
  else
    return match[i] + k / m * m + m;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)n; ++i)
    scanf("%d", &a[i]), sa[i] = make_pair(a[i], i);
  for (int i = 0; i < (int)m; ++i)
    scanf("%d", &b[i]), sb[i] = make_pair(b[i], i);
  sort(sa, sa + n);
  sort(sb, sb + m);
  for (int i = 0; i < (int)n; ++i) {
    int p = lower_bound(sb, sb + m, make_pair(a[i], -1)) - sb;
    if (p < m && sb[p].first == a[i])
      match[i] = sb[p].second;
    else
      match[i] = -1;
  }
  for (int i = 0; i < (int)n; ++i) match[i + n] = match[i];
  int res = 0, begin2 = -1;
  long long int begin, end;
  for (int i = 0; i < (int)n * 2; ++i) {
    if (match[i] == -1) {
      begin2 = -1;
      continue;
    }
    if (begin2 == -1) {
      begin = end = match[i];
      place[i] = match[i];
      res = max(res, 1);
      begin2 = i;
      continue;
    }
    long long int nxt = getoverK(end, i);
    while (nxt - begin >= m) {
      ++begin2;
      begin = place[begin2];
    }
    place[i] = nxt;
    end = nxt;
    res = max(res, i - begin2 + 1);
  }
  printf("%d\n", res);
  return 0;
}
