1 1
1
2
 6 3
1 2 3 4 5 6
3 5 1
 5 5
5 6 3 2 1
4 3 1 5 6
 1 1
1
1
 10 10
1 2 3 4 5 6 7 8 9 10
6 7 8 9 10 1 2 3 4 5
 1 2
1
1 2
 10 10
9 12 15 2 4 14 1 5 6 13
12 11 13 1 4 3 8 2 14 15
 9 9
1 2 3 4 5 6 7 8 9
7 8 9 1 2 4 5 6 3
 5 5
9 2 5 1 3
1 7 2 3 9

0
 1
 4
 1
 10
 1
 3
 8
 3

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
template <class T>
bool remax(T &a, const T &b) {
  return a < b ? (a = b, 1) : 0;
}
template <class T>
bool remin(T &a, const T &b) {
  return a > b ? (a = b, 1) : 0;
}
const int N = 1000001;
int A[2 * N], posB[N];
int main() {
  int la, lb, x;
  (scanf("%d %d", &(la), &(lb)) == 2);
  for (int i = (1); i <= (la); ++i) {
    (scanf("%d", &(A[i])) == 1);
    A[i + la] = A[i];
  }
  for (int i = (1); i <= (lb); ++i) {
    (scanf("%d", &(x)) == 1);
    posB[x] = i;
  }
  int best = 0;
  int lastMatch = 0;
  for (int start = (1); start <= (la); ++start) {
    int sb = posB[A[start]];
    if (!sb) {
      lastMatch = 0;
      continue;
    }
    remax(lastMatch, 0);
    int last = 0;
    int ini = start + max(1, lastMatch - 1);
    remin(ini, start + la - 1);
    for (int i = (ini); i <= (start + la - 1); ++i) {
      int p = posB[A[i]];
      remax(best, i - start - 1);
      if (!p) {
        lastMatch = 0;
        break;
      }
      p -= sb;
      if (p < 0) p += lb;
      if (!remax(last, p)) {
        lastMatch = i - start - 1;
        break;
      }
      ++lastMatch;
    }
  }
  printf("%d\n", best + !!best);
}
