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>
int hash[2000001], link[2000001];
int a[2000001], b[2000001];
int n, m, ans = 0;
int main() {
  int i, j, k;
  int tk = 0, bb;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; ++i) scanf("%d", a + i);
  for (i = 1; i <= m; ++i) {
    scanf("%d", b + i);
    hash[b[i]] = i;
  }
  for (i = 1; i <= n; ++i)
    if (hash[a[i]]) link[i] = hash[a[i]];
  for (i = 1; i <= n; ++i) {
    hash[a[i]] = 0;
    a[i + n] = a[i];
    link[i + n] = link[i];
  }
  bb = 1000000000;
  for (j = i = 1; i <= 2 * n; ++i) {
    if (i == 5) i = 5;
    if (link[i] == 0) {
      while (j <= i) hash[a[j]] = 0, ++j;
      continue;
    }
    hash[a[i]]++;
    if (hash[a[i]] > 1) {
      for (j; j < i; ++j) {
        hash[a[j]]--;
      }
    }
    if (i == j)
      tk = 0, bb = 1000000000;
    else {
      if (link[i] > link[i - 1]) {
        ;
      } else if (tk == 0) {
        tk = 1;
        bb = i;
      } else {
        while (j < bb) {
          hash[a[j]]--;
          ++j;
        }
        bb = i;
      }
    }
    if (tk) {
      while (link[i] > link[j] && j < i) {
        ++j;
        hash[a[j]]--;
        if (j >= bb) tk = 0, bb = 1000000000;
      }
    }
    ans = (ans > i - j + 1 ? ans : i - j + 1);
  }
  printf("%d\n", ans);
  return 0;
}
