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 double eps = 1e-9;
const int int_inf = 2000000000;
const long long i64_inf = 1000000000000000000LL;
const double pi = acos(-1.0);
using namespace std;
int wa[1000010];
int wb[1000010];
int a[1000100];
int b[1000100];
int la, lb;
int res = 0;
int main() {
  memset(wa, -1, sizeof(wa));
  memset(wb, -1, sizeof(wb));
  cin >> la >> lb;
  for (int i = 0; i < (int)la; i++) scanf("%d", &a[i]), wa[a[i]] = i;
  for (int i = 0; i < (int)lb; i++) scanf("%d", &b[i]), wb[b[i]] = i;
  int first = wb[a[0]], last = first, len = 0;
  if (first != -1) {
    for (int i = 1; i < (int)la; i++) {
      if (wb[a[i]] == -1) {
        len = i;
        break;
      }
      if (first <= last)
        if (wb[a[i]] > last || wb[a[i]] < first) {
          last = wb[a[i]];
          continue;
        } else {
          len = i;
          break;
        }
      if (wb[a[i]] > last && wb[a[i]] < first) {
        last = wb[a[i]];
        continue;
      } else {
        len = i;
        break;
      }
    }
  }
  res = max(res, len);
  for (int i = 1; i < (int)la; i++) {
    if (len == 0) {
      if (wb[a[i]] == -1) continue;
      first = last = wb[a[i]];
      len = 1;
    } else {
      len--;
      first = wb[a[i]];
      if (first == -1) {
        len = 0;
        continue;
      }
    }
    int j = (i + len) % la;
    while (j != i) {
      if (wb[a[j]] == -1) break;
      if (first <= last)
        if (wb[a[j]] > last || wb[a[j]] < first) {
          last = wb[a[j]];
          len++;
          j = (j + 1) % la;
          continue;
        } else
          break;
      if (wb[a[j]] > last && wb[a[j]] < first) {
        last = wb[a[j]];
        len++;
        j = (j + 1) % la;
        continue;
      } else
        break;
    }
    res = max(res, len);
  }
  cout << res << endl;
  return 0;
}
