/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = INF + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int LOGN = 21;
const int N = 1 << LOGN;

int n;
int a[N];

bool read () {
	if (scanf("%d", &n) != 1)
		return false;
	forn(i, n)
		scanf("%d", &a[i]);
	return true;
}

pt amask[N];
int ok[N];

void solve() {
	memset(amask, -1, sizeof(amask));
	forn(i, n) amask[a[i]].x = i;

	for (int mask = N - 1; mask >= 0; --mask) forn(i, LOGN) if ((mask >> i) & 1){
		int nmask = mask ^ (1 << i);
		if (amask[mask].x > amask[nmask].x){
			int k = max(amask[mask].y, amask[nmask].x);
			if (k < amask[mask].x)
				amask[nmask].y = k;
			else
				amask[nmask].y = min(amask[mask].y, amask[nmask].x);
			amask[nmask].x = amask[mask].x;
		}
		else if (amask[mask].x < amask[nmask].x){
			int k = max(amask[nmask].y, amask[mask].x);
			if (k < amask[nmask].x)
				amask[nmask].y = k;
			else
				amask[nmask].y = min(amask[nmask].y, amask[mask].x);
		}
		else{
			amask[nmask].y = max(amask[mask].y, amask[nmask].y);
		}
	}

	forn(mask, N)
		ok[mask] = amask[mask].y;

	int ans = 0;
	forn(i, n){
		int res = 0;
		forn(j, LOGN){
			if ((a[i] >> j) & 1) continue;
			int nres = res | (1 << j);
			if (ok[nres] > i)
				res = nres;
		}
		ans = max(ans, res | a[i]);
	}
	printf("%d\n", ans);
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

	while(read()) {
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = INF + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int LOGN = 21;
const int N = 1 << LOGN;

int n;
int a[N];

bool read () {
	if (scanf("%d", &n) != 1)
		return false;
	forn(i, n)
		scanf("%d", &a[i]);
	return true;
}

pt amask[N];
int okl[N], okr[N];

void solve() {
	memset(amask, -1, sizeof(amask));
	forn(i, n){
		amask[a[i]].y = amask[a[i]].x;
		amask[a[i]].x = i;
	}
	forn(mask, N) forn(i, LOGN) if (!((mask >> i) & 1)){
		int nmask = mask ^ (1 << i);
		if (amask[mask].x > amask[nmask].x){
			amask[nmask].y = max(amask[mask].y, amask[nmask].x);
			amask[nmask].x = amask[mask].x;
		}
		else{
			amask[nmask].y = max(amask[nmask].y, amask[mask].x);
		}
	}

	forn(mask, N)
		okr[mask] = amask[mask].y;
	for (int mask = N - 1; mask >= 0; --mask) forn(i, LOGN) if (!((mask >> i) & 1)){
		int nmask = mask ^ (1 << i);
		okr[nmask] = max(okr[nmask], okr[mask]);
	}

	forn(mask, N)
		okl[mask] = n;
	for (int i = n - 1; i >= 0; --i)
		okl[a[i]] = i;
	for (int mask = N - 1; mask >= 0; --mask) forn(i, LOGN) if (!((mask >> i) & 1)){
		int nmask = mask ^ (1 << i);
		okl[nmask] = min(okl[nmask], okl[mask]);
	}

	int ans = 0;
	forn(mask, N){
		//cerr << mask << " " << okl[mask] << " " << okr[mask] << endl;
		if (okl[mask] <= okr[mask])
			ans = mask;
	}
	printf("%d\n", ans);
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

	while(read()) {
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = INF + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int LOGN = 21;
const int N = 1 << LOGN;

int n;
int a[N];

bool read () {
	if (scanf("%d", &n) != 1)
		return false;
	forn(i, n)
		scanf("%d", &a[i]);
	return true;
}

pt amask[N];
int ok[N];

void solve() {
	memset(amask, -1, sizeof(amask));
	forn(i, n){
		amask[a[i]].y = amask[a[i]].x;
		amask[a[i]].x = i;
	}

	for (int mask = N - 1; mask >= 0; --mask) forn(i, LOGN) if ((mask >> i) & 1){
		int nmask = mask ^ (1 << i);
		if (amask[mask].x > amask[nmask].x){
			int k = max(amask[mask].y, amask[nmask].x);
			if (k < amask[mask].x)
				amask[nmask].y = k;
			else
				amask[nmask].y = min(amask[mask].y, amask[nmask].x);
			amask[nmask].x = amask[mask].x;
		}
		else if (amask[mask].x < amask[nmask].x){
			int k = max(amask[nmask].y, amask[mask].x);
			if (k < amask[nmask].x)
				amask[nmask].y = k;
			else
				amask[nmask].y = min(amask[nmask].y, amask[mask].x);
		}
		else{
			amask[nmask].y = max(amask[mask].y, amask[nmask].y);
		}
	}

	forn(mask, N)
		ok[mask] = amask[mask].y;

	int ans = 0;
	forn(i, n){
		int res = 0;
		for (int j = LOGN - 1; j >= 0; --j){
			if ((a[i] >> j) & 1) continue;
			int nres = res | (1 << j);
			if (ok[nres] > i)
				res = nres;
		}
		ans = max(ans, res | a[i]);
	}
	printf("%d\n", ans);
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

	while(read()) {
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = INF + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int LOGN = 21;
const int N = 1 << LOGN;

int n;
int a[N];

bool read () {
	if (scanf("%d", &n) != 1)
		return false;
	forn(i, n)
		scanf("%d", &a[i]);
	return true;
}

pt amask[N];
int ok[N];

void solve() {
	memset(amask, -1, sizeof(amask));
	forn(i, n){
		amask[a[i]].y = amask[a[i]].x;
		amask[a[i]].x = i;
	}

	for (int mask = N - 1; mask >= 0; --mask) forn(i, LOGN) if ((mask >> i) & 1){
		int nmask = mask ^ (1 << i);
		if (amask[mask].x > amask[nmask].x){
			int k = max(amask[mask].y, amask[nmask].x);
			if (k < amask[mask].x)
				amask[nmask].y = k;
			else
				amask[nmask].y = min(amask[mask].y, amask[nmask].x);
			amask[nmask].x = amask[mask].x;
		}
		else{
			int k = max(amask[nmask].y, amask[mask].x);
			if (k < amask[nmask].x)
				amask[nmask].y = k;
			else
				amask[nmask].y = min(amask[nmask].y, amask[mask].x);
		}
	}

	forn(mask, N)
		ok[mask] = amask[mask].y;

	int ans = 0;
	forn(i, n){
		int res = 0;
		forn(j, LOGN){
			if ((a[i] >> j) & 1) continue;
			int nres = res | (1 << j);
			if (ok[nres] > i)
				res = nres;
		}
		ans = max(ans, res | a[i]);
	}
	printf("%d\n", ans);
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

	while(read()) {
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
const int N = 1e6+9 , M = 1<<21 , bit = 20 ;
int arr[N],pw2[bit+1];
pii mx[M];
pii storemx(pii a,pii b)
{
    if(b.fi>a.fi)
    {
        a.se=a.fi,a.fi=b.fi;
        if(b.se>a.se)a.se=b.se;
    }
    else if(b.fi>a.se)a.se=b.fi;
    return a ;
}
bool check(int mask,int idx)
{
    int fd = (mask&arr[idx])^mask;
    return mx[fd].se>=idx;
}
int compute(int idx)
{
    int ret = 0 ;
    for(int i=bit;i>=0;i--)///1<<i
    {
        if(check(ret+(1<<i),idx))
        {
            ret+=1<<i;
        }
    }
    return ret ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    int n ;
    cin >> n ;
    for(int i=0;i<=bit;i++)pw2[i]=1<<i;
    for(int i=1;i<=n;i++)
    {
        cin >> arr[i];
        mx[arr[i]]=storemx(mx[arr[i]],{i,0});
    }
    for(int mask=M-1;mask>0;mask--)
    {
        for(int i=0;i<=bit;i++)
        {
            if(mask&pw2[i])
            {
                if(mx[mask].fi!=0)mx[mask-pw2[i]]=storemx(mx[mask-pw2[i]],mx[mask]);
            }
        }
    }
    int ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        ans = max(ans,compute(i));
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
const int N = 1e6+9 , M = 1<<21 , bit = 20 ;
int arr[N],pw2[bit+1];
pii mx[M];
pii storemx(pii a,pii b)
{
    if(b.fi>a.fi)
    {
        a.se=a.fi,a.fi=b.fi;
        if(b.se>a.se)a.se=b.se;
    }
    else if(b.fi>a.se)a.se=b.fi;
    return a ;
}
bool check(int mask,int idx)
{
    int fd = (mask&arr[idx])^mask;
    return mx[fd].se>=idx;
}
int compute(int idx)
{
    int ret = 0 ;
    for(int i=bit;i>=0;i--)///1<<i
    {
        if(check(ret+pw2[i],idx))
        {
            ret+=pw2[i];
        }
    }
    return ret ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    int n ;
    cin >> n ;
    for(int i=0;i<=bit;i++)pw2[i]=1<<i;
    for(int mask=0;mask<M;mask++)mx[mask]={0,0};
    for(int i=1;i<=n;i++)
    {
        cin >> arr[i];
        mx[arr[i]]=storemx(mx[arr[i]],{i,0});
    }
    for(int mask=M-1;mask>0;mask--)
    {
        for(int i=0;i<=bit;i++)
        {
            if(mask&pw2[i])
            {
                mx[mask-pw2[i]]=storemx(mx[mask-pw2[i]],mx[mask]);
            }
        }
    }
    int ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        ans = max(ans,compute(i));
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
const int N = 1e6+9 , M = 1<<21 , bit = 20 ;
int arr[N],pw2[bit+1];
pii mx[M];
pii storemx(pii a,pii b)
{
    if(b.fi>a.fi)
    {
        a.se=a.fi,a.fi=b.fi;
        if(b.se>a.se)a.se=b.se;
    }
    else if(b.fi>a.se)a.se=b.fi;
    return a ;
}
bool check(int mask,int idx)
{
    int fd = (mask&arr[idx])^mask;
    return mx[fd].se>idx;
}
int compute(int idx)
{
    int ret = 0 ;
    for(int i=bit;i>=0;i--)///1<<i
    {
        if(check(ret+pw2[i],idx))
        {
            ret+=pw2[i];
        }
    }
    return ret ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    int n ;
    cin >> n ;
    for(int i=0;i<=bit;i++)pw2[i]=1<<i;
    for(int mask=0;mask<M;mask++)mx[mask]={0,0};
    for(int i=1;i<=n;i++)
    {
        cin >> arr[i];
        mx[arr[i]]=storemx(mx[arr[i]],{i,0});
    }
    for(int mask=M-1;mask>0;mask--)
    {
        for(int i=0;i<=bit;i++)
        {
            if(mask&pw2[i])
            {
                mx[mask-pw2[i]]=storemx(mx[mask-pw2[i]],mx[mask]);
            }
        }
    }
    int ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        ans = max(ans,compute(i));
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
const int N = 1e6+9 , M = 1<<21 , bit = 20 ;
int arr[N],pw2[bit+1];
pii mx[M];
pii storemx(pii a,pii b)
{
    if(b.fi>a.fi)
    {
        a.se=a.fi,a.fi=b.fi;
        if(b.se>a.se)a.se=b.se;
    }
    else if(b.fi>a.se&&b.fi!=a.fi)a.se=b.fi;
    return a ;
}
bool check(int mask,int idx)
{
    int fd = (mask&arr[idx])^mask;
    return mx[fd].se>idx;
}
int compute(int idx)
{
    int ret = 0 ;
    for(int i=bit;i>=0;i--)///1<<i
    {
        if(check(ret+pw2[i],idx))
        {
            ret+=pw2[i];
        }
    }
    return ret ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    int n ;
    cin >> n ;
    for(int i=0;i<=bit;i++)pw2[i]=1<<i;
    for(int mask=0;mask<M;mask++)mx[mask]={0,0};
    for(int i=1;i<=n;i++)
    {
        cin >> arr[i];
        mx[arr[i]]=storemx(mx[arr[i]],{i,0});
    }
    for(int mask=M-1;mask>0;mask--)
    {
        for(int i=0;i<=bit;i++)
        {
            if(mask&pw2[i])
            {
                mx[mask-pw2[i]]=storemx(mx[mask-pw2[i]],mx[mask]);
            }
        }
    }
    int ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        ans = max(ans,compute(i));
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
const int N = 1e6+9 , M = 1<<21 , bit = 20 ;
int arr[N];
pii mx[M];
pii storemx(pii a,pii b)
{
    vector<int> V ;
    V.emplace_back(a.fi);
    V.emplace_back(a.se);
    V.emplace_back(b.fi);
    V.emplace_back(b.se);
    sort(V.begin(),V.end(),greater<int>());
    return {V[0],V[1]};
}
bool check(int mask,int idx)
{
    int fd = (mask&arr[idx])^mask;
    return mx[fd].se>=idx;
}
int compute(int idx)
{
    int ret = 0 ;
    for(int i=bit;i>=0;i--)///1<<i
    {
        if(check(ret+(1<<i),idx))
        {
            ret+=1<<i;
        }
    }
    return ret ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    int n ;
    cin >> n ;
    for(int i=1;i<=n;i++)
    {
        cin >> arr[i];
        mx[arr[i]]=storemx(mx[arr[i]],{i,0});
    }
    for(int mask=M-1;mask>0;mask--)
    {
        for(int i=0;i<=bit;i++)
        {
            if(mask&(1<<i))
            {
                if(mx[mask].fi!=0)mx[mask-(1<<i)]=storemx(mx[mask-(1<<i)],mx[mask]);
            }
        }
    }
    int ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        ans = max(ans,compute(i));
        //printf()
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
const int N = 1e6+9 , M = 1<<21 , bit = 20 ;
int arr[N],pw2[bit+1];
pii mx[M];
pii storemx(pii a,pii b)
{
    if(b.fi>a.fi)
    {
        a.se=a.fi,a.fi=b.fi;
        if(b.se>a.se)a.se=b.se;
    }
    else if(b.fi>a.se&&b.fi!=a.fi)a.se=b.fi;
    else if(b.se>a.se)a.se=b.se;
    return a ;
}
bool check(int mask,int idx)
{
    int fd = (mask&arr[idx])^mask;
    return mx[fd].se>idx;
}
int compute(int idx)
{
    int ret = 0 ;
    for(int i=bit;i>=0;i--)///1<<i
    {
        if(check(ret+pw2[i],idx))
        {
            ret+=pw2[i];
        }
    }
    return ret ;
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0);
    int n ;
    cin >> n ;
    for(int i=0;i<=bit;i++)pw2[i]=1<<i;
    for(int mask=0;mask<M;mask++)mx[mask]={0,0};
    for(int i=1;i<=n;i++)
    {
        cin >> arr[i];
        mx[arr[i]]=storemx(mx[arr[i]],{i,0});
    }
    for(int mask=M-1;mask>0;mask--)
    {
        for(int i=0;i<=bit;i++)
        {
            if(mask&pw2[i])
            {
                mx[mask-pw2[i]]=storemx(mx[mask-pw2[i]],mx[mask]);
            }
        }
    }
    int ans = 0 ;
    for(int i=1;i<=n;i++)
    {
        ans = max(ans,compute(i));
    }
    cout << ans << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 3e6 + 6;
const int M = 21;

int main() {

	int n;
	int a[N];

	cin >> n;
	for(int i=0; i<n; i++)
		scanf("%d", &a[i]);

	int pos[N][2];
	memset(pos, -1, sizeof(pos));
	for(int i=n-1; i>=0; i--) {
		if(pos[a[i]][0] == -1)
			pos[a[i]][0] = i;
		else if(pos[a[i]][1] == -1)
			pos[a[i]][1] = i;
	}

	for(int i=M-1; i>=0; i--)
		for(int mask = (1<<M) -1; mask >= 0; mask--)
			if(mask & (1<<i)) {
				int mx1 = -1, mx2 = -1;

				for(int k=0; k<2; k++) {
					if(pos[mask ^ (1<<i)][k] > mx1) {
						mx2 = mx1;
						mx1 = pos[mask ^ (1<<i)][k];
					}
					else if(pos[mask ^ (1<<i)][k] > mx2)
						mx2 = pos[mask ^ (1<<i)][k];
				}
				for(int k=0; k<2; k++) {
					if(pos[mask][k] > mx1) {
						mx2 = mx1;
						mx1 = pos[mask][k];
					}
					else if(pos[mask][k] > mx2)
						mx2 = pos[mask][k];
				}

				pos[mask ^ (1<<i)][0] = mx1;
				pos[mask ^ (1<<i)][1] = mx2;
			}

	// for(int i=0; i<16; i++)
	// 	printf("%d : %d\n", i, pos[i][1]);

	int ans = -1;
	for(int i=0; i<n; i++) {
		int mask = (1<<M) -1;
		mask ^= a[i];
		int pattern = 0;
		for(int bit=M-1; bit>=0; bit--)
			if(mask & (1<<bit))
				if(pos[pattern | (1<<bit)][1] > i)
					pattern |= (1<<bit);

		ans = max(ans, pattern | a[i]);
	}

	printf("%d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;

const int N = 3e6 + 6;
const int M = 21;

int main() {

	int n;
	int a[N];

	cin >> n;
	for(int i=0; i<n; i++)
		scanf("%d", &a[i]);

	int pos[N][2];
	memset(pos, -1, sizeof(pos));
	for(int i=n-1; i>=0; i--) {
		if(pos[a[i]][0] == -1)
			pos[a[i]][0] = i;
		else if(pos[a[i]][1] == -1)
			pos[a[i]][1] = i;
	}

	for(int i=M-1; i>=0; i--)
		for(int mask = (1<<M) -1; mask >= 0; mask--)
			if(mask & (1<<i)) {
				int mx1 = -1, mx2 = -1;

				for(int k=0; k<2; k++) {
					if(pos[mask ^ (1<<i)][k] > mx1) {
						mx2 = mx1;
						mx1 = pos[mask ^ (1<<i)][k];
					}
					else if(pos[mask ^ (1<<i)][k] > mx2 && pos[mask ^ (1<<i)][k] < mx1)
						mx2 = pos[mask ^ (1<<i)][k];
				}
				for(int k=0; k<2; k++) {
					if(pos[mask][k] > mx1) {
						mx2 = mx1;
						mx1 = pos[mask][k];
					}
					else if(pos[mask][k] > mx2 && pos[mask][k] < mx1)
						mx2 = pos[mask][k];
				}

				pos[mask ^ (1<<i)][0] = mx1;
				pos[mask ^ (1<<i)][1] = mx2;
			}

	// for(int i=0; i<16; i++)
	// 	printf("%d : %d\n", i, pos[i][1]);

	int ans = -1;
	for(int i=0; i<n-2; i++) {
		int mask = (1<<M) -1;
		mask ^= a[i];
		int pattern = 0;
		for(int bit=M-1; bit>=0; bit--)
			if(mask & (1<<bit))
				if(pos[pattern | (1<<bit)][1] > i)
					pattern |= (1<<bit);

		// printf(">> %d : %d\n", a[i], pattern);
		ans = max(ans, pattern | a[i]);
	}

	printf("%d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;

const int N = 3e6 + 6;
const int M = 21;

int main() {

	int n;
	int a[N];

	cin >> n;
	for(int i=0; i<n; i++)
		scanf("%d", &a[i]);

	int pos[N][2];
	memset(pos, -1, sizeof(pos));
	for(int i=n-1; i>=0; i--) {
		if(pos[a[i]][0] == -1)
			pos[a[i]][0] = i;
		else if(pos[a[i]][1] == -1)
			pos[a[i]][1] = i;
	}

	for(int i=M-1; i>=0; i--)
		for(int mask = (1<<M) -1; mask >= 0; mask--)
			if(mask & (1<<i)) {
				multiset<int> ind;
				ind.insert(pos[mask ^ (1<<i)][0]);
				ind.insert(pos[mask ^ (1<<i)][1]);
				ind.insert(pos[mask][0]);
				ind.insert(pos[mask][1]);

				auto it = ind.end();
				it--;
				pos[mask ^ (1<<i)][0] = *it;
				it--;
				pos[mask ^ (1<<i)][1] = *it;
			}

	// for(int i=0; i<16; i++)
	// 	printf("%d : %d\n", i, pos[i][1]);

	int ans = -1;
	for(int i=0; i<n; i++) {
		int mask = (1<<M) -1;
		mask ^= a[i];
		int pattern = 0;
		for(int bit=M-1; bit>=0; bit--)
			if(mask & (1<<bit))
				if(pos[pattern | (1<<bit)][1] > i)
					pattern |= (1<<bit);

		ans = max(ans, pattern | a[i]);
	}

	printf("%d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;

const int N = 3e6 + 6;
const int M = 22;

int main() {

	int n;
	int a[N];

	cin >> n;
	for(int i=0; i<n; i++)
		scanf("%d", &a[i]);

	int pos[N][2];
	memset(pos, -1, sizeof(pos));
	for(int i=n-1; i>=0; i--) {
		if(pos[a[i]][0] == -1)
			pos[a[i]][0] = i;
		else if(pos[a[i]][1] == -1)
			pos[a[i]][1] = i;
	}

	for(int i=M-1; i>=0; i--)
		for(int mask = (1<<M) -1; mask >= 0; mask--)
			if(mask & (1<<i)) {
				multiset<int> ind;
				ind.insert(pos[mask ^ (1<<i)][0]);
				ind.insert(pos[mask ^ (1<<i)][1]);
				ind.insert(pos[mask][0]);
				ind.insert(pos[mask][1]);

				auto it = ind.end();
				it--;
				pos[mask ^ (1<<i)][0] = *it;
				it--;
				pos[mask ^ (1<<i)][1] = *it;
			}

	// for(int i=0; i<16; i++)
	// 	printf("%d : %d\n", i, pos[i][1]);

	int ans = -1;
	for(int i=0; i<n; i++) {
		int mask = (1<<M) -1;
		mask ^= a[i];
		int pattern = 0;
		for(int bit=M-1; bit>=0; bit--)
			if(mask & (1<<bit))
				if(pos[pattern | (1<<bit)][1] > i)
					pattern |= (1<<bit);

		ans = max(ans, pattern | a[i]);
	}

	printf("%d\n", ans);
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class C {
    static String[] first;
    static String[] second;
    static int[] wanted;
    static int n;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader( new InputStreamReader(System.in) );
        out = new PrintWriter(System.out);
        st = new StringTokenizer("");
        sb = new StringBuilder();

        n = nxtInt();
        first = new String[n];
        second = new String[n];

        for( int i = 0;i < n;i++){
            String line = br.readLine();
            String[] tokens = line.split(" ");
            first[i] = tokens[0];
            second[i] = tokens[1];
        }

        wanted = new int[n];
        for( int i = 0;i < n;i++){
            int t = nxtInt();
            wanted[i] = t-1;
        }//end for.

        ArrayList<String> em = new ArrayList<String>();
//      System.out.println( calc( 0 , em ) );

        TreeSet<String> t = new TreeSet<String>();
        boolean check = calc(0,t);

        if( check ){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }//end main.

    static boolean check( TreeSet<String> t){

        Iterator it = t.iterator();
        int i = 0;
        while( it.hasNext() ){
            String h = (String) it.next();
            if( first[wanted[i]].equals( h ) || second[wanted[i]].equals( h ) ){

            }else{
                return false;
            }
            i++;
        }//end while.

        return true;

    }//end method.

    static boolean calc( int index, TreeSet<String> t ){

        if( index == n ){
            return check( t );
        }

        boolean ret = false;
        boolean check;
//      TreeSet<String> temp = (TreeSet<String>) t.clone();
        t.add( first[ wanted[ index ] ] );
        check = check( t );
//      temp.add( first[ wanted[ index ] ] );
//      check = check(  temp);

        if( check ){
            ret = calc( index + 1, t );
        }

        t.remove( first[ wanted[ index ] ] );
        t.add(  second[ wanted[index] ] );

//      temp = (TreeSet<String>) t.clone();
//      t.clear();
//      temp.add( second[ wanted[index] ] );

        //(String[])
//      check = check(  temp );
        check = check( t );
        if( check ){
            ret = ret | calc( index + 1, t );
        }
        t.remove(  second[ wanted[index] ] );
        return ret;
    }//end method.

    static BufferedReader br;
    static StringTokenizer st;
    static StringBuilder sb;
    static PrintWriter out;

    static String nxtTok() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s.trim());
        }
        return st.nextToken();
    }

    static int nxtInt() throws IOException {
        return Integer.parseInt(nxtTok());
    }

    static long nxtLng() throws IOException {
        return Long.parseLong(nxtTok());
    }

    static double nxtDbl() throws IOException {
        return Double.parseDouble(nxtTok());
    }

    static int[] nxtIntArr(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtInt();
        return a;
    }

    static long[] nxtLngArr(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtLng();
        return a;
    }

    static char[] nxtCharArr() throws IOException {
        return nxtTok().toCharArray();
    }

}//end class.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class C {
    static String[] first;
    static String[] second;
    static int[] wanted;
    static int n;
    static int[][] dp;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader( new InputStreamReader(System.in) );
        out = new PrintWriter(System.out);
        st = new StringTokenizer("");
        sb = new StringBuilder();

        n = nxtInt();
        first = new String[n];
        second = new String[n];

        for( int i = 0;i < n;i++){
            String line = br.readLine();
            String[] tokens = line.split(" ");
            first[i] = tokens[0];
            second[i] = tokens[1];
        }
        dp = new int[n][2];
        wanted = new int[n];
        for( int i = 0;i < n;i++){
            int t = nxtInt();
            wanted[i] = t-1;
            dp[i][0] = -1;
            dp[i][1] = -1;
        }//end for.

        ArrayList<String> em = new ArrayList<String>();
//      System.out.println( calc( 0 , em ) );

        TreeSet<String> t = new TreeSet<String>();
        int check = calc(0,t,0);

        if( check == 1 ){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }//end main.

    static boolean check( TreeSet<String> t){

        Iterator it = t.iterator();
        int i = 0;
        while( it.hasNext() ){
            String h = (String) it.next();
            if( first[wanted[i]].equals( h ) || second[wanted[i]].equals( h ) ){

            }else{
                return false;
            }
            i++;
        }//end while.

        return true;

    }//end method.

    static int calc( int index, TreeSet<String> t , int last){

        if( index == n ){
            return check( t ) == true ? 1 : 0;
        }

        if( dp[index][last] != -1){
            return dp[index][last] ;
        }

        boolean ret = false;
        boolean check;
        t.add( first[ wanted[ index ] ] );
        check = check( t );

        if( check ){
            ret = (calc( index + 1, t ,0) == 1 ? true: false);
        }

        t.remove( first[ wanted[ index ] ] );
        t.add(  second[ wanted[index] ] );

        check = check( t );
        if( check ){
            ret = ret | (calc( index + 1, t ,1) == 1 ? true : false);
        }
        t.remove(  second[ wanted[index] ] );
        if( ret == false ){
            dp[index][last] = 0;
        }else{
            dp[index][last] = 1;
        }
        return dp[index][last];
    }//end method.

    static BufferedReader br;
    static StringTokenizer st;
    static StringBuilder sb;
    static PrintWriter out;

    static String nxtTok() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s.trim());
        }
        return st.nextToken();
    }

    static int nxtInt() throws IOException {
        return Integer.parseInt(nxtTok());
    }

    static long nxtLng() throws IOException {
        return Long.parseLong(nxtTok());
    }

    static double nxtDbl() throws IOException {
        return Double.parseDouble(nxtTok());
    }

    static int[] nxtIntArr(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtInt();
        return a;
    }

    static long[] nxtLngArr(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtLng();
        return a;
    }

    static char[] nxtCharArr() throws IOException {
        return nxtTok().toCharArray();
    }

}//end class.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class C {
    static String[] first;
    static String[] second;
    static int[] wanted;
    static int n;
    static int[][] dp;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader( new InputStreamReader(System.in) );
        out = new PrintWriter(System.out);
        st = new StringTokenizer("");
        sb = new StringBuilder();

        n = nxtInt();
        first = new String[n];
        second = new String[n];

        for( int i = 0;i < n;i++){
            String line = br.readLine();
            String[] tokens = line.split(" ");
            first[i] = tokens[0];
            second[i] = tokens[1];
        }
        dp = new int[n][2];
        wanted = new int[n];
        for( int i = 0;i < n;i++){
            int t = nxtInt();
            wanted[i] = t-1;
            dp[i][0] = -1;
            dp[i][1] = -1;
        }//end for.

        int check = Math.max( calc( 0 , 0), calc(0,1));

        if( check == 1 ){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }//end main.

    static int calc( int index, int last){

        if( index == n-1 ){
            return 1;
        }

        if( dp[index][last] != -1 ){
            return dp[index][last];
        }
        int f = -1;
        int s = -1;
        if( last == 0 ){
            int comp = first[wanted[index]].compareTo( first[ wanted[index+1] ] );
            if( comp < 0 ){
                f = calc( index + 1, 0 );
            }//end if.
            comp = first[wanted[index]].compareTo( second[ wanted[index+1] ] );
            if( comp < 0 ){
                s = calc(index+1, 0);
            }

        }else{

            int comp = second[wanted[index]].compareTo( first[ wanted[index+1] ] );
            if( comp < 0 ){
                f = calc( index + 1, 0 );
            }//end if.
            comp = second[wanted[index]].compareTo( second[ wanted[index+1] ] );
            if( comp < 0 ){
                s = calc(index+1, 0);
            }

        }//end else.

        return dp[index][last] = Math.max(f, s);
    }//end method.

    static BufferedReader br;
    static StringTokenizer st;
    static StringBuilder sb;
    static PrintWriter out;

    static String nxtTok() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s.trim());
        }
        return st.nextToken();
    }

    static int nxtInt() throws IOException {
        return Integer.parseInt(nxtTok());
    }

    static long nxtLng() throws IOException {
        return Long.parseLong(nxtTok());
    }

    static double nxtDbl() throws IOException {
        return Double.parseDouble(nxtTok());
    }

    static int[] nxtIntArr(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtInt();
        return a;
    }

    static long[] nxtLngArr(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtLng();
        return a;
    }

    static char[] nxtCharArr() throws IOException {
        return nxtTok().toCharArray();
    }

}//end class.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class C {
    static String[] first;
    static String[] second;
    static int[] wanted;
    static int n;
    static int[][] dp;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader( new InputStreamReader(System.in) );
        out = new PrintWriter(System.out);
        st = new StringTokenizer("");
        sb = new StringBuilder();

        n = nxtInt();
        first = new String[n];
        second = new String[n];

        for( int i = 0;i < n;i++){
            String line = br.readLine();
            String[] tokens = line.split(" ");
            first[i] = tokens[0];
            second[i] = tokens[1];
        }
        dp = new int[n][2];
        wanted = new int[n];
        for( int i = 0;i < n;i++){
            int t = nxtInt();
            wanted[i] = t-1;
            dp[i][0] = -1;
            dp[i][1] = -1;
        }//end for.

        int check = Math.max( calc( 0 , 0), calc(0,1));

        if( check == 1 ){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }//end main.

    static int calc( int index, int last){

        if( index == n-1 ){
            return 1;
        }

        if( dp[index][last] != -1 ){
            return dp[index][last];
        }
        int f = -1;
        int s = -1;
        if( last == 0 ){
            int comp = first[wanted[index]].compareTo( first[ wanted[index+1] ] );
            if( comp < 0 ){
                f = calc( index + 1, 0 );
            }//end if.
            comp = first[wanted[index]].compareTo( second[ wanted[index+1] ] );
            if( comp < 0 ){
                s = calc(index+1, 1);
            }

        }else{

            int comp = second[wanted[index]].compareTo( first[ wanted[index+1] ] );
            if( comp < 0 ){
                f = calc( index + 1, 0 );
            }//end if.
            comp = second[wanted[index]].compareTo( second[ wanted[index+1] ] );
            if( comp < 0 ){
                s = calc(index+1, 1);
            }

        }//end else.

        return dp[index][last] = Math.max(f, s);
    }//end method.

    static BufferedReader br;
    static StringTokenizer st;
    static StringBuilder sb;
    static PrintWriter out;

    static String nxtTok() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s.trim());
        }
        return st.nextToken();
    }

    static int nxtInt() throws IOException {
        return Integer.parseInt(nxtTok());
    }

    static long nxtLng() throws IOException {
        return Long.parseLong(nxtTok());
    }

    static double nxtDbl() throws IOException {
        return Double.parseDouble(nxtTok());
    }

    static int[] nxtIntArr(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtInt();
        return a;
    }

    static long[] nxtLngArr(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtLng();
        return a;
    }

    static char[] nxtCharArr() throws IOException {
        return nxtTok().toCharArray();
    }

}//end class.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class C {
    static String[] first;
    static String[] second;
    static int[] wanted;
    static int n;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader( new InputStreamReader(System.in) );
        out = new PrintWriter(System.out);
        st = new StringTokenizer("");
        sb = new StringBuilder();

        n = nxtInt();
        first = new String[n];
        second = new String[n];

        for( int i = 0;i < n;i++){
            String line = br.readLine();
            String[] tokens = line.split(" ");
            first[i] = tokens[0];
            second[i] = tokens[1];
        }

        wanted = new int[n];
        for( int i = 0;i < n;i++){
            int t = nxtInt();
            wanted[i] = t-1;
        }//end for.

        ArrayList<String> em = new ArrayList<String>();
//      System.out.println( calc( 0 , em ) );

        TreeSet<String> t = new TreeSet<String>();
        boolean check = calc(0,t);

        if( check ){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }//end main.

    static boolean check( Object[] arr){

        for( int i = 0;i < arr.length ;i++){
            if( first[wanted[i]].equals(arr[i]) || second[wanted[i]].equals( arr[i] ) ){

            }else{
                return false;
            }
        }//end for.

        return true;

    }//end method.

    static boolean calc( int index, TreeSet<String> t ){

        if( index == n ){
            return check( t.toArray() );
        }

        boolean ret = false;
        boolean check;
        TreeSet<String> temp = (TreeSet<String>) t.clone();
        temp.add( first[ wanted[ index ] ] );
        check = check(  temp.toArray() );

        if( check ){
            ret = calc( index + 1, temp );
        }

        temp = (TreeSet<String>) t.clone();
        temp.add( second[ wanted[index] ] );
        //(String[])
        check = check(  temp.toArray() );
        if( check ){
            ret = ret | calc( index + 1, temp );
        }

        return ret;
    }//end method.

    static BufferedReader br;
    static StringTokenizer st;
    static StringBuilder sb;
    static PrintWriter out;

    static String nxtTok() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s.trim());
        }
        return st.nextToken();
    }

    static int nxtInt() throws IOException {
        return Integer.parseInt(nxtTok());
    }

    static long nxtLng() throws IOException {
        return Long.parseLong(nxtTok());
    }

    static double nxtDbl() throws IOException {
        return Double.parseDouble(nxtTok());
    }

    static int[] nxtIntArr(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtInt();
        return a;
    }

    static long[] nxtLngArr(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtLng();
        return a;
    }

    static char[] nxtCharArr() throws IOException {
        return nxtTok().toCharArray();
    }

}//end class.


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class C {
    static String[] first;
    static String[] second;
    static int[] wanted;
    static int n;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader( new InputStreamReader(System.in) );
        out = new PrintWriter(System.out);
        st = new StringTokenizer("");
        sb = new StringBuilder();

        n = nxtInt();
        first = new String[n];
        second = new String[n];

        for( int i = 0;i < n;i++){
            String line = br.readLine();
            String[] tokens = line.split(" ");
            first[i] = tokens[0];
            second[i] = tokens[1];
        }

        wanted = new int[n];
        for( int i = 0;i < n;i++){
            int t = nxtInt();
            wanted[i] = t-1;
        }//end for.

        ArrayList<String> em = new ArrayList<String>();
//      System.out.println( calc( 0 , em ) );

        TreeSet<String> t = new TreeSet<String>();
        boolean check = calc(0,t);

        if( check ){
            System.out.println("YES");
        }else{
            System.out.println("NO");
        }

    }//end main.

    static boolean check( TreeSet<String> t){

        Iterator it = t.iterator();
        int i = 0;
        while( it.hasNext() ){
            String h = (String) it.next();
            if( first[wanted[i]].equals( h ) || second[wanted[i]].equals( h ) ){

            }else{
                return false;
            }
            i++;
        }//end while.

        return true;

    }//end method.

    static boolean calc( int index, TreeSet<String> t ){

        if( index == n ){
            return check( t );
        }

        boolean ret = false;
        boolean check;
        TreeSet<String> temp = (TreeSet<String>) t.clone();
        temp.add( first[ wanted[ index ] ] );
        check = check(  temp);

        if( check ){
            ret = calc( index + 1, temp );
        }

        temp = (TreeSet<String>) t.clone();
        temp.add( second[ wanted[index] ] );
        //(String[])
        check = check(  temp );
        if( check ){
            ret = ret | calc( index + 1, temp );
        }

        return ret;
    }//end method.

    static BufferedReader br;
    static StringTokenizer st;
    static StringBuilder sb;
    static PrintWriter out;

    static String nxtTok() throws IOException {
        while (!st.hasMoreTokens()) {
            String s = br.readLine();
            if (s == null)
                return null;
            st = new StringTokenizer(s.trim());
        }
        return st.nextToken();
    }

    static int nxtInt() throws IOException {
        return Integer.parseInt(nxtTok());
    }

    static long nxtLng() throws IOException {
        return Long.parseLong(nxtTok());
    }

    static double nxtDbl() throws IOException {
        return Double.parseDouble(nxtTok());
    }

    static int[] nxtIntArr(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtInt();
        return a;
    }

    static long[] nxtLngArr(int n) throws IOException {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nxtLng();
        return a;
    }

    static char[] nxtCharArr() throws IOException {
        return nxtTok().toCharArray();
    }

}//end class.


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class C {

    public static class node{
        String str1,str2;
        int t;
        node(String s,String s2,int m){
            str1=s;
            str2=s2;
            t=m;
        }

    }

    public static void main(String[] args) {

        Scanner x=new Scanner(System.in);
        int n=x.nextInt();
        node arr[]=new node [n];

        for(int i=0;i<n;i++){
            node nn=new node(x.next(),x.next(),0);
            arr[i]=nn;
        }

        for (int i = 0; i < n; i++)
        {

            arr[x.nextInt()-1].t=i;

        }

        Arrays.sort(arr, new Comparator<node>() {

            @Override
            public int compare(node o1, node o2) {

                if(o1.t>o2.t)return 1;
                if(o1.t<o2.t)return -1;

                return 0;
            }
        });

        String temp="";
        boolean f=false;
        for (int i = 0; i < arr.length; i++) {

            if(i==0){
                if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                else{temp=arr[i].str2;}
            }

            else{

                if(temp.compareTo(arr[i].str1)>=1&&temp.compareTo(arr[i].str2)>=1){System.out.println("NO");f=true;break;}

                else if(temp.compareTo(arr[i].str1)>=1&&temp.compareTo(arr[i].str2)>=1){

                    if(temp.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                    else{temp=arr[i].str2;}

                }

                else if(temp.compareTo(arr[i].str1)<=-1)    {
                    temp=arr[i].str1;
                }

                else if(temp.compareTo(arr[i].str2)<=-1){
                    temp=arr[i].str2;
                }

            }

        }

    if(f==false){
        System.out.println("YES");
    }

    }

}


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class C {

    public static class node{
        String str1,str2;
        int t;
        node(String s,String s2,int m){
            str1=s;
            str2=s2;
            t=m;
        }

    }

    public static void main(String[] args) {

        Scanner x=new Scanner(System.in);
        int n=x.nextInt();
        node arr[]=new node [n];

        for(int i=0;i<n;i++){

            node nn=new node(x.next(),x.next(),0);
            arr[i]=nn;
        }

        for (int i = 0; i < n; i++)
        {

            arr[x.nextInt()-1].t=i;

        }

        Arrays.sort(arr, new Comparator<node>() {

            @Override
            public int compare(node o1, node o2) {

                if(o1.t>o2.t)return 1;
                if(o1.t<o2.t)return -1;

                return 0;
            }
        });

        String temp="";
        boolean f=false;
        for (int i = 0; i < arr.length; i++) {

            if(i==0){
                if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                else{temp=arr[i].str2;}
            }

            else{

                if(arr[i].str1.compareTo(temp)<=-1&&arr[i].str2.compareTo(temp)<=-1){System.out.println("NO");f=true;break;}
                else if(arr[i].str1.compareTo(temp)>=1&&arr[i].str2.compareTo(temp)>=1){

                    if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                    else{temp=arr[i].str2;}

                }
                else if(arr[i].str1.compareTo(temp)<=-1)    {
                    temp=arr[i].str1;
                }

                else if(arr[i].str2.compareTo(temp)<=-1){
                    temp=arr[i].str2;
                }

            }

        }

    if(f==false){
        System.out.println("YES");
    }

    }

}


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class C {

    public static class node{
        String str1,str2;
        int t;
        node(String s,String s2,int m){
            str1=s;
            str2=s2;
            t=m;
        }

    }

    public static void main(String[] args) {

        Scanner x=new Scanner(System.in);
        int n=x.nextInt();
        node arr[]=new node [n];

        for(int i=0;i<n;i++){
            node nn=new node(x.next(),x.next(),0);
            arr[i]=nn;
        }

        for (int i = 0; i < n; i++)
        {

            arr[x.nextInt()-1].t=i;

        }

        Arrays.sort(arr, new Comparator<node>() {

            @Override
            public int compare(node o1, node o2) {

                if(o1.t>o2.t)return 1;
                if(o1.t<o2.t)return -1;

                return 0;
            }
        });

        String temp="";
        boolean f=false;
        for (int i = 0; i < arr.length; i++) {

            if(i==0){
                if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                else{temp=arr[i].str2;}
            }

            else{

                if(arr[i].str1.compareTo(temp)<=-1&&arr[i].str2.compareTo(temp)<=-1){System.out.println("NO");f=true;break;}
                else if(arr[i].str1.compareTo(temp)>=1&&arr[i].str2.compareTo(temp)>=1){

                    if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                    else{temp=arr[i].str2;}

                }
                else {

                if(arr[i].str1.compareTo(temp)<=-1) {
                    temp=arr[i].str1;
                }

                    else {
                        temp=arr[i].str2;
                    }

                }

            }

        }

    if(f==false){
        System.out.println("YES");
    }

    }

}


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class C {

    public static class node{
        String str1,str2;
        int t;
        node(String s,String s2,int m){
            str1=s;
            str2=s2;
            t=m;
        }

    }

    public static void main(String[] args) {

        Scanner x=new Scanner(System.in);
        int n=x.nextInt();
        node arr[]=new node [n];

        for(int i=0;i<n;i++){
            node nn=new node(x.next(),x.next(),0);
            arr[i]=nn;
        }

        for (int i = 0; i < n; i++)
        {

            arr[x.nextInt()-1].t=i;

        }

        Arrays.sort(arr, new Comparator<node>() {

            @Override
            public int compare(node o1, node o2) {

                if(o1.t>o2.t)return 1;
                if(o1.t<o2.t)return -1;

                return 0;
            }
        });

        String temp="";
        boolean f=false;
        for (int i = 0; i < arr.length; i++) {

            if(i==0){
                if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                else{temp=arr[i].str2;}
            }

            else{

                if(arr[i].str1.compareTo(temp)<=-1&&arr[i].str2.compareTo(temp)<=-1){System.out.println("NO");f=true;break;}
                else if(arr[i].str1.compareTo(temp)>=0&&arr[i].str2.compareTo(temp)>=0){

                    if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                    else{temp=arr[i].str2;}

                }
                else if(arr[i].str1.compareTo(temp)<=-1)    {
                    temp=arr[i].str1;
                }

                else if(arr[i].str2.compareTo(temp)<=-1){
                    temp=arr[i].str2;
                }

            }

        }

    if(f==false){
        System.out.println("YES");
    }

    }

}


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class C {

    public static class node{
        String str1,str2;
        int t;
        node(String s,String s2,int m){
            str1=s;
            str2=s2;
            t=m;
        }

    }

    public static void main(String[] args) {

        Scanner x=new Scanner(System.in);
        int n=x.nextInt();
        node arr[]=new node [n];

        for(int i=0;i<n;i++){
            node nn=new node(x.next(),x.next(),0);
            arr[i]=nn;
        }

        for (int i = 0; i < n; i++)
        {

            arr[x.nextInt()-1].t=i;

        }

        Arrays.sort(arr, new Comparator<node>() {

            @Override
            public int compare(node o1, node o2) {

                if(o1.t>o2.t)return 1;
                if(o1.t<o2.t)return -1;

                return 0;
            }
        });

        String temp="";
        boolean f=false;
        for (int i = 0; i < arr.length; i++) {

            if(i==0){
                if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                else{temp=arr[i].str2;}
            }

            else{

                if(temp.compareTo(arr[i].str1)>=1&&temp.compareTo(arr[i].str2)>=1){System.out.println("NO");f=true;break;}

                else if(temp.compareTo(arr[i].str1)<=-1&&temp.compareTo(arr[i].str2)<=-1){

                    if(arr[i].str1.compareTo(arr[i].str2)<=-1){temp=arr[i].str1;}
                    else{temp=arr[i].str2;}

                }

                else if(temp.compareTo(arr[i].str1)<=-1)    {
                    temp=arr[i].str1;
                }

                else if(temp.compareTo(arr[i].str2)<=-1){
                    temp=arr[i].str2;
                }

            }

        }

    if(f==false){
        System.out.println("YES");
    }

    }

}


import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.awt.Point;
import java.math.BigInteger;

import static java.lang.Math.*;

public class CodeforcesC implements Runnable {

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        if (ONLINE_JUDGE) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new CodeforcesC(), "", 128 * (1L << 20)).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    void debug(Object... objects) {
        if (DEBUG) {
            for (Object o : objects) {
                System.err.println(o.toString());
            }
        }
    }

    int[] readIntArray(int n) throws IOException {
        int[] array = new int[n];
        for (int i = 0; i < n; i++)
            array[i] = readInt();
        return array;
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            time();
            memory();
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    boolean DEBUG = false;

    void solve() throws IOException {

        int n = readInt();
        String[] sName = new String[n], fName = new String[n];
        String[] s = new String[n], f = new String[n];

        for (int i=0; i<n; i++) {
            f[i] = readString();
            s[i] = readString();
        }

        int[] p = new int[n];
        for (int i=0; i<n; i++)
            p[i] = readInt() - 1;

        for (int i=0; i<n; i++) {
            fName[i] = f[p[i]];
            sName[i] = s[p[i]];
        }

        String last = fName[0].compareTo(sName[0]) < 0 ? fName[0] : sName[0];
        for (int i=1; i<n; i++) {
            String minimal = fName[i].compareTo(sName[i]) < 0 ? fName[i] : sName[i];
            String maximal = fName[i].compareTo(sName[i]) > 0 ? fName[i] : sName[i];
            if (minimal.compareTo(last) > 0) {
                last = minimal;
                continue;
            }
            if (maximal.compareTo(last) > 0) {
                last = minimal;
                continue;
            }
            out.println("NO");
            return;
        }

        out.println("YES");
    }
}


import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.awt.Point;
import java.math.BigInteger;

import static java.lang.Math.*;

public class CodeforcesC implements Runnable {

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        if (ONLINE_JUDGE) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new CodeforcesC(), "", 128 * (1L << 20)).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    void debug(Object... objects) {
        if (DEBUG) {
            for (Object o : objects) {
                System.err.println(o.toString());
            }
        }
    }

    int[] readIntArray(int n) throws IOException {
        int[] array = new int[n];
        for (int i = 0; i < n; i++)
            array[i] = readInt();
        return array;
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            time();
            memory();
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    boolean DEBUG = false;

    void solve() throws IOException {

        int n = readInt();
        String[] sName = new String[n], fName = new String[n];
        String[] s = new String[n], f = new String[n];

        for (int i=0; i<n; i++) {
            f[i] = readString();
            s[i] = readString();
        }

        int[] p = new int[n];
        for (int i=0; i<n; i++)
            p[i] = readInt() - 1;

        for (int i=0; i<n; i++) {
            fName[i] = f[p[i]];
            sName[i] = s[p[i]];
        }

        String last = fName[0].compareTo(sName[0]) < 0 ? fName[0] : sName[0];
        for (int i=1; i<n; i++) {
            String minimal = fName[i].compareTo(sName[i]) < 0 ? fName[i] : sName[i];
            String maximal = fName[i].compareTo(sName[i]) > 0 ? fName[i] : sName[i];
            if (minimal.compareTo(last) > 0) {
                last = minimal;
                continue;
            }
            if (maximal.compareTo(last) > 0) {
                last = maximal;
                continue;
            }
            out.println("NO");
            return;
        }

        out.println("YES");
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class MakeItNonDeterministic {
	public static class Node implements Comparable<Node>{
		String name;
		int index;
		public Node(String name, int index) {
			this.name = name;
			this.index = index;
		}
		@Override
		public int compareTo(Node o) {
			return this.name.compareTo(o.name);
		}
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return name+":"+index;
		}
	}
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();
		ArrayList<Node> arr = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			String temp=scanner.nextLine();
				arr.add(new Node(temp.split(" ")[0],i+1));
				arr.add(new Node(temp.split(" ")[1],i+1));
		}
		Collections.sort(arr);
		int permutation	[]=new int[n];
		boolean found[]=new boolean[n];
		for (int i = 0; i < permutation.length; i++) {
			permutation[i]=scanner.nextInt();
			found[i]=false;
		}
		int j=0;
		for (int i = 0; i < permutation.length; i++) {
			while(j<arr.size() && arr.get(j).index!=permutation[i]){
				j++;
			}
			if(j<arr.size()){
				found[i]=true;
				if(i>0 && !found[i-1])
				{
					System.out.println("NO");
					return;
				}
			}else{
				System.out.println("NO");
				return;
			}
		}
		System.out.println("YES");
	}
}


import java.util.Scanner;

public final class ID472A {

    static String f[] = new String[100004];
    static String s[] = new String[100004];
    static int p[] = new int[100004];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n, i;
        String f1 = new String();
        String f2 = new String();
        String s1 = new String();
        String s2 = new String();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (i = 1; i <= n; i++) {
            f[i] = in.next();
            s[i] = in.next();
        }
        for (i = 1; i <= n; i++) {
            p[i] = in.nextInt();
        }
        int k = 0;
        int slct = 0;
        int bigone, smallone;
        for (i = 1; i < n; i++) {
            if (slct == 0) {
                if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0
                        || f[p[i]].compareTo(s[p[i + 1]]) <= 0
                        || s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    if (f[p[i]].compareTo(s[p[i]]) <= 0) {
                        f1 = f[p[i]];
                        f2 = s[p[i]];
                    } else {
                        f2 = f[p[i]];
                        f1 = s[p[i]];
                    }
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f2.compareTo(s1) < 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            } else if (slct == 1) {
                if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0 || f[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    f2 = f[p[i]];
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f2.compareTo(s1) < 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            } else if (slct == 2) {
                if (!(s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    f2 = s[p[i]];
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f2.compareTo(s1) < 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            }
        }
        if (k == 0)
            System.out.println("YES");
        in.close();
    }

}


package codeforces;

import java.util.Scanner;

public final class ID472A {

    static String f[] = new String[100004];
    static String s[] = new String[100004];
    static int p[] = new int[100004];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n, i;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (i = 1; i <= n; i++) {
            f[i] = in.next();
            s[i] = in.next();
        }
        for (i = 1; i <= n; i++) {
            p[i] = in.nextInt();
        }
        int k = 0;
        for (i = 1; i < n; i++) {
            if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0
                    || f[p[i]].compareTo(s[p[i + 1]]) <= 0
                    || s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                    .compareTo(s[p[i + 1]]) <= 0)) {
                System.out.println("NO");
                k = 1;
                break;
            }
        }
        if (k == 0)
            System.out.println("YES");
        in.close();
    }

}


import java.util.Scanner;

public final class ID472A {

    static String f[] = new String[100004];
    static String s[] = new String[100004];
    static int p[] = new int[100004];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n, i;
        String f1 = new String();
        String f2 = new String();
        String s1 = new String();
        String s2 = new String();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (i = 1; i <= n; i++) {
            f[i] = in.next();
            s[i] = in.next();
        }
        for (i = 1; i <= n; i++) {
            p[i] = in.nextInt();
        }
        int k = 0;
        int slct = 0;
        int bigone, smallone;
        for (i = 1; i < n; i++) {
            if (slct == 0) {
                if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0
                        || f[p[i]].compareTo(s[p[i + 1]]) <= 0
                        || s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    if (f[p[i]].compareTo(s[p[i]]) <= 0) {
                        f1 = f[p[i]];
                        f2 = s[p[i]];
                    } else {
                        f2 = f[p[i]];
                        f1 = s[p[i]];
                    }
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f2.compareTo(s1) <= 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            } else if (slct == 1) {
                if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0 || f[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    f2 = f[p[i]];
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f2.compareTo(s1) <= 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            } else if (slct == 2) {
                if (!(s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    f2 = s[p[i]];
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f2.compareTo(s1) <= 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            }
        }
        if (k == 0)
            System.out.println("YES");
        in.close();
    }

}


import java.util.Scanner;

public final class ID472A {

    static String f[] = new String[100004];
    static String s[] = new String[100004];
    static int p[] = new int[100004];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n, i;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (i = 1; i <= n; i++) {
            f[i] = in.next();
            s[i] = in.next();
        }
        for (i = 1; i <= n; i++) {
            p[i] = in.nextInt();
        }
        int k = 0;
        for (i = 1; i < n; i++) {
            if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0
                    || f[p[i]].compareTo(s[p[i + 1]]) <= 0
                    || s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                    .compareTo(s[p[i + 1]]) <= 0)) {
                System.out.println("NO");
                k = 1;
                break;
            }
        }
        if (k == 0)
            System.out.println("YES");
        in.close();
    }

}


import java.util.Scanner;

public final class ID472A {

    static String f[] = new String[100004];
    static String s[] = new String[100004];
    static int p[] = new int[100004];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n, i;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (i = 1; i <= n; i++) {
            f[i] = in.next();
            s[i] = in.next();
        }
        for (i = 1; i <= n; i++) {
            p[i] = in.nextInt();
        }
        int k = 0;
        for (i = 1; i < n; i++) {
            if (!(f[p[i]].compareTo(f[p[i + 1]]) < 0
                    || f[p[i]].compareTo(s[p[i + 1]]) < 0
                    || s[p[i]].compareTo(f[p[i + 1]]) < 0 || s[p[i]]
                    .compareTo(s[p[i + 1]]) < 0)) {
                System.out.println("NO");
                k = 1;
                break;
            }
        }
        if (k == 0)
            System.out.println("YES");
        in.close();
    }

}


import java.util.Scanner;

public final class ID472A {

    static String f[] = new String[100004];
    static String s[] = new String[100004];
    static int p[] = new int[100004];

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n, i;
        String f1 = new String();
        String f2 = new String();
        String s1 = new String();
        String s2 = new String();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for (i = 1; i <= n; i++) {
            f[i] = in.next();
            s[i] = in.next();
        }
        for (i = 1; i <= n; i++) {
            p[i] = in.nextInt();
        }
        int k = 0;
        int slct = 0;
        int bigone, smallone;
        for (i = 1; i < n; i++) {
            if (slct == 0) {
                if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0
                        || f[p[i]].compareTo(s[p[i + 1]]) <= 0
                        || s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    if (f[p[i]].compareTo(s[p[i]]) <= 0) {
                        f1 = f[p[i]];
                        f2 = s[p[i]];
                    } else {
                        f2 = f[p[i]];
                        f1 = s[p[i]];
                    }
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f1.compareTo(s1) <= 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            } else if (slct == 1) {
                if (!(f[p[i]].compareTo(f[p[i + 1]]) <= 0 || f[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    f1 = f[p[i]];
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f1.compareTo(s1) <= 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            } else if (slct == 2) {
                if (!(s[p[i]].compareTo(f[p[i + 1]]) <= 0 || s[p[i]]
                        .compareTo(s[p[i + 1]]) <= 0)) {
                    System.out.println("NO");
                    k = 1;
                    break;
                } else {
                    f1 = s[p[i]];
                    if (f[p[i + 1]].compareTo(s[p[i + 1]]) <= 0) {
                        s1 = f[p[i + 1]];
                        s2 = s[p[i + 1]];
                        bigone = 2;
                        smallone = 1;
                    } else {
                        s2 = f[p[i + 1]];
                        s1 = s[p[i + 1]];
                        bigone = 1;
                        smallone = 2;
                    }
                    if (f1.compareTo(s1) <= 0) {
                        slct = smallone;
                    } else
                        slct = bigone;
                }
            }
            // System.out.println(slct);
        }
        if (k == 0)
            System.out.println("YES");
        in.close();
    }

}


import java.io.*;
import java.util.*;

public class SolutionC {

    public void solve() {
        nextLine();
        int n = nextInt();
        HashMap<Integer, String> f = new HashMap<Integer, String>();
        HashMap<Integer, String> s = new HashMap<Integer, String>();
        for (int i = 0; i < n; i++) {
            nextLine();
            StringTokenizer ss= new StringTokenizer(line);
            f.put(i+1, ss.nextToken());
            s.put(i+1, ss.nextToken());
        }
        nextLine();
        int x = nextInt();
        String max = (f.get(x).compareTo(s.get(x))<0)?f.get(x) : s.get(x);
        for (int i = 1; i < n; i++) {
//            System.out.println("asdasdas "+max);
            String mx = "", mn="";
            x = nextInt();
            if(f.get(x).compareTo(s.get(x))<0){
                mx = s.get(x);
                mn = f.get(x);
            }else{
                mn = s.get(x);
                mx = f.get(x);
            }
            if(max.compareTo(mx) > 0){
                out.println("NO");
                out.flush();
                return;
            }else if(max.compareTo(mn) > 0){
                max = mx;
            }else{
                max = mn;
            }
        }
        out.println("YES");
        out.flush();
    }

    public static void main(String args[]) {
        new SolutionC().solve();
    }
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);
    String line;
    StringTokenizer st;

    public void nextLine() {
        try {
            line = bf.readLine();
            st = new StringTokenizer(line);
        } catch (IOException e) {
            System.out.println("1010 0111 1010");
        }
    }

    public int nextInt() {
        return Integer.parseInt(st.nextToken());
    }

    public long nextLong() {
        return Long.parseLong(st.nextToken());
    }

    public double nextDouble() {
        return Double.parseDouble(st.nextToken());
    }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main
{

	private static BufferedReader in;
	private static BufferedWriter out;

	public static void main(String[] args) throws NumberFormatException, IOException
	{
		// streams
		boolean file = false;
		if (file)
			in = new BufferedReader(new FileReader("input.txt"));
		else
			in = new BufferedReader(new InputStreamReader(System.in));
		// out = new BufferedWriter(new OutputStreamWriter(System.out));
		out = new BufferedWriter(new FileWriter("output.txt"));
		StringTokenizer tok;

		// read input
		int n = Integer.parseInt(in.readLine());
		Person persons[] = new Person[n + 1];
		for (int i = 1; i <= n; i++)
		{
			tok = new StringTokenizer(in.readLine());
			persons[i] = new Person(tok.nextToken(), tok.nextToken());
		}

		// read permutation
		int perm[] = new int[n];
		tok = new StringTokenizer(in.readLine());
		for (int i = 0; i < n; i++)
			perm[i] = Integer.parseInt(tok.nextToken());

		// check validity
		for (int i = 0; i < n - 1; i++)
		{
			int idx = perm[i];
			int nextIdx = perm[i + 1];
			if ((!persons[idx].firstInValid && persons[idx].first.compareTo(persons[nextIdx].first) <=0) || (!persons[idx].SecondInvalid && persons[idx].second.compareTo(persons[nextIdx].first) <=0) )
				persons[nextIdx].firstInValid = false;
			else
				persons[nextIdx].firstInValid = true;

			if ((!persons[idx].firstInValid &&persons[idx].first.compareTo(persons[nextIdx].second) <=0) || (!persons[idx].SecondInvalid && persons[idx].second.compareTo(persons[nextIdx].second) <=0) )
				persons[nextIdx].SecondInvalid = false;
			else
				persons[nextIdx].SecondInvalid = true;
		}

		boolean valid = true;
		for (int i = 1; i <= n; i++)
		{
			if (persons[i].firstInValid && persons[i].SecondInvalid)
				valid = false;
		}

		// ouput
		if (valid)
			System.out.println("YES");
		else
			System.out.println("NO");

	}

}

class Person
{
	String first;
	String second;
	boolean firstInValid;
	boolean SecondInvalid;

	public Person(String first, String second)
	{
		this.first = first;
		this.second = second;
	}
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main
{

	private static BufferedReader in;
	private static BufferedWriter out;

	public static void main(String[] args) throws NumberFormatException, IOException
	{
		// streams
		boolean file = false;
		if (file)
			in = new BufferedReader(new FileReader("input.txt"));
		else
			in = new BufferedReader(new InputStreamReader(System.in));
		// out = new BufferedWriter(new OutputStreamWriter(System.out));
		out = new BufferedWriter(new FileWriter("output.txt"));
		StringTokenizer tok;

		// read input
		int n = Integer.parseInt(in.readLine());
		Person persons[] = new Person[n + 1];
		for (int i = 1; i <= n; i++)
		{
			tok = new StringTokenizer(in.readLine());
			persons[i] = new Person(tok.nextToken(), tok.nextToken());
		}

		// read permutation
		int perm[] = new int[n];
		tok = new StringTokenizer(in.readLine());
		for (int i = 0; i < n; i++)
			perm[i] = Integer.parseInt(tok.nextToken());

		// check validity
		for (int i = 0; i < n - 1; i++)
		{
			int idx = perm[i];
			int nextIdx = perm[i + 1];
			if (persons[idx].first.compareTo(persons[nextIdx].first) <=0 || persons[idx].second.compareTo(persons[nextIdx].first) <=0 )
				persons[nextIdx].firstInValid = false;
			else
				persons[nextIdx].firstInValid = true;

			if (persons[idx].first.compareTo(persons[nextIdx].second) <=0 || persons[idx].second.compareTo(persons[nextIdx].second) <=0 )
				persons[nextIdx].SecondInvalid = false;
			else
				persons[nextIdx].SecondInvalid = true;
		}

		boolean valid = true;
		for (int i = 1; i <= n; i++)
		{
			if (persons[i].firstInValid && persons[i].SecondInvalid)
				valid = false;
		}

		// ouput
		if (valid)
			System.out.println("YES");
		else
			System.out.println("NO");

	}

}

class Person
{
	String first;
	String second;
	boolean firstInValid;
	boolean SecondInvalid;

	public Person(String first, String second)
	{
		this.first = first;
		this.second = second;
	}
}


import java.util.Scanner;

public class non_deterministic {
public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String []arr1=new String[n];
    String []arr2=new String[n];
    for(int i=0;i<n;i++)
    {
    arr1[i]=sc.next();
    arr2[i]=sc.next();
    }
    int []arr3=new int[n];
    for(int i=0;i<n;i++)
        arr3[i]=sc.nextInt();
    if(arr2[arr3[0]-1].compareTo(arr1[arr3[0]-1])<0)
    {String temp=arr1[arr3[0]-1];
    arr1[arr3[0]-1]=arr2[arr3[0]-1];
    arr2[arr3[0]-1]=temp;
    }

    for(int i=0;i<n-1;i++){
        if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0)
        { System.out.println("NO");
    return;
        }
      if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])>0)
    {String temp=arr1[arr3[i+1]-1];
    arr1[arr3[i+1]-1]=arr2[arr3[1+1]-1];
    arr2[arr3[i+1]-1]=temp;
    }
    }
    System.out.println("YES");
}
}


import java.util.Scanner;

public class non_deterministic {
public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String []arr1=new String[n];
    String []arr2=new String[n];
    for(int i=0;i<n;i++)
    {
    arr1[i]=sc.next();
    arr2[i]=sc.next();
    }
    int []arr3=new int[n];
    for(int i=0;i<n;i++)
        arr3[i]=sc.nextInt();
    for(int i=0;i<n-1;i++)
        if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr1[arr3[i+1]-1].compareTo(arr2[arr3[i]-1])<0
                &&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr2[arr3[i]-1])<0)
        { System.out.println("No");
    return;
        }
    System.out.println("Yes");
}
}


import java.util.Scanner;

public class non_deterministic {
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        String [] min = new String[N];
        String [] max = new String[N];
        for(int i=0;i<N;i++) {
            String s1 =in.next();
            String s2 =in.next();
            min[i] = s1.compareTo(s2) < 0 ? s1 : s2;
            max[i] = s1.compareTo(s2) < 0 ? s2 : s1;
        }
        String prev = "";
        for(int i=0;i<N;i++) {
            int x=in.nextInt() - 1;
            if(prev.compareTo(min[x]) < 0)
                prev=min[x];
            else if(prev.compareTo(max[x]) < 0)
                prev=max[x];
            else {
                System.out.println("NO");
                return;
            }

        }
        System.out.println("YES");
    }

}


import java.util.Scanner;

public class non_deterministic {
public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String []arr1=new String[n];
    String []arr2=new String[n];
    for(int i=0;i<n;i++)
    {
    arr1[i]=sc.next();
    arr2[i]=sc.next();
    }
    int []arr3=new int[n];
    for(int i=0;i<n;i++)
        arr3[i]=sc.nextInt();
    if(arr2[arr3[0]-1].compareTo(arr1[arr3[0]-1])<0)
    {String temp=arr1[arr3[0]-1];
    arr1[arr3[0]-1]=arr2[arr3[0]-1];
    arr2[arr3[0]-1]=temp;
    }

    for(int i=0;i<n-1;i++){
        if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0)
        { System.out.println("NO");
    return;
        }
      if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])>0)
    {String temp=arr1[arr3[i+1]-1];
    arr1[arr3[i+1]-1]=arr2[arr3[1+1]-1];
    arr2[arr3[i+1]-1]=temp;
    }
            if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])>0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])>0)
                    if(arr2[arr3[i+1]-1].compareTo(arr1[arr3[i+1]-1])<0)
    {String temp=arr1[arr3[i+1]-1];
    arr1[arr3[i+1]-1]=arr2[arr3[i+1]-1];
    arr2[arr3[i+1]-1]=temp;
    }
    }
    System.out.println("YES");
}
}


import java.util.Scanner;

public class non_deterministic {
public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String []arr1=new String[n];
    String []arr2=new String[n];
    for(int i=0;i<n;i++)
    {
    arr1[i]=sc.next();
    arr2[i]=sc.next();
    }
    int []arr3=new int[n];
    for(int i=0;i<n;i++)
        arr3[i]=sc.nextInt();
    if(arr2[arr3[0]-1].compareTo(arr1[arr3[0]-1])<0)
    {String temp=arr1[arr3[0]-1];
    arr1[arr3[0]-1]=arr2[arr3[0]-1];
    arr2[arr3[0]-1]=temp;
    }

    for(int i=0;i<n-1;i++){
        if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0)
        { System.out.println("NO");
    return;
        }
      if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])>0)
    {String temp=arr1[arr3[i+1]-1];
    arr1[arr3[i+1]-1]=arr2[arr3[1+1]-1];
    arr2[arr3[i+1]-1]=temp;
    }
            if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])>0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])>0)
                    if(arr2[arr3[i+1]-1].compareTo(arr1[arr3[i+1]-1])<0)
    {String temp=arr1[arr3[i+1]-1];
    arr1[arr3[i+1]-1]=arr2[arr3[1+1]-1];
    arr2[arr3[i+1]-1]=temp;
    }
    }
    System.out.println("YES");
}
}


import java.util.Scanner;

public class non_deterministic {
public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String []arr1=new String[n];
    String []arr2=new String[n];
    for(int i=0;i<n;i++)
    {
    arr1[i]=sc.next();
    arr2[i]=sc.next();
    }
    int []arr3=new int[n];
    for(int i=0;i<n;i++)
        arr3[i]=sc.nextInt();
    if(arr1[arr3[0]-1].compareTo(arr2[arr3[0]-1])<0)
    {String temp=arr1[arr3[0]-1];
    arr1[arr3[0]-1]=arr2[arr3[0]-1];
    arr2[arr3[0]-1]=temp;
    }

    for(int i=0;i<n-1;i++){
        if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr1[arr3[i+1]-1].compareTo(arr2[arr3[i]-1])<0)
        { System.out.println("NO");
    return;
        }
      if(arr1[arr3[i+1]-1].compareTo(arr2[arr3[i+1]-1])<0)
    {String temp=arr1[arr3[i+1]-1];
    arr1[arr3[i+1]-1]=arr2[arr3[1+1]-1];
    arr2[arr3[i+1]-1]=temp;
    }
    }
    System.out.println("YES");
}
}


import java.util.Scanner;

public class non_deterministic {
public static void main(String[]args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String []arr1=new String[n];
    String []arr2=new String[n];
    for(int i=0;i<n;i++)
    {
    arr1[i]=sc.next();
    arr2[i]=sc.next();
    }
    int []arr3=new int[n];
    for(int i=0;i<n;i++)
        arr3[i]=sc.nextInt();
    if(arr1[arr3[0]-1].compareTo(arr2[arr3[0]-1])<0)
    {String temp=arr1[arr3[0]-1];
    arr1[arr3[0]-1]=arr2[arr3[0]-1];
    arr2[arr3[0]-1]=temp;
    }

    for(int i=0;i<n-1;i++){
        if(arr1[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0&&arr2[arr3[i+1]-1].compareTo(arr1[arr3[i]-1])<0)
        { System.out.println("NO");
    return;
        }
      if(arr1[arr3[i+1]-1].compareTo(arr2[arr3[i+1]-1])<0)
    {String temp=arr1[arr3[i+1]-1];
    arr1[arr3[i+1]-1]=arr2[arr3[1+1]-1];
    arr2[arr3[i+1]-1]=temp;
    }
    }
    System.out.println("YES");
}
}