#include <bits/stdc++.h>
using namespace std;
int vs[2000000], vt[2000000], s[2000000], t[2000000];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= 1000000; ++i) {
    vs[i] = -1;
    vt[i] = -1;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", s + i);
    vs[s[i]] = i;
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d", t + i);
    vt[t[i]] = i;
  }
  if (n == 1) {
    cout << ((vt[s[0]] != -1) ? 1 : 0) << endl;
    return 0;
  }
  if (m == 1) {
    cout << ((vs[t[0]] != -1) ? 1 : 0) << endl;
    return 0;
  }
  int r = -1;
  int ans = 0;
  for (int l = 0; l < n; ++l) {
    if (vt[s[l % n]] == -1) {
      r = l;
    } else {
      if (r < l) {
        r = l;
      }
      if (r == l) {
        if (vt[s[(r + 1) % n]] != -1) ++r;
      }
      if (r != l) {
        while (1) {
          if (r + 1 >= n + l) break;
          int lsp = vt[s[l]];
          int nsp = vt[s[(r + 1) % n]];
          int rsp = vt[s[r % n]];
          if (nsp == -1) break;
          if (nsp < rsp) {
            if ((lsp > rsp) || (lsp < nsp)) {
              break;
            }
          } else {
            if ((rsp < lsp) && (nsp > lsp)) {
              break;
            }
          }
          ++r;
        }
      }
      if (r - l + 1 > ans) ans = (r - l + 1);
    }
  }
  cout << ans << endl;
  return 0;
}
