#include <bits/stdc++.h>
using namespace std;
int a[1000020], b[1000020];
int la, lb, i, head, tail, x, ans = 0, ii;
int l1, l2, r1, r2;
int que[2000010];
bool check(int x) {
  if (l1 == -1) {
    l1 = r1 = x;
    return true;
  }
  if (l2 == -1) {
    if (r1 < x) {
      r1 = x;
      return true;
    }
    if (x < l1) {
      l2 = r2 = x;
      return true;
    }
    return false;
  } else {
    if (r2 < x && x < l1) {
      r2 = x;
      return true;
    }
    return false;
  }
}
void pop() {
  if (head == tail) {
    l1 = r1 = l2 = r2 = -1;
    return;
  }
  head++;
  if (l1 < r1) {
    l1 = b[que[head + 1]];
  } else if (l1 == r1) {
    l1 = l2;
    r1 = r2;
    l2 = r2 = -1;
  }
}
int main() {
  scanf("%d%d", &la, &lb);
  for (i = 1; i <= la; i++) scanf("%d", &a[i]);
  for (i = 1; i <= 1000010; i++) b[i] = -1;
  for (i = 1; i <= lb; i++) {
    scanf("%d", &x);
    b[x] = i;
  }
  head = tail = 1;
  l1 = r1 = l2 = r2 = -1;
  for (i = 1; i <= (la << 1) - 1; i++) {
    if (i > la)
      ii = i - la;
    else
      ii = i;
    if (b[a[ii]] == -1) {
      head = tail = 1;
      l1 = r1 = l2 = r2 = -1;
    } else {
      while (!check(b[a[ii]])) pop();
      que[++tail] = a[ii];
      ans = max(ans, tail - head);
    }
  }
  printf("%d\n", ans);
  return 0;
}
