1 #include <iostream>
   2 #include <sstream>
   3 #include <string>
   4 #include <algorithm>
   5 #include <vector>
   6 using namespace std;
   7 
   8 #define GI ({int _t; scanf("%d", &_t); _t;})
   9 #define FOR(i, a, b) for (int i=a; i<b; i++)
  10 #define REP(i, a) FOR(i, 0, a)
  11 template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();}
  12 int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
  13 #define DBGV(_v) { REP(_i, cnt) { cout << _v[_i] << "\t";} cout << endl;}
  14 int dp[21][21];
  15 
  16 int main() {
  17 	int cnt = GI, t;
  18 	int ans[21], cur[21];
  19 	REP(i, cnt) {
  20 		int t = GI;
  21 		ans[t-1] = i+1;
  22 	}
  23 	//DBGV(ans);
  24 	while (1) {
  25 		REP(i, cnt) {
  26 			if (cin >> t == false) return 0;
  27 			cur[t-1] = i+1;
  28 		}
  29 		//DBGV(cur);
  30 		REP(i, 21) REP(j,21) dp[i][j] = 0;
  31 		for (int i = cnt; i>=0 ; i--) {
  32 			for (int j= cnt; j>=0; j--) {
  33 				if (i== cnt || j == cnt) { dp[i][j] = 0; continue; }
  34 				if (ans[i] == cur[j]) {
  35 					dp[i][j] = dp[i+1][j+1] + 1;
  36 				}
  37 				else {
  38 					dp[i][j] = max(dp[i+1][j], dp[i][j+1]);
  39 				}
  40 			}
  41 		}
  42 		//REP(i, cnt+1) { REP(j, cnt+1) { cout << dp[i][j] << "\t"; } cout << endl; }
  43 		cout << dp[0][0] << endl;
  44 	}
  45 	return 0;
  46 }