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[1000050];
int b[1000050];
int pos[1000050];
int main() {
  int mx = 0;
  int la, lb;
  cin >> la >> lb;
  for (int i = 0; i < la; i++) scanf("%d", a + i);
  memset(pos, -1, sizeof(pos));
  for (int i = 0; i < lb; i++) {
    scanf("%d", b + i);
    pos[b[i]] = i;
  }
  int first = 0;
  int kol = 0;
  vector<int> podstr(2 * la);
  int curl = 0;
  for (int i = 0; i < 2 * la - 1; i++) {
    int ps = pos[a[i % la]];
    if (ps == -1)
      kol = 0;
    else {
      if (kol == 0) {
        podstr[first] = ps;
        kol++;
        curl = 1;
        if (kol > mx) mx = kol;
      } else {
        int tmp = ps;
        if (podstr[first + kol - 1] > ps) ps += lb;
        curl += ps - podstr[first + kol - 1];
        podstr[first + kol] = tmp;
        kol++;
        while (curl > lb) {
          if (podstr[first] < podstr[first + 1])
            curl -= podstr[first + 1] - podstr[first];
          else
            curl -= lb + podstr[first + 1] - podstr[first];
          first++;
          kol--;
        }
        if (kol > mx) mx = kol;
        if (kol == la) break;
      }
    }
  }
  cout << mx << endl;
  return 0;
}
