#include <bits/stdc++.h>
using namespace std;
int an, bn;
int a[2000005], b[1000005];
int p[1000005];
int ans;
int main() {
  int l, r, len;
  cin >> an >> bn;
  for (int i = 0; i < an; i++) scanf("%d", &a[i]);
  for (int i = 0; i < an; i++) a[i + an] = a[i];
  for (int i = 0; i < bn; i++) scanf("%d", &b[i]);
  for (int i = 0; i <= 1000000; i++) p[i] = -1;
  for (int i = 0; i < bn; i++) p[b[i]] = i;
  len = 0;
  for (int i = 0; i < 2 * an; i++) {
    if (p[a[i]] == -1) {
      len = 0;
    } else {
      if (len == 0) {
        len = 1;
        l = r = p[a[i]];
        ans = max(ans, len);
      } else {
        int pos = p[a[i]];
        if (pos < l) pos += bn;
        if (r < l) r += bn;
        if (pos < l || pos > r) {
          r = pos;
          len++;
          ans = max(ans, len);
        } else {
          int old_l = l;
          do {
            len--;
            l = p[a[i - len]];
            if (l < old_l) l += bn;
          } while (len > 0 && pos >= l);
          r = pos;
          len++;
        }
        l = l % bn;
        r = r % bn;
      }
    }
  }
  cout << ans;
  return 0;
}
