#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
const int INF = 1000000000;
const long double EPS = 1E-9;
int n, m;
vector<int> a, pos;
int q[2000000], l = -1, r = -1;
int main() {
  scanf("%d%d", &n, &m);
  a.resize(n);
  for (int i = 0; i < (int)n; i++) {
    scanf("%d", &a[i]);
  }
  pos.assign(1000001, -1);
  for (int i = 0; i < (int)m; i++) {
    int v;
    scanf("%d", &v);
    pos[v] = i;
  }
  int res = 0, cur = 0, j = 0;
  for (int i = 0; i < (int)n; i++) {
    l++;
    cur--;
    if (l > r) {
      cur = 0;
      j = (i - 1 + n) % n;
      l = r + 1;
    }
    while (true) {
      j = (j + 1) % n;
      int to = pos[a[j]];
      bool can = true;
      if (to == -1) {
        can = false;
      } else {
        if (l <= r) {
          int st = q[l], fs = q[r];
          if (st <= fs && to >= st && to <= fs) can = false;
          if (st > fs && (to >= st || to <= fs)) can = false;
        }
      }
      if (!can) {
        j = (j - 1 + n) % n;
        break;
      } else {
        cur++;
        res = max(res, cur);
        q[++r] = to;
      }
    }
  }
  printf("%d", res);
}
