#include <bits/stdc++.h>
using namespace std;
int A[1000010];
int B[1000010];
long long pos[2000010];
int pos_in_B[1000010];
int main() {
  cin.sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; ++i) cin >> A[i];
  for (int i = 0; i < M; ++i) cin >> B[i];
  memset(pos_in_B, -1, sizeof(pos_in_B));
  for (int i = 0; i < M; ++i) pos_in_B[B[i]] = i;
  int ma = 0;
  int st = 0;
  for (int i = 0; i < 2 * N; ++i) {
    int id = i % N;
    if (i == 0)
      pos[i] = pos_in_B[A[id]];
    else {
      if (pos_in_B[A[id]] == -1)
        pos[i] = -1;
      else {
        long long b = (pos[i - 1] / M) * M + pos_in_B[A[id]];
        if (b <= pos[i - 1]) b += M;
        pos[i] = b;
      }
    }
    if (pos[i] == -1) {
      st = i + 1;
      continue;
    }
    st = max(st, i + 1 - N);
    int p = lower_bound(pos + st, pos + i + 1, pos[i] - M + 1) - pos;
    ma = max(ma, i - p + 1);
  }
  cout << ma << endl;
  return 0;
}
