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 limite = 1000001;
int a[limite];
int posicion[limite];
int main() {
  ios_base::sync_with_stdio(false);
  int na, nb;
  cin >> na >> nb;
  for (int i = 1; i <= na; i++) cin >> a[i];
  for (int i = 1; i <= nb; i++) {
    int b;
    cin >> b;
    posicion[b] = i;
  }
  int ii = 1;
  int len = 1;
  int maximo = 0;
  for (int i = 1; i <= na; i++) {
    len = max(len - 1, 1);
    if (posicion[a[i]] == 0) {
      ii = i + 1;
      len = 1;
      continue;
    }
    for (;;) {
      int nextii = (ii + 1);
      if (nextii > na) nextii = 1;
      if (nextii == i) break;
      if (posicion[a[nextii]] == 0) break;
      if (i == ii) {
        ii = nextii;
        len = 2;
      } else {
        int pi = posicion[a[i]];
        int pii = posicion[a[ii]];
        int pnextii = posicion[a[nextii]];
        if (pi < pii and pi < pnextii and pnextii < pii) break;
        if (pii < pi and pi < pnextii) break;
        if (pii < pi and pnextii < pii) break;
        ii = nextii;
        len++;
      }
    }
    maximo = max(maximo, len);
  }
  cout << maximo << endl;
}
