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;
int a[2000000 + 8] = {0};
int b[1000000 + 8] = {0};
int my[1000000 + 8] = {0};
int main() {
  int n = 0, m = 0, i = 0, ans = 0, now = 0, p = 0, pp = 0, qq = 0;
  while (scanf("%d%d", &n, &m) == 2) {
    memset(my, 0, sizeof(my));
    for (i = 1; i <= n; i++) {
      scanf("%d", a + i);
    }
    for (i = 1; i <= m; i++) {
      scanf("%d", b + i);
      my[b[i]] = i;
    }
    for (i = 1; i <= n; i++) {
      a[i] = my[a[i]];
      a[i + n] = a[i];
    }
    ans = 0;
    now = 0;
    p = 0;
    for (i = 1; i <= n + n; i++) {
      if (a[i]) {
        if (p == 0) {
          now = 1;
          p = i;
        } else {
          pp = a[p];
          qq = a[i - 1];
          while (true) {
            if (pp <= qq) {
              if (a[i] < pp || a[i] > qq) {
                break;
              }
            } else {
              if (a[i] > qq && a[i] < pp) {
                break;
              }
            }
            now--;
            p++;
            if (p == i) {
              break;
            }
            pp = a[p];
          }
          now++;
        }
      } else {
        now = 0;
        p = 0;
      }
      if (now > ans) {
        ans = now;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
