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>
const int N = 1001000;
int t[N], s[N], n, m;
int cnt[N], co[N];
int ans;
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
void check() {
  int L = 1, R = 1;
  int first = co[L], last = co[R];
  while (L <= n && R < 2 * n) {
    if (first == 0) {
      R = ++L;
      last = first = co[L];
      continue;
    }
    while (L <= n && R < 2 * n) {
      if (R - L + 1 > ans) ans = R - L + 1;
      ++R;
      if (co[R] == 0) {
        L = ++R;
        last = first = co[L];
        break;
      }
      if (first <= last) {
        if (co[R] > last)
          last = co[R];
        else if (co[R] < first)
          last = co[R];
        else {
          while (co[++L] < co[R])
            ;
          first = co[L];
          last = co[R];
        }
      } else {
        if (co[R] > last && co[R] < first)
          last = co[R];
        else if (last > co[R]) {
          while (1) {
            ++L;
            if (co[L] > co[R] && co[L] <= last) break;
          }
          first = co[L];
          last = co[R];
        } else if (co[R] > first) {
          while (1) {
            ++L;
            if (co[L] > co[R] || co[L] <= last) break;
          }
          first = co[L];
          last = co[R];
        }
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t[i]);
    cnt[t[i]] = i;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &s[i]);
    if (cnt[s[i]] != 0) co[cnt[s[i]] + n] = co[cnt[s[i]]] = i;
  }
  check();
  if (ans >= min(n, m)) ans = min(n, m);
  printf("%d\n", ans);
  scanf(" ");
}
