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 <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <sstream>
#include <cctype>
#include <cmath>
using namespace std;
typedef long long LL;
#define FOR(x, b, e) for (int x = (b); x <= (e); ++x)
#define FORD(x, b, e) for (int x = (b); x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define EACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define REACH(i, c) for (VAR(i,(c).rbegin()); i != (c).rend(); ++i)
#define UNIQUE(v) do { sort(ALL(v)); (v).resize(unique(ALL(v)) - (v).begin()); } while (0)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define skip continue
const int INF = 1000000001;

#define GET(x)			(scanf("%d", &(x)) == 1)
#define GET2(x, y)		(scanf("%d %d", &(x), &(y)) == 2)
#define GET3(x, y, z)	(scanf("%d %d %d", &(x), &(y), &(z)) == 3)
#define GETS(x)			(scanf("%s", (x)) == 1)
#define DGET(x)			int x; GET(x);

template<class T>
bool remax(T &a, const T &b) { return a < b ? (a = b, 1) : 0; }
template<class T>
bool remin(T &a, const T &b) { return a > b ? (a = b, 1) : 0; }


const int N = 1000001;
int A[2 * N], posB[N];

int main()
{
	int la, lb, x;
	GET2(la, lb);
	
	FOR (i, 1, la) {
		GET(A[i]);
		A[i + la] = A[i];
	}
	
	FOR (i, 1, lb) {
		GET(x);
		posB[x] = i;
	}
	
	int best = 0;
	int lastMatch = 0;
	
	FOR (start, 1, la) {
		int sb = posB[A[start]];
		if (!sb) {
			skip;
		}
		
		remax(lastMatch, 0);
		
		int last = 0;
		int ini = start + max(1, lastMatch - 1);
		
		remin(ini, start + la - 1);
		
		FOR (i, ini, start + la - 1) {
			int p = posB[A[i]];
			remax(best, i - start);
			
			if (!p) {
				goto noMatch;
			}
			
			p -= sb;
			if (p < 0) p += lb;
			
			if (!remax(last, p)) {
				noMatch:
				lastMatch = i - start - 1;
				break;
			}
			++lastMatch;
		}
	}
	
	printf("%d\n", best);
}
