#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const long long INF = ((1 << 31) - 1);
const long long LLINF = (((1LL << 63) - 1LL));
const double EPS = 1e-9;
const double PI = 3.14159265358979323846;
vector<int> first, second;
vector<int> whereInSecond;
const int MAXN = 1000 * 1000 + 4;
int moveInFirst(int r) {
  ++r;
  if (r == first.size()) {
    r = 0;
  }
  return r;
}
int moveInFirstBack(int r) {
  --r;
  if (r == -1) {
    r = (int)first.size() - 1;
  }
  return r;
}
int getLen(int l, int r) {
  int res = r - l + 1;
  res += first.size();
  while (res > first.size()) {
    res -= first.size();
  }
  return res;
}
bool isRightTriplet(int a, int b, int c) {
  if (a <= b) {
    return c <= a || c >= b;
  } else {
    return b <= c && c <= a;
  }
}
int solve(const vector<int>& _first, const vector<int>& _second) {
  first = _first;
  second = _second;
  whereInSecond.assign(MAXN, -1);
  for (int i = 0; i < second.size(); ++i) {
    whereInSecond[second[i]] = i;
  }
  int res = 0;
  int r = 0;
  for (int l = 0; l < first.size() && res != first.size(); ++l) {
    for (;;) {
      int val = first[r];
      if (whereInSecond[val] == -1) {
        if (l == r) {
          r = moveInFirst(r);
        }
        break;
      }
      bool ok = false;
      if (l == r || moveInFirst(l) == r) {
        ok = true;
      }
      if (!ok) {
        int a = whereInSecond[first[l]];
        int b = whereInSecond[first[moveInFirstBack(r)]];
        int c = whereInSecond[first[r]];
        if (isRightTriplet(a, b, c)) {
          ok = true;
        }
      }
      if (!ok) {
        break;
      }
      int candidate = getLen(l, r);
      res = max(res, candidate);
      if (res == first.size()) {
        break;
      }
      r = moveInFirst(r);
    }
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> first(n), second(m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &first[i]);
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", &second[i]);
  }
  int res = solve(first, second);
  cout << res << endl;
  return 0;
}
