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]].compareTo(arr2[arr3[0]])<0)
    {String temp=arr1[arr3[0]];
    arr1[arr3[0]]=arr2[arr3[0]];
    arr2[arr3[0]]=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();
    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.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;

public class C270 {
    public static void main(String args[])throws IOException
    {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        String line = obj.readLine();
        int n= Integer.parseInt(line);
        String arr[][]= new String[n+1][2];
        for (int i=1;i<=n;i++)
        {
            line = obj.readLine();
            String str[]= line.split(" ");
            arr[i][0]= str[0];
            arr[i][1] =str[1];
        }

        line  =obj.readLine();
        String str[]= line.split(" ");
        int arr2[]= new int[n];
        for (int i=0;i<n;i++)
        {
            arr2[i] = Integer.parseInt(str[i]);

        }
        int ans=1;
        String prev;
        int num = arr2[0];
        if (arr[num][0].compareTo(arr[num][1])<0)
            prev = arr[num][0];
        else
            prev = arr[num][1];
        for (int i=1;i<n;i++)
        {
            String pres1;
            //System.out.println(prev);
             num =arr2[i];
            if (arr[num][0].compareTo(arr[num][1])<0)
                pres1 = arr[num][0];
            else
                pres1= arr[num][1];
            String pres2;
            if (arr[num][0].equals(pres1))
                pres2 = arr[num][1];
            else
                pres2= arr[num][0];
            //System.out.println(pres1 +" "+pres2);
            if (pres2.compareTo(prev)<0)
            {
                ans =0;
                break;
            }
            else if (pres1.compareTo(prev)>0)
                prev =pres1;
            else
                prev =pres1;

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

    }
}


import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;

public class C270 {
    public static void main(String args[])throws IOException
    {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        String line = obj.readLine();
        int n= Integer.parseInt(line);
        String arr[][]= new String[n+1][2];
        for (int i=1;i<=n;i++)
        {
            line = obj.readLine();
            String str[]= line.split(" ");
            arr[i][0]= str[0];
            arr[i][1] =str[1];
        }

        line  =obj.readLine();
        String str[]= line.split(" ");
        int arr2[]= new int[n];
        for (int i=0;i<n;i++)
        {
            arr2[i] = Integer.parseInt(str[i]);

        }
        //System.out.println("a".compareTo("b"));
        int ans=1;
        String prev;
        int num = arr2[0];
        if (arr[num][0].compareTo(arr[num][1])<0)
            prev = arr[num][0];
        else
            prev = arr[num][1];
        for (int i=1;i<n;i++)
        {
            String pres1;
            //System.out.println(prev);
             num =arr2[i];
            if (arr[num][0].compareTo(arr[num][1])<0)
                pres1 = arr[num][0];
            else
                pres1= arr[num][1];
            String pres2;
            if (arr[num][0].equals(pres1))
                pres2 = arr[num][1];
            else
                pres2= arr[num][0];
            //System.out.println(prev +" "+pres1 +" "+pres2);
            if (pres2.compareTo(prev)<0)
            {
                ans =0;
                break;
            }

            else if (pres1.compareTo(prev)>0)
                prev =pres1;
            else
                prev =pres2;

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

    }
}


import java.util.Scanner;

public class Main11 {

	public static void main(String[] agrs){

		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		String cas[];
		String cad[] = new String[n];
		String cad2[] = new String[n];
		for (int i = 0; i < n; i++) {
			cas = scan.nextLine().split(" ");
			cad[i] = cas[0];
			cad2[i] = cas[1];
		}

		boolean flag = false;
		String aux = "";
		int first = scan.nextInt();
		if(cad[first-1].compareTo(cad2[first-1])<0){
		aux = cad[first-1];
		}else aux = cad2[first-1];
		for (int i = 0; i < n-1; i++) {
			int index = scan.nextInt();
			if(aux.compareTo(cad[index-1])<0 && cad[index-1].compareTo(cad2[index-1])<0){
				aux = cad[index-1];
			}else if(aux.compareTo(cad2[index-1])<0 && cad2[index-1].compareTo(cad[index-1])<0){
				aux = cad2[index-1];
				}else if(aux.compareTo(cad[index-1])<0){
					aux = cad[index-1];
				}else if(aux.compareTo(cad2[index-1])<0){
					aux = cad2[index-1];
				}else flag = true;

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


import java.util.Scanner;

public class Main11 {

	public static void main(String[] agrs){

		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		String cas[];
		String cad[] = new String[n];
		String cad2[] = new String[n];
		for (int i = 0; i < n; i++) {
			cas = scan.nextLine().split(" ");
			cad[i] = cas[0];
			cad2[i] = cas[1];
		}

		boolean flag = false;
		String aux = "";
		int first = scan.nextInt();
		if(cad[first-1].compareTo(cad2[first-1])<0){
		aux = cad[first-1];
		}else aux = cad2[first-1];
		for (int i = 0; i < n-1; i++) {
			int index = scan.nextInt();
			if(aux.compareTo(cad[index-1])<0 && cad[index-1].compareTo(cad2[index-1])<0){
				aux = cad[index-1]; System.out.println(aux);
			}else if(aux.compareTo(cad2[index-1])<0 && cad2[index-1].compareTo(cad[index-1])<0){
				aux = cad2[index-1]; System.out.println(aux);
				}else if(aux.compareTo(cad[index-1])<0){
					aux = cad[index-1];
				}else if(aux.compareTo(cad2[index-1])<0){
					aux = cad2[index-1];
				}else flag = true;

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


import java.util.Scanner;

public class Main11 {

	public static void main(String[] agrs){

		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		String cas[];
		String cad[] = new String[n];
		String cad2[] = new String[n];
		for (int i = 0; i < n; i++) {
			cas = scan.nextLine().split(" ");
			cad[i] = cas[0];
			cad2[i] = cas[1];
		}

		boolean flag = false;
		String aux = "";
		int first = scan.nextInt();
		if(cad[first-1].compareTo(cad2[first-1])<0){
		aux = cad[first-1];
		}else aux = cad2[first-1];
		for (int i = 0; i < n-1; i++) {
			int index = scan.nextInt();
			if(aux.compareTo(cad[index-1])>0){
				if(aux.compareTo(cad2[index-1])>0){
					flag = true;
				}else aux = cad2[index-1];
			}else if(cad[index-1].compareTo(cad2[index-1])<0){
				aux = cad[index-1];
			}else aux = cad2[index-1];
		}
		if(!flag){
			System.out.println("YES");
		}else System.out.println("NO");
	}
}


import java.util.Scanner;

public class Main{

	public static void main(String[] agrs){

		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.nextLine());
		String cas[];
		String cad[] = new String[n];
		String cad2[] = new String[n];
		for (int i = 0; i < n; i++) {
			cas = scan.nextLine().split(" ");
			cad[i] = cas[0];
			cad2[i] = cas[1];
		}

		boolean flag = false;
		String aux = "";
		int first = scan.nextInt();
		if(cad[first-1].compareTo(cad2[first-1])<0){
		aux = cad[first-1];
		}else aux = cad2[first-1];
		for (int i = 0; i < n-1; i++) {
			int index = scan.nextInt();
			if(aux.compareTo(cad[index-1])>0){
				if(aux.compareTo(cad2[index-1])>0){
					flag = true;
				}else aux = cad2[index-1];
			}else aux = cad[index-1];
		}
		if(!flag){
			System.out.println("YES");
		}else System.out.println("NO");
	}
}


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

public class Solution{
public void main (String[] args){
Scanner sc= new Scanner (System.in);
int n= sc.nextInt();
String[] frst_name= new String [n];
String[] lst_name = new String [n];

for(int i=0; i< n; i++){
frst_name[i]=sc.next();
lst_name[i]=sc.next();
}
int [] perm= new int[n];
for(int i=0;i<n;i++){
perm [i]=sc.nextInt();
}
int poss =1;
int i=1;
String[] act_perm= new String [n];
int d=frst_name[perm[0]-1].compareTo(lst_name[perm[0]-1]);
if (d>0){act_perm[0]=lst_name[perm[0]-1];}
else {act_perm[0]=frst_name[perm[0]-1];}
while (poss!=0 && i!=n)
{
int a=act_perm[i-1].compareTo(frst_name[perm[i]-1]);
int b=act_perm[i-1].compareTo(lst_name[perm[i]-1]);
int c=frst_name[perm[i]-1].compareTo(lst_name[perm[i]-1]);
if (a>0 && b>0){poss=0;}
if (a<0 && b>0) {act_perm[i]=frst_name[perm[i]-1];}
if (a>0 && b<0) {act_perm[i]=lst_name[perm[i]-1];}
if (a<0 && b<0 && c>0) {act_perm[i]=lst_name[perm[i]-1];}
if (a<0 && b<0 && c<0) {act_perm[i]=frst_name[perm[i]-1];}
}

if (poss==0){System.out.println("NO");}
else {System.out.println("YES");}

}

}


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

public class Solution{
public void main (String[] args){
Scanner sc= new Scanner (System.in);
int n= sc.nextInt();
String[] frst_name= new String [n];
String[] lst_name = new String [n];

for(int i=0; i< n; i++){
frst_name[i]=sc.next();
lst_name[i]=sc.next();
}
int [] perm= new int[n];
for(int i=0;i<n;i++){
perm [i]=sc.nextInt();
}
int poss =1;
int i=1;
String[] act_perm= new String [n];
int d=frst_name[perm[0]-1].compareTo(lst_name[perm[0]-1]);
if (d>0){act_perm[0]=lst_name[perm[0]-1];}
else {act_perm[0]=frst_name[perm[0]-1];}
while (poss!=0 && i!=n)
{
int a=act_perm[i-1].compareTo(frst_name[perm[i]-1]);
int b=act_perm[i-1].compareTo(lst_name[perm[i]-1]);
int c=frst_name[perm[i]-1].compareTo(lst_name[perm[i]-1]);
if (a>0 && b>0){poss=0;}
if (a<0 && b>0) {act_perm[i]=frst_name[perm[i]-1];}
if (a>0 && b<0) {act_perm[i]=lst_name[perm[i]-1];}
if (a<0 && b<0 && c>0) {act_perm[i]=lst_name[perm[i]-1];}
if (a<0 && b<0 && c<0) {act_perm[i]=frst_name[perm[i]-1];}
i++;
}

if (poss==0){System.out.println("NO");}
else {System.out.println("YES");}

}

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
import java.io.*;
import java.math.*;
/**
 *
 * @author Ayush Gupta
 */
public class Solution {

public static void main (String[] args){
Scanner sc= new Scanner (System.in);
int n= sc.nextInt();
String[] frst_name= new String [n];
String[] lst_name = new String [n];

for(int i=0; i< n; i++){
frst_name[i]=sc.next();
lst_name[i]=sc.next();
}
int [] perm= new int[n];
for(int i=0;i<n;i++){
perm [i]=sc.nextInt();
}
int poss =1;
int i=1;
String[] act_perm= new String [n];
int d=frst_name[perm[0]-1].compareTo(lst_name[perm[0]-1]);
if (d>0){act_perm[0]=lst_name[perm[0]-1];}
else {act_perm[0]=frst_name[perm[0]-1];}
while (poss!=0 && i!=n)
{
int a=act_perm[i-1].compareTo(frst_name[perm[i]-1]);
int b=act_perm[i-1].compareTo(lst_name[perm[i]-1]);
int c=frst_name[perm[i]-1].compareTo(lst_name[perm[i]-1]);
if (a>0 && b>0){poss=0;}
if (a<0 && b>0) {act_perm[i]=frst_name[perm[i]-1];}
if (a>0 && b<0) {act_perm[i]=lst_name[perm[i]-1];}
if (a<0 && b<0 && c>0) {act_perm[i]=lst_name[perm[i]-1];}
if (a<0 && b<0 && c<0) {act_perm[i]=frst_name[perm[i]-1];}
i++;
}

if (poss==0){System.out.println("NO");}
else {System.out.println("YES");}

}

}


import java.util.*;

public class MakeItNondeterministic{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		boolean yes = true;
		int people = in.nextInt();
		String[] firstName = new String[people], lastName = new String[people];
		for (int i = 0; i < people; i++){
			firstName[i] = in.next();
			lastName[i] = in.next();
		}
		String current;
		int nextPerson = in.nextInt() - 1;
		if (firstName[nextPerson].compareTo(lastName[nextPerson]) < 0){
			current = firstName[nextPerson];
		}else
			current = lastName[nextPerson];
		for (int i = 1; i < people; i++){
			nextPerson = in.nextInt() - 1;
			if (firstName[nextPerson].compareTo(lastName[nextPerson]) < 0){
				if (firstName[nextPerson].compareTo(current) > 0){
					current = firstName[nextPerson];
					continue;
				}else if (lastName[nextPerson].compareTo(current) > 0){
					current = lastName[nextPerson];
					continue;
				}else{
					yes = false;
					break;
				}
			}else{
				if (lastName[nextPerson].compareTo(current) > 0){
					current = lastName[nextPerson];
					continue;
				}else if (firstName[nextPerson].compareTo(current) > 0){
					current = firstName[nextPerson];
					continue;
				}else{
					yes = false;
					break;
				}
			}
		}
		System.out.println(yes? "YES" : "NO");
	}
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
static class Reader
{
BufferedReader r;
StringTokenizer str;
Reader()
{
r=new BufferedReader(new InputStreamReader(System.in));
}
Reader(String fileName) throws FileNotFoundException
{
r=new BufferedReader(new FileReader(fileName));
}
public String getNextToken() throws IOException
{
if(str==null||!str.hasMoreTokens())
{
str=new StringTokenizer(r.readLine());
}
return str.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(getNextToken());
}
public long nextLong() throws IOException
{
return Long.parseLong(getNextToken());
}
public Double nextDouble() throws IOException
{
return Double.parseDouble(getNextToken());
}
public String nextString() throws IOException
{
return getNextToken();
}
public int[] intArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] longArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public String[] stringArray(int n) throws IOException
{
String a[]=new String[n];
for(int i=0;i<n;i++)
a[i]=nextString();
return a;
}
public int gcd(int a, int b) {
if(b == 0){
return a;
}
return gcd(b, a%b);
}
public int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}
public int[] getPrimes(int n)
{
    boolean a[]=new boolean[n+1];
    Arrays.fill(a,true);
    for(int i=2;i*i<=n;i++)
    {
    if(a[i])
    {
    for(int x=i;x*i<=n;x++)
    {
    a[x*i]=false;
    }
    }
    }
    int top=0;
    int prime[]=new int[n];
    for(int i=2;i<=n;i++)
    {if(a[i])
    prime[top++]=i;
    }
    return Arrays.copyOf(prime, top);
}
}
public static void main(String args[]) throws IOException, Exception{

    Reader r=new Reader();
PrintWriter pr=new PrintWriter(System.out,false);
int n=r.nextInt();
String a[][]=new String[n][2];

for(int i=0;i<n;i++)
    a[i]=r.stringArray(2);

int per[]=r.intArray(n);

String s[][]=new String[n][2];

for(int i=0;i<n;i++)
{
    s[i][0]=a[per[i]-1][0];
    s[i][1]=a[per[i]-1][1];
    Arrays.sort(s[i]);

}

for(int i=1;i<n;i++)
{
    if(s[i-1][0].compareTo(s[i][0])>0)
    {

        if(s[i-1][0].compareTo(s[i][1])<=0)
        {
            s[i][0]=s[i][1];
        }
        else
        {
        pr.println("NO");
        pr.flush();
        pr.close();
        }

    }

}

pr.println("YES");
pr.flush();
pr.close();

}
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Main {
static class Reader
{
BufferedReader r;
StringTokenizer str;
Reader()
{
r=new BufferedReader(new InputStreamReader(System.in));
}
Reader(String fileName) throws FileNotFoundException
{
r=new BufferedReader(new FileReader(fileName));
}
public String getNextToken() throws IOException
{
if(str==null||!str.hasMoreTokens())
{
str=new StringTokenizer(r.readLine());
}
return str.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(getNextToken());
}
public long nextLong() throws IOException
{
return Long.parseLong(getNextToken());
}
public Double nextDouble() throws IOException
{
return Double.parseDouble(getNextToken());
}
public String nextString() throws IOException
{
return getNextToken();
}
public int[] intArray(int n) throws IOException
{
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=nextInt();
return a;
}
public long[] longArray(int n) throws IOException
{
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=nextLong();
return a;
}
public String[] stringArray(int n) throws IOException
{
String a[]=new String[n];
for(int i=0;i<n;i++)
a[i]=nextString();
return a;
}
public int gcd(int a, int b) {
if(b == 0){
return a;
}
return gcd(b, a%b);
}
public int lcm(int a,int b)
{
    return (a*b)/gcd(a,b);
}
public int[] getPrimes(int n)
{
    boolean a[]=new boolean[n+1];
    Arrays.fill(a,true);
    for(int i=2;i*i<=n;i++)
    {
    if(a[i])
    {
    for(int x=i;x*i<=n;x++)
    {
    a[x*i]=false;
    }
    }
    }
    int top=0;
    int prime[]=new int[n];
    for(int i=2;i<=n;i++)
    {if(a[i])
    prime[top++]=i;
    }
    return Arrays.copyOf(prime, top);
}
}
public static void main(String args[]) throws IOException, Exception{

    Reader r=new Reader();
PrintWriter pr=new PrintWriter(System.out,false);
int n=r.nextInt();
String a[][]=new String[n][2];

for(int i=0;i<n;i++)
    a[i]=r.stringArray(2);

int per[]=r.intArray(n);

String s[][]=new String[n][2];

for(int i=0;i<n;i++)
{
    s[i][0]=a[per[i]-1][0];
    s[i][1]=a[per[i]-1][1];
    Arrays.sort(s[i]);
}

for(int i=1;i<n;i++)
{
    if(s[i-1][0].compareTo(s[i][0])>0)
    {

        pr.println("NO");
        pr.flush();
        pr.close();

    }

}

pr.println("YES");
pr.flush();
pr.close();

}
}


import java.util.Scanner;

public class C472 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        Person[] array = new Person[N];
        for(int i=0;i<N;i++){
            array[i]=new Person(scanner.next(), scanner.next());
        }

        int[] orders = new int[N];
        for(int i=0;i<N;i++){
            orders[i]=scanner.nextInt()-1;
        }
        scanner.close();

        String ans = "YES";
        for(int i=0;i<N-1;i++){
            String currentPerson = array[orders[i]].getAlphabeticallyFirst();
            String followingPersonLast =  array[orders[i+1]].getAlphabeticallyLast();
            if (currentPerson.compareTo(followingPersonLast) >0){
                ans = "NO";
                break;
            }
        }
        System.out.println(ans);
    }

    public static class Person{
        String name;
        String lastName;

        public Person(String name, String lastName) {
            super();
            this.name = name;
            this.lastName = lastName;
        }

        public String getAlphabeticallyFirst(){
            String retval=null;
            if (name.compareTo(lastName)<0) {
                retval = name;
            }else{
                retval = lastName;
            }
            return retval;
        }

        public String getAlphabeticallyLast(){
            String retval=null;
            if (name.compareTo(lastName)>0) {
                retval = name;
            }else{
                retval = lastName;
            }
            return retval;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

    }

}


import java.util.Scanner;

public class C472 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        Person[] array = new Person[N];
        for(int i=0;i<N;i++){
            array[i]=new Person(scanner.next(), scanner.next());
        }

        int[] orders = new int[N];
        for(int i=0;i<N;i++){
            orders[i]=scanner.nextInt()-1;
        }
        scanner.close();

        String ans = "YES";
        int i=0;
        String currentPerson = array[orders[i]].getAlphabeticallyFirst();
        while(i<N-1){
            i++;
            String followingPersonfirst =  array[orders[i]].getAlphabeticallyFirst();
            String followingPersonLast =  array[orders[i]].getAlphabeticallyLast();
            if (currentPerson.compareTo(followingPersonLast) >0){
                ans = "NO";
                break;
            }
            currentPerson = currentPerson.compareTo(followingPersonfirst)<0? followingPersonfirst:followingPersonLast;
        }
        System.out.println(ans);
    }

    public static class Person{
        String name;
        String lastName;

        public Person(String name, String lastName) {
            super();
            this.name = name;
            this.lastName = lastName;
        }

        public String getAlphabeticallyFirst(){
            String retval=null;
            if (name.compareTo(lastName)<0) {
                retval = name;
            }else{
                retval = lastName;
            }
            return retval;
        }

        public String getAlphabeticallyLast(){
            String retval=null;
            if (name.compareTo(lastName)>0) {
                retval = name;
            }else{
                retval = lastName;
            }
            return retval;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

    }

}


import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main
{
	public void foo()
	{
		MyScanner scan = new MyScanner();
		int n = scan.nextInt();
		String[] first = new String[n];
		String[] last = new String[n];
		for(int i = 0;i < n;++i)
		{
			String s1 = scan.next();
			String s2 = scan.next();
			if(s1.compareTo(s2) < 0)
			{
				first[i] = s1;
				last[i] = s2;
			}
			else
			{
				first[i] = s2;
				last[i] = s1;
			}
		}
		Handle[] h = new Handle[n];
		for(int i = 0;i < n;++i)
		{
			h[i] = new Handle(i, scan.nextInt() - 1);
		}
		Arrays.sort(h);
		boolean isYes = true;
		String pre = "";
		for(int i = 0;i < n;++i)
		{
			int j = h[i].index;
			if(first[j].compareTo(pre) > 0)
			{
				pre = first[j];
			}
			else if(last[j].compareTo(pre) > 0)
			{
				pre = last[j];
			}
			else
			{
				isYes = false;
				break;
			}
		}
		System.out.println(isYes ? "YES" : "NO");
	}

	public static void main(String[] args)
	{
		new Main().foo();
	}

	class Handle implements Comparable<Handle>
	{
		private int rank;
		private int index;
		public Handle(int aRank, int aIndex)
		{
			rank = aRank;
			index = aIndex;
		}

		public int compareTo(Handle h)
		{
			return rank - h.rank;
		}
	}

	class MyScanner
	{
	    private byte[] buf = new byte[1024];
	    private int curChar;
	    private int numChars;
	    BufferedInputStream bis = new BufferedInputStream(System.in);

	    public int read()
	    {
	        if (-1 == numChars)
	        {
	        	throw new InputMismatchException();
	        }
	        if (curChar >= numChars)
	        {
	            curChar = 0;
	            try
	            {
	                numChars = bis.read(buf);
	            }
	            catch (IOException e)
	            {
	                throw new InputMismatchException();
	            }
	            if (numChars <= 0)
	            {
	            	return -1;
	            }
	        }
	        return buf[curChar++];
	    }

	    public int nextInt()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        int sgn = 1;
	        if (c == '-')
	        {
	            sgn = -1;
	            c = read();
	        }
	        int res = 0;
	        do
	        {
	            if (c < '0' || c > '9')
	            {
	            	throw new InputMismatchException();
	            }
	            res *= 10;
	            res += c - '0';
	            c = read();
	        } while (!isSpaceChar(c));
	        return res * sgn;
	    }

	    public String next()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        StringBuilder res = new StringBuilder();
	        do
	        {
	            res.appendCodePoint(c);
	            c = read();
	        } while (!isSpaceChar(c));
	        return res.toString();
	    }

	    private boolean isSpaceChar(int c)
	    {
	        return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
	    }
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int dr[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dc[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
    //freopen ("in.txt", "r", stdin);
    //freopen ("out.txt", "w", stdout);

    int n;

    while (cin >> n) {
        vector<pair<string, string> > arr;
        arr.assign(n, pair<string, string>());

        for (int i = 0; i < n; i++) {
            cin >> arr[i].first >> arr[i].second;
        }

        bool bisa = true;

        vector<string> ans;

        for (int i = 0; i < n; i++) {
            int nn;
            scanf("%d", &nn);

            --nn;

            if (ans.size() == 0) {
                string temp = arr[nn].first < arr[nn].second ? arr[nn].first : arr[nn].second;
                ans.push_back(temp);
            }
            else {
                string akhir = ans[(int) ans.size() - 1];

                vector<string> temp;
                temp.push_back(arr[nn].first);
                temp.push_back(arr[nn].second);

                sort(temp.begin(), temp.end());

                if (akhir < temp[0]) ans.push_back(temp[0]);
                else if (akhir < temp[1]) ans.push_back(temp[1]);
                else {
                    bisa = false;
                }
            }
        }

        if (bisa) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int dr[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dc[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
    //freopen ("in.txt", "r", stdin);
    //freopen ("out.txt", "w", stdout);

    int n;

    while (cin >> n) {
        vector<pair<string, string> > arr;
        arr.assign(n, pair<string, string>());

        for (int i = 0; i < n; i++) {
            cin >> arr[i].first >> arr[i].second;
        }

        bool bisa = true;

        vector<string> ans;

        for (int i = 0; i < n; i++) {
            int nn;
            scanf("%d", &nn);

            --nn;

            if (ans.size() == 0) {
                string temp = arr[nn].first <= arr[nn].second ? arr[nn].first : arr[nn].second;
                ans.push_back(temp);
            }
            else {
                string akhir = ans[(int) ans.size() - 1];

                if (akhir <= arr[nn].first) ans.push_back(arr[nn].first);
                else if (akhir <= arr[nn].second) ans.push_back(arr[nn].second);
                else {
                    bisa = false;
                }
            }
        }

        if (bisa) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int dr[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dc[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
    //freopen ("in.txt", "r", stdin);
    //freopen ("out.txt", "w", stdout);

    int n;

    while (cin >> n) {
        vector<pair<string, string> > arr;
        arr.assign(n, pair<string, string>());

        for (int i = 0; i < n; i++) {
            cin >> arr[i].first >> arr[i].second;
        }

        bool bisa = true;

        vector<string> ans;

        for (int i = 0; i < n; i++) {
            int nn;
            scanf("%d", &nn);

            --nn;

            if (ans.size() == 0) {
                string temp = arr[nn].first < arr[nn].second ? arr[nn].first : arr[nn].second;
                ans.push_back(temp);
            }
            else {
                string akhir = ans[(int) ans.size() - 1];

                if (akhir < arr[nn].first) ans.push_back(arr[nn].first);
                else if (akhir < arr[nn].second) ans.push_back(arr[nn].second);
                else {
                    bisa = false;
                }
            }
        }

        if (bisa) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}


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

int main() {
 int a;
cin>>a;
pair<string ,string>p[a];
string s1,s2;
vector<string>h;
for(int i=0;i<a;i++)
{
    cin>>s1>>s2;p[i]=make_pair(s1,s2);
    h.push_back(s1);h.push_back(s2);
}
sort(h.begin(),h.end());
map<string,int>m;
for(int i=0;i<2*a;i++)
{
    m[h[i]]=i;
}
m["."]=INT_MAX;
int d=1;
int arr[a];
for(int i=0;i<a;i++){cin>>arr[i];arr[i]--;}
int k=0;
for(int i=1;i<a;i++)
{k=0;
    int q,w,e,r;
    q=m[p[arr[i]].first];w=m[p[arr[i]].second];
    e=m[p[arr[i-1]].first];r=m[p[arr[i-1]].second];
    if(q>e||q>r)k=1;else p[arr[i]].first=".";
    if(w>e||w>r)k=1;else p[arr[i]].second=".";
    if(!k){d=0;break;}
}
if(d)cout<<"YES";else cout<<"NO";
cout<<endl;
	return 0;
}


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

int main() {
 int a;
cin>>a;
pair<string ,string>p[a];
string s1,s2;
vector<string>h;
for(int i=0;i<a;i++)
{
    cin>>s1>>s2;p[i]=make_pair(s1,s2);
    h.push_back(s1);h.push_back(s2);
}
sort(h.begin(),h.end());
unordered_map<string,int>m;
for(int i=0;i<2*a;i++)
{
    m[h[i]]=i;
}
m["."]=INT_MAX;
int d=1;
int arr[a];
for(int i=0;i<a;i++){cin>>arr[i];arr[i]--;}
int k=0;
for(int i=1;i<a;i++)
{k=0;
    int q,w,e,r;
    q=m[p[arr[i]].first];w=m[p[arr[i]].second];
    e=m[p[arr[i-1]].first];r=m[p[arr[i-1]].second];
    if(q>e||q>r)k=1;else p[arr[i]].first=".";
    if(w>e||w>r)k=1;else p[arr[i]].second=".";
    if(!k){d=0;break;}
}
if(d)cout<<"YES";else cout<<"NO";
cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
string z="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
int main() {
int a;cin>>a;
pair<string,string>p[a];
for(int i=0;i<a;i++)
{
    string s1,s2;cin>>s1>>s2;p[i]=make_pair(s1,s2);
}
int arr[a];
for(int i=0;i<a;i++){cin>>arr[i];arr[i]--;}
int i;
for( i=1;i<a;i++)
{
    int h=0;
    string s1,s2,s3,s4;
    s1=p[arr[i-1]].first;s2=p[arr[i-1]].second;
     s3=p[arr[i]].first;s4=p[arr[i]].second;
    if(s1.compare(s3)>0&&s2.compare(s3)>0){p[arr[i]].first=z;}else h=1;
     if(s1.compare(s4)>0&&s2.compare(s4)>0){p[arr[i]].second=z;}else h=1;
     if(h==0)break;
}
if(i==a)cout<<"YES";else cout<<"NO";
cout<<endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define ll long long
#define EPS 1e-7
using namespace std;
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	/*freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);*/
	ll n; string s, t;
	cin >> n;
	vector<pair<string,string>>v(n),v1(n);
	for (int i = 0; i < n; ++i){
		cin >> s >> t;
		v1[i] = make_pair(s,t);
	}
	for (int i = 0; i < n; ++i){
		int x;
		cin >> x;
		v[i] = v1[x - 1];
	}
	for (int i = 0; i < n-1; ++i){
		if (v[i].first>v[i + 1].first&&v[i].second>v[i + 1].first&&v[i].first>v[i + 1].second&&v[i].second>v[i + 1].second){
			cout << "NO\n"; return 0;
		}
	}
	cout << "YES\n";
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define ll long long
#define EPS 1e-7
using namespace std;
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	/*freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);*/
	ll n; string s, t;
	cin >> n;
	vector<pair<string,string>>v(n),v1(n);

	for (int i = 0; i < n; ++i){
		cin >> s >> t;
		v1[i] = make_pair(s,t);
	}
	for (int i = 0; i < n; ++i){
		int x;
		cin >> x;
		v[i] = v1[x - 1];
	}
	s = min(v[0].first,v[0].second);
	for (int i = 1; i < n; ++i){
		if (v[i].first>s&&v[i].second<s)
			s = v[i].first;
		else if (v[i].first<s&&v[i].second>s)
			s = v[i].first;
		else if (v[i].first>s&&v[i].second > s)
			s = min(v[i].first, v[i].second);
		else {
			cout << "NO\n";
			return 0;
		}

	}
	cout << "YES\n";
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define ll long long
#define EPS 1e-7
using namespace std;
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	/*freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);*/
	ll n; string s, t;
	cin >> n;
	vector<pair<string,string>>v(n),v1(n);

	for (int i = 0; i < n; ++i){
		cin >> s >> t;
		v1[i] = make_pair(s,t);
	}
	for (int i = 0; i < n; ++i){
		int x;
		cin >> x;
		v[i] = v1[x - 1];
	}
	s = min(v[0].first,v[0].second);
	for (int i = 1; i < n; ++i){
		if (v[i].first>s&&v[i].second<s)
			s = v[i].first;
		else if (v[i].first<s&&v[i].second>s)
			s = v[i].second;
		else if (v[i].first>s&&v[i].second > s)
			s = min(v[i].first, v[i].second);
		else {
			cout << "NO\n";
			return 0;
		}

	}
	cout << "YES\n";
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+111;
string a[N],b[N],tmp="";
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0);
    int n,i,t;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]>b[i])
            swap(a[i],b[i]);
        //cout<<a[i]<<" "<<b[i]<<"+++"<<endl;
    }
    for(i=1;i<=n;i++)
    {
        cin>>t;
        if(a[t]>tmp)    tmp=a[t];
        else if(b[t]>tmp)   tmp=b[t];
        else    break;
    }
    if(i<=n)
        puts("NO");
    else
        puts("YES");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+111;
string a[N],b[N],tmp="";
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0);
    int n,i,t;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]>b[i])
            swap(a[i],b[i]);
        //cout<<a[i]<<" "<<b[i]<<"+++"<<endl;
    }
    for(i=1;i<=n;i++)
    {
        scanf("%d",&t);
        if(a[t]>tmp)    tmp=a[t];
        else if(b[t]>tmp)   tmp=b[t];
        else    break;
    }
    if(i<=n)
        puts("NO");
    else
        puts("YES");
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e5+111;
string a[N],b[N];
int w[N];
int main()
{
    int n,i,t;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]>b[i])
            swap(a[i],b[i]);
        //cout<<a[i]<<" "<<b[i]<<"+++"<<endl;
    }
    for(i=1;i<=n;i++)
    {
        scanf("%d",&t);
        w[i]=t;
    }
    for(i=2;i<=n;i++)
    {
        if(b[w[i]]<a[w[i-1]])
        {
            //cout<<i<<"+++"<<endl;
            //cout<<b[w[i]]<<" "<<a[w[i-1]]<<endl;
            break;
        }
    }
    if(i<=n)
        puts("NO");
    else
        puts("YES");
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e5+111;
string a[N],b[N],tmp="";
int main()
{
    cin.sync_with_stdio(0);
    cin.tie(0);
    int n,i,t;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]>b[i])
            swap(a[i],b[i]);
        //cout<<a[i]<<" "<<b[i]<<"+++"<<endl;
    }
    for(i=1;i<=n;i++)
    {
        scanf("%d",&t);
        if(a[t]>tmp)    tmp=a[t];
        else if(b[t]>tmp)   tmp=b[t];
        else    break;
    }
    if(i<=n)
        puts("NO");
    else
        puts("YES");
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N=1e5+111;
string a[N],b[N],tmp="";
int main()
{
    int n,i,t;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]>b[i])
            swap(a[i],b[i]);
        //cout<<a[i]<<" "<<b[i]<<"+++"<<endl;
    }
    for(i=1;i<=n;i++)
    {
        scanf("%d",&t);
        if(a[t]>tmp)    tmp=a[t];
        else if(b[t]>tmp)   tmp=b[t];
        else    break;
    }
    if(i<=n)
        puts("NO");
    else
        puts("YES");
    return 0;
}


#include<bits/stdc++.h>
#define setzero(a) memset(a,0,sizeof(a))
#define setdp(a) memset(a,-1,sizeof(a))
#define sf(a) scanf("%d", &a);
#define sf2(a,b) scanf("%d%d", &a ,&b );
#define sf3(a,b,c) scanf("%d%d%d", &a ,&b,&c );
#define sfll2(a,b) scanf("%I64d%I64d", &a ,&b );
#define sfll3(a,b,c) scanf("%I64d%I64d%I64d", &a ,&b,&c );
#define sfll(a) scanf("%I64d", &a);
#define print(a) printf("%d\n", a);
#define printll(a) printf("%I64d\n", a);
#define printll2(a,b) printf("%I64d %I64d\n", a,b);
#define pr(a) printf("%.9f\n", a);

typedef long long ll;

using namespace std;

int main()
{
    int n;
    sf(n);
    pair<string,string> arr[n];
    for(int i=0; i<n; i++)
        cin>>arr[i].first>>arr[i].second;
    int brr[n];
    for(int i=0; i<n; i++)
        sf(brr[i]);
    string cur="";
    for(int i=0; i<n; i++)
    {
        if(cur<=arr[brr[i]-1].first || cur<=arr[brr[i]-1].second)
            if(cur<=arr[brr[i]-1].first && arr[brr[i]-1].first<=arr[brr[i]-1].second)
                cur=arr[brr[i]-1].first;
            else if(cur<=arr[brr[i]-1].second && arr[brr[i]-1].second<=arr[brr[i]-1].first)
                cur=arr[brr[i]-1].second;
            else if(cur<=arr[brr[i]-1].second)
                cur=arr[brr[i]-1].second;
            else if(cur<=arr[brr[i]-1].first)
                cur=arr[brr[i]-1].first;
            else
            {
                cout<<"NO";
                return 0;
            }
        else
        {
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES"<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define setzero(a) memset(a,0,sizeof(a))
#define setdp(a) memset(a,-1,sizeof(a))
#define sf(a) scanf("%d", &a);
#define sf2(a,b) scanf("%d%d", &a ,&b );
#define sf3(a,b,c) scanf("%d%d%d", &a ,&b,&c );
#define sfll2(a,b) scanf("%I64d%I64d", &a ,&b );
#define sfll3(a,b,c) scanf("%I64d%I64d%I64d", &a ,&b,&c );
#define sfll(a) scanf("%I64d", &a);
#define print(a) printf("%d\n", a);
#define printll(a) printf("%I64d\n", a);
#define printll2(a,b) printf("%I64d %I64d\n", a,b);
#define pr(a) printf("%.9f\n", a);

typedef long long ll;

using namespace std;

int main()
{
    int n;
    sf(n);
    pair<string,string> arr[n];
    for(int i=0; i<n; i++)
        cin>>arr[i].first>>arr[i].second;
    int brr[n];
    for(int i=0; i<n; i++)
        sf(brr[i]);
    string cur="";
    for(int i=0; i<n; i++)
    {
        if(cur<=arr[brr[i]-1].first || cur<=arr[brr[i]-1].second)
            if(cur<=arr[brr[i]-1].first && arr[brr[i]-1].first<=arr[brr[i]-1].second)
                cur=arr[brr[i]-1].first;
            else if(cur<=arr[brr[i]-1].second)
                cur=arr[brr[i]-1].second;
            else
            {
                cout<<"NO";
                return 0;
            }
        else
        {
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES"<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define setzero(a) memset(a,0,sizeof(a))
#define setdp(a) memset(a,-1,sizeof(a))
#define sf(a) scanf("%d", &a);
#define sf2(a,b) scanf("%d%d", &a ,&b );
#define sf3(a,b,c) scanf("%d%d%d", &a ,&b,&c );
#define sfll2(a,b) scanf("%I64d%I64d", &a ,&b );
#define sfll3(a,b,c) scanf("%I64d%I64d%I64d", &a ,&b,&c );
#define sfll(a) scanf("%I64d", &a);
#define print(a) printf("%d\n", a);
#define printll(a) printf("%I64d\n", a);
#define printll2(a,b) printf("%I64d %I64d\n", a,b);
#define pr(a) printf("%.9f\n", a);

typedef long long ll;

using namespace std;

int main()
{
    int n;
    sf(n);
    pair<string,string> arr[n];
    for(int i=0;i<n;i++)
        cin>>arr[i].first>>arr[i].second;
    int brr[n];
    for(int i=0;i<n;i++)
        sf(brr[i]);
    string cur="";
    for(int i=0;i<n;i++)
    {
        if(cur<=arr[brr[i]-1].first || cur<=arr[brr[i]-1].second)
            cur=min(arr[brr[i]-1].first,arr[brr[i]-1].second);
        else
        {
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES"<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define setzero(a) memset(a,0,sizeof(a))
#define setdp(a) memset(a,-1,sizeof(a))
#define sf(a) scanf("%d", &a);
#define sf2(a,b) scanf("%d%d", &a ,&b );
#define sf3(a,b,c) scanf("%d%d%d", &a ,&b,&c );
#define sfll2(a,b) scanf("%I64d%I64d", &a ,&b );
#define sfll3(a,b,c) scanf("%I64d%I64d%I64d", &a ,&b,&c );
#define sfll(a) scanf("%I64d", &a);
#define print(a) printf("%d\n", a);
#define printll(a) printf("%I64d\n", a);
#define printll2(a,b) printf("%I64d %I64d\n", a,b);
#define pr(a) printf("%.9f\n", a);

typedef long long ll;

using namespace std;

int main()
{
    int n;
    sf(n);
    pair<string,string> arr[n];
    for(int i=0; i<n; i++)
        cin>>arr[i].first>>arr[i].second;
    int brr[n];
    for(int i=0; i<n; i++)
        sf(brr[i]);
    string cur="";
    for(int i=0; i<n; i++)
    {
        if(cur<=arr[brr[i]-1].first || cur<=arr[brr[i]-1].second)
            if(cur<=arr[brr[i]-1].first && arr[brr[i]-1].first<=arr[brr[i]-1].second)
                cur=arr[brr[i]-1].first;
            else
                cur=arr[brr[i]-1].second;
        else
        {
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES"<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits\stdc++.h>
using namespace std;
#define all(v)     (v).begin(),(v).end()
#define sz(v)      (ll) (v).size()
#define ll  long long
#define clr(a,val) memset(a,val,sizeof(a))
#define d(x) cout << "<< " << #x << ':' << x << endl;
#define pf  printf
#define sc scanf
#define mp make_pair
#define C (ll)
#define ii pair<ll,ll>
template<typename T> using V = vector<T>;
inline void fastInOut() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main() {
    fastInOut();
    map<int ,pair<string,string> >arr;
    string a, b,mn;
    int n,x,p;
    cin >> n;
    for (int i = 0; i < n;++i) {
        cin >> a >> b;
        arr[i + 1] = { a,b };
    }
    cin >> p;
    for (int i = 1; i < n; i++) {
        cin >> x;
        mn = min(arr[p].first, arr[p].second);
        if (mn >= arr[x].second && mn >= arr[x].first) {

            cout << "NO";
            return 0;
        }
        p = x;
    }
    cout << "YES";
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits\stdc++.h>
using namespace std;
#define all(v)     (v).begin(),(v).end()
#define sz(v)      (ll) (v).size()
#define ll  long long
#define clr(a,val) memset(a,val,sizeof(a))
#define d(x) cout << "<< " << #x << ':' << x << endl;
#define pf  printf
#define sc scanf
#define mp make_pair
#define C (ll)
#define ii pair<ll,ll>
template<typename T> using V = vector<T>;
inline void fastInOut() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main() {
    fastInOut();
    map<int ,pair<string,string> >arr;
    string a, b,mn;
    int n,x,p;
    cin >> n;
    for (int i = 0; i < n;++i) {
        cin >> a >> b;
        arr[i + 1] = { a,b };
    }
    cin >> p;
    for (int i = 1; i < n; i++) {
        cin >> x;
        mn = min(arr[p].first, arr[p].second);
        if (mn > arr[x].second && mn > arr[x].first) {
            cout << "NO";
            return 0;
        }
        p = x;
    }
    cout << "YES";
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits\stdc++.h>
using namespace std;
#define all(v)     (v).begin(),(v).end()
#define sz(v)      (ll) (v).size()
#define ll  long long
#define clr(a,val) memset(a,val,sizeof(a))
#define d(x) cout << "<< " << #x << ':' << x << endl;
#define pf  printf
#define sc scanf
#define mp make_pair
#define C (ll)
#define ii pair<ll,ll>
template<typename T> using V = vector<T>;
inline void fastInOut() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main() {
    fastInOut();
    map<int ,pair<string,string> >arr;
    string a, b,mn;
    int n,x,p;
    cin >> n;
    for (int i = 0; i < n;++i) {
        cin >> a >> b;
        arr[i + 1] = { a,b };
    }
    cin >> p;
    for (int i = 1; i < n-1; i++) {
        cin >> x;
        mn = min(arr[p].first, arr[p].second);
        if (mn >= arr[x].second && mn >= arr[x].first) {
            cout << "NO";
            return 0;
        }
        p = x;
    }
    cout << "YES";
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits\stdc++.h>
using namespace std;
#define all(v)     (v).begin(),(v).end()
#define sz(v)      (ll) (v).size()
#define ll  long long
#define clr(a,val) memset(a,val,sizeof(a))
#define d(x) cout << "<< " << #x << ':' << x << endl;
#define pf  printf
#define sc scanf
#define mp make_pair
#define C (ll)
#define ii pair<ll,ll>
template<typename T> using V = vector<T>;
inline void fastInOut() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main() {
    fastInOut();
    map<int, pair<string, string> >arr;
    string a, b, mn;
    int n, x; pair<int, string> p;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a >> b;
        arr[i + 1] = { a,b };
    }
    cin >> p.first;
    p.second = min(arr[p.first].first, arr[p.first].second);
    for (int i = 1; i < n; i++) {
        cin >> x;
        if (p.second >= arr[x].second && p.second >= arr[x].first) {
            cout << "NO";
            return 0;
        }
        p.first = x;
        if (p.second < arr[x].second&&p.second < arr[x].first)
            p.second = min(arr[x].second, arr[x].first);
        else if (p.second < arr[x].first)
            p.second = arr[x].first;
        else
            p.second = arr[x].second;

    }
    cout << "YES";
    return 0;
}


#include<iostream>
#include<math.h>
//#include<stdio.h>
#include<vector>
#include<map>
//#include<stdlib.h>
#include<algorithm>
#include<set>
#include<fstream>
//#include<iomanip>
//#include<string>
#include<cstring>
using namespace std;
char first[100010][60], last[100010][60];
int main() {
    int n, temp;
    char ocurr[60];
    scanf("%d", &n);
    for(int i = 0;i < n;i ++)
        scanf("%s%s", first[i], last[i]);
    scanf("%d", &temp);
    temp --;
    if(strcmp(first[temp], last[temp]) < 0)
        strcpy(ocurr, first[temp]);
    else
        strcpy(ocurr, last[temp]);
    for(int i = 0;i < n - 1;i ++)
    {
        scanf("%d", &temp);
        temp --;
        if(strcmp(ocurr, first[temp]) > 0 && strcmp(ocurr, last[temp]) > 0)
        {
            printf("NO\n");
            return 0;
        }
        else
        {
            if(strcmp(first[temp], last[temp]) < 0 && strcmp(ocurr, first[temp]) < 0)
                strcpy(ocurr, first[temp]);
            else
                strcpy(ocurr, last[temp]);
        }

    }
    printf("YES\n");
    return 0;
}


#include<iostream>
#include<math.h>
//#include<stdio.h>
#include<vector>
#include<map>
//#include<stdlib.h>
#include<algorithm>
#include<set>
#include<fstream>
//#include<iomanip>
//#include<string>
#include<cstring>
using namespace std;

int main() {
    int n, j = 0;
    cin >> n;
    vector <string> name;
    map <string, int> mp;
    for(int i = 0;i < n;i ++)
    {
        string fir, last;
        cin >> fir >> last;
        mp[fir] = i + 1, mp[last] = i + 1;
        name.push_back(fir), name.push_back(last);
    }
    sort(name.begin(), name.end());
    for(int i = 0;i < n;i ++)
    {
        int temp;
        cin >> temp;
        while(mp[name[j]] != temp)
        {
            j ++;
            if(j == name.size())
            {
                cout << "NO\n";
                return 0;
            }
        }
    }
    cout << "YES\n";
    return 0;
}


#include<iostream>
#include<math.h>
//#include<stdio.h>
#include<vector>
#include<map>
//#include<stdlib.h>
#include<algorithm>
#include<set>
#include<fstream>
//#include<iomanip>
//#include<string>
#include<cstring>
using namespace std;
char first[100010][60], last[100010][60];
int main() {
    int n, temp;
    char ocurr[60];
    scanf("%d", &n);
    for(int i = 0;i < n;i ++)
        scanf("%s%s", first[i], last[i]);
    scanf("%d", &temp);
    temp --;
    if(strcmp(first[temp], last[temp]) < 0)
        strcpy(ocurr, first[temp]);
    else
        strcpy(ocurr, last[temp]);
    for(int i = 0;i < n - 1;i ++)
    {
        scanf("%d", &temp);
        temp --;
        if(strcmp(ocurr, first[temp]) > 0 && strcmp(ocurr, last[temp]) > 0)
        {
            printf("NO\n");
            return 0;
        }
        else
        {
            if(strcmp(first[temp], last[temp]) < 0)
                strcpy(ocurr, first[temp]);
            else
                strcpy(ocurr, last[temp]);
        }

    }
    printf("YES\n");
    return 0;
}


#include<iostream>
#include<math.h>
//#include<stdio.h>
#include<vector>
#include<map>
//#include<stdlib.h>
#include<algorithm>
#include<set>
#include<fstream>
//#include<iomanip>
//#include<string>
#include<cstring>
using namespace std;
char first[100010][60], last[100010][60];
int main() {
    int n, temp;
    char ocurr[60];
    scanf("%d", &n);
    for(int i = 0;i < n;i ++)
        scanf("%s%s", first[i], last[i]);
    scanf("%d", &temp);
    temp --;
    if(strcmp(first[temp], last[temp]) < 0)
        strcpy(ocurr, first[temp]);
    else
        strcpy(ocurr, last[temp]);
    for(int i = 0;i < n - 1;i ++)
    {
        scanf("%d", &temp);
        temp --;
        if(strcmp(ocurr, first[temp]) > 0 && strcmp(ocurr, last[temp]) > 0)
        {
            printf("NO\n");
            return 0;
        }
        else
        {
            if((strcmp(first[temp], last[temp]) < 0 && strcmp(ocurr, first[temp]) < 0) || (strcmp(ocurr, last[temp]) > 0))
                strcpy(ocurr, first[temp]);
            else
                strcpy(ocurr, last[temp]);
        }
    }
    printf("YES\n");
    return 0;
}


#include<iostream>
#include<math.h>
//#include<stdio.h>
#include<vector>
#include<map>
//#include<stdlib.h>
#include<algorithm>
#include<set>
#include<fstream>
//#include<iomanip>
//#include<string>
#include<cstring>
using namespace std;

int main() {
    int n;
    cin >> n;
    set <string> name;
    map <string, int> mp;
    for(int i = 0;i < n;i ++)
    {
        string fir, last;
        cin >> fir >> last;
        mp[fir] = i + 1, mp[last] = i + 1;
        name.insert(fir), name.insert(last);
    }
    set<string> :: iterator j = name.begin();
    for(int i = 0;i < n;i ++)
    {
        int temp;
        cin >> temp;
        while(mp[*j] != temp)
        {
            j ++;
            if(j == name.end())
            {
                cout << "NO\n";
                return 0;
            }
        }
    }
    cout << "YES\n";
    return 0;
}


#include<iostream>
#include<math.h>
//#include<stdio.h>
#include<vector>
#include<map>
//#include<stdlib.h>
#include<algorithm>
#include<set>
#include<fstream>
//#include<iomanip>
//#include<string>
#include<cstring>
using namespace std;
char first[100010][60], last[100010][60];
int main() {
    int n, temp;
    char ocurr[60];
    scanf("%d", &n);
    for(int i = 0;i < n;i ++)
        scanf("%s%s", first[i], last[i]);
    scanf("%d", &temp);
    temp --;
    if(strcmp(first[temp], last[temp]) < 0)
        strcpy(ocurr, first[temp]);
    else
        strcpy(ocurr, last[temp]);
    for(int i = 0;i < n - 1;i ++)
    {
        scanf("%d", &temp);
        temp --;
        if(strcmp(ocurr, first[temp]) > 0 && strcmp(ocurr, last[temp]) > 0)
        {
            printf("NO\n");
            return 0;
        }
        else
        {
            if(strcmp(first[temp], last[temp]) < 0 && strcmp(ocurr, first[temp]) > 0)
                strcpy(ocurr, first[temp]);
            else
                strcpy(ocurr, last[temp]);
        }

    }
    printf("YES\n");
    return 0;
}


#include <bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define prl(c) cout<<(c)<<endl
#define all(fu) (fu).begin(), (fu).end()
#define fr(i, l, n) for(size_t (i)=(l); (i)<(n); (i)++)
#define mod 1000000007
#define pi 3.141592653
#define br cout<<endl
#define prv(v) fr(z,0,(v).size()) cout<<(v)[z]<<' '; br
#define iosync ios::sync_with_stdio(0)

using namespace std;

int main(){
    int n;
    cin>>n;
    vector<string> f(n),s(n),f1(n),s1(n);
    fr(i,0,n){
        cin>>f1[i]>>s1[i];
    }
    fr(i,0,n){
        int q;
        cin>>q;
        f[i]=f1[q-1];
        s[i]=s1[q-1];
    }
    //prv(s);prv(f);
    fr(i,1,n){
        bool b=f[i]>s[i],ba=s[i-1]<f[i-1];
        if((!b?s[i]:f[i])<(ba?s[i-1]:f[i-1])){
                //prl(i);
            cout<<"NO";
            return 0;
        }
        if((b?s[i]:f[i])>(ba?s[i-1]:f[i-1])){
            if(!b) s[i]='z'+1;
            else f[i]='z'+1;
        }
        else {
            if(b) s[i]='z'+1;
            else f[i]='z'+1;
        }
    }
        //prv(s);prv(f);

    cout<<"YES";
    return 0;
}


#include <bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define prl(c) cout<<(c)<<endl
#define all(fu) (fu).begin(), (fu).end()
#define fr(i, l, n) for(size_t (i)=(l); (i)<(n); (i)++)
#define mod 1000000007
#define pi 3.141592653
#define br cout<<endl
#define prv(v) fr(z,0,(v).size()) cout<<(v)[z]<<' '; br
#define iosync ios::sync_with_stdio(0)

using namespace std;

int main(){
    int n;
    cin>>n;
    vector<string> f(n),s(n),f1(n),s1(n);
    fr(i,0,n){
        cin>>f1[i]>>s1[i];
    }
    fr(i,0,n){
        int q;
        cin>>q;
        f[i]=f1[q-1];
        s[i]=s1[q-1];
    }
    //prv(s);prv(f);
    fr(i,1,n){
        if(max(s[i],f[i])<min(s[i-1],f[i-1])){
                //prl(i);
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES";
    return 0;
}


#include <bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define prl(c) cout<<(c)<<endl
#define all(fu) (fu).begin(), (fu).end()
#define fr(i, l, n) for(size_t (i)=(l); (i)<(n); (i)++)
#define mod 1000000007
#define pi 3.141592653
#define br cout<<endl
#define prv(v) fr(z,0,(v).size()) cout<<(v)[z]<<' '; br
#define iosync ios::sync_with_stdio(0)

using namespace std;

int main(){
    int n;
    cin>>n;
    vector<string> f(n),s(n),f1(n),s1(n);
    fr(i,0,n){
        cin>>f1[i]>>s1[i];
    }
    fr(i,0,n){
        int q;
        cin>>q;
        f[i]=f1[q-1];
        s[i]=s1[q-1];
    }
    //prv(s);prv(f);
    fr(i,1,n){
        if(max(s[i],f[i])<min(s[i-1],f[i-1])){
                //prl(i);
            cout<<"NO";
            return 0;
        }
        if(min(s[i],f[i])>min(s[i-1],f[i-1])){
            if(s[i]>f[i]) s[i]='z'+1;
            else f[i]='z'+1;
        }
        else {
            if(s[i]<f[i]) s[i]='z'+1;
            else f[i]='z'+1;
        }
    }
        //prv(s);prv(f);

    cout<<"YES";
    return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

string t1[100005];
string t2[100005];

int main(){
	int p;
	int n;
	string mi, ma;
	cin >> n;
	for (int i = 1; i <= n; ++i){
		cin >> t1[i] >> t2[i];
	}
	cin >> p;
	string s = min(t1[p], t2[p]);
	for (int i = 2; i <= n; ++i){
		cin >> p;
		mi = min(t1[p], t2[p]);
		ma = max(t1[p], t2[p]);
		if (s < mi)
			s = mi;
		else if (s < ma){
			s = ma;
		}
		else{
			cout << "NO";
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

string t1[100005];
string t2[100005];

int main(){
	int p;
	int n;
	string mi, ma;
	cin >> n;
	for (int i = 0; i < n; ++i){
		cin >> t1[i] >> t2[i];
	}
	cin >> p;
	string s = min(t1[p], t2[p]);
	for (int i = 1; i < n; ++i){
		cin >> p;
		mi = min(t1[p], t2[p]);
		ma = max(t1[p], t2[p]);
		if (s < mi)
			s = mi;
		else if (s < ma){
			s = ma;
		}
		cout << "NO";
		return 0;
	}
	cout << "YES" << endl;
		return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

string t1[100005];
string t2[100005];

int main(){
	int p;
	int n;
	string mi, ma;
	cin >> n;
	for (int i = 1; i <= n; ++i){
		cin >> t1[i] >> t2[i];
	}
	cin >> p;
	string s = min(t1[p], t2[p]);
	for (int i = 2; i <= n; ++i){
		cin >> p;
		mi = min(t1[p], t2[p]);
		ma = max(t1[p], t2[p]);
		if (s < mi)
			s = mi;
		else if (s < ma){
			s = ma;
		}
		cout << "NO";
		return 0;
	}
	cout << "YES" << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int mod = 1e5;
pair<char,int> sk[100005];
int main(){
	string a,b;
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
	    sk[i] = make_pair(max(a[0],b[0]),(i+1));
	}
	sort(sk,sk+n);
	for(int i=0;i<n;i++){
		cin>>x;
		if(sk[i].second != x) {
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
int mod = 1e5;
pair<pair<string,string>,int> sk[100005];
int main(){
	string a,b;
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
	    sk[i] = make_pair(make_pair(a,b),(i+1));
	}
//	sort(sk,sk+n);
    string st = "a";
	for(int i=0;i<n;i++){
		//cout<<st<<endl;
		cin>>x;
		string mn = min(sk[x-1].f.f,sk[x-1].f.s);
		string mx = max(sk[x-1].f.f,sk[x-1].f.s);
		if( mn.compare(st) >= 0)       st =  mn;
		else if(mx.compare(st) >= 0)   st =  mx;
		else {
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int mod = 1e5;
pair<char,int> sk[100005];
int main(){
	string a,b;
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
	    sk[i] = make_pair(min(a[0],b[0]),(i+1));
	}
	sort(sk,sk+n);
	for(int i=0;i<n;i++){
		cin>>x;
		if(sk[i].second != x) {
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
int mod = 1e5;
pair<pair<char,char>,int> sk[100005];
int main(){
	string a,b;
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
	    sk[i] = make_pair(make_pair(a[0],b[0]),(i+1));
	}
	sort(sk,sk+n);
	char st = 0;
	for(int i=0;i<n;i++){
		cin>>x;
		int mn = min(sk[i].f.f,sk[i].f.s);
		int mx = max(sk[i].f.f,sk[i].f.s);
		if( mn > st)      st = sk[i].s;
		else if(mx > st)  st = sk[i].s;
		else {
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
int mod = 1e5;
pair<pair<char,char>,int> sk[100005];
int main(){
	string a,b;
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
	    sk[i] = make_pair(make_pair(a[0],b[0]),(i+1));
	}
//	sort(sk,sk+n);
	char st = char(96);
	for(int i=0;i<n;i++){
		//cout<<st<<endl;
		cin>>x;
		int mn = min(sk[x-1].f.f,sk[x-1].f.s);
		int mx = max(sk[x-1].f.f,sk[x-1].f.s);
		if( mn > st)      st =  mn;
		else if(mx > st)  st =  mx;
		else {
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
int mod = 1e5;
pair<pair<char,char>,int> sk[100005];
int main(){
	string a,b;
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
	    sk[i] = make_pair(make_pair(a[0],b[0]),(i+1));
	}
//	sort(sk,sk+n);
	char st = char(96);
	for(int i=0;i<n;i++){
		//cout<<st<<endl;
		cin>>x;
		int mn = min(sk[x-1].f.f,sk[x-1].f.s);
		int mx = max(sk[x-1].f.f,sk[x-1].f.s);
		if( mn >= st)    {
		  st =  mn;
		  continue;
	   }
		else if(mx >= st)  st =  mx;
		else {
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
int mod = 1e5;
pair<pair<char,char>,int> sk[100005];
int main(){
	string a,b;
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
	    sk[i] = make_pair(make_pair(a[0],b[0]),(i+1));
	}
//	sort(sk,sk+n);
	char st = char(96);
	for(int i=0;i<n;i++){
		//cout<<st<<endl;
		cin>>x;
		int mn = min(sk[x-1].f.f,sk[x-1].f.s);
		int mx = max(sk[x-1].f.f,sk[x-1].f.s);
		if( mn >= st)      st =  mn;
		else if(mx >= st)  st =  mx;
		else {
			cout<<"NO";
			return 0;
		}
	}
	cout<<"YES";
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

long long  _pow(long long base, long long exp)	{ return exp == 0 ? 1 : base * _pow(base, exp - 1);}
string int_to_str(int x)	{ string xs; stringstream ss; ss<<x; xs=ss.str(); return xs; }
int str_vec_find (string x,vector<string> y) {vector<string>::iterator it; it=find(y.begin(),y.end(),x); int pos = distance(y.begin(), it); return pos; }
int main()
{
//freopen("input.txt","r",stdin);

string temp1,temp2,test;
vector<string> a,b;
int n,index,pos;

cin>>n;
for(int i=0; i<n; i++)
{
	cin>>temp1>>temp2;
	a.push_back(temp1);
	b.push_back(temp2);
}

cin>>index;
index--;
test=min(a[index],b[index]);
for(int i=0; i<n; i++)
{
	cin>>index;
	index--;
	temp1=min(a[index],b[index]);
	temp2=max(a[index],b[index]);
	if(temp1<test && temp2<test)
	{
		cout<<"NO"<<endl;
		return 0;
	}
	else
	{
		if(temp1>test)
			test=temp1;
		else
			test=temp2;
	}
}
cout<<"YES"<<endl;

//system("pause");
  return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

long long  _pow(long long base, long long exp)	{ return exp == 0 ? 1 : base * _pow(base, exp - 1);}
string int_to_str(int x)	{ string xs; stringstream ss; ss<<x; xs=ss.str(); return xs; }
int str_vec_find (string x,vector<string> y) {vector<string>::iterator it; it=find(y.begin(),y.end(),x); int pos = distance(y.begin(), it); return pos; }
int main()
{
//freopen("input.txt","r",stdin);

string temp1,temp2,test;
vector<string> a,b;
int n,index,pos;

cin>>n;
for(int i=0; i<n; i++)
{
	cin>>temp1>>temp2;
	a.push_back(temp1);
	b.push_back(temp2);
}

cin>>index;
index--;
test=min(a[index],b[index]);
for(int i=1; i<n; i++)
{
	cin>>index;
	index--;
	temp1=min(a[index],b[index]);
	temp2=max(a[index],b[index]);
	if(temp1<test && temp2<test)
	{
		cout<<"NO"<<endl;
		return 0;
	}
	else
	{
		if(temp1>test)
			test=temp1;
		else
			test=temp2;
	}
}
cout<<"YES"<<endl;

//system("pause");
  return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

long long  _pow(long long base, long long exp)	{ return exp == 0 ? 1 : base * _pow(base, exp - 1);}
string int_to_str(int x)	{ string xs; stringstream ss; ss<<x; xs=ss.str(); return xs; }
int str_vec_find (string x,vector<string> y) {vector<string>::iterator it; it=find(y.begin(),y.end(),x); int pos = distance(y.begin(), it); return pos; }
int main()
{
//freopen("input.txt","r",stdin);
long long n;
string temp1,temp2,q;
vector<string> a,b;
int index,pos,counter;

cin>>n;
for(int i=0; i<n; i++)
{
	cin>>temp1>>temp2;
	a.push_back(temp1);
	b.push_back(temp2);
}

for(int i=0; i<n; i++)
{
	cin>>index;
	pos=i+1;
	counter=n-pos;
	temp1=a[index-1];
	temp2=b[index-1];
	while(counter>0)
	{
		for(int i=0; i<n; i++)
		{
			if(i!=(index-1))
			{
				if(a[i]>temp1 || a[i]>temp2 || b[i]>temp1 || b[i]>temp2)
					counter--;
			}
		}
		if(counter>0)
			{cout<<"NO"<<endl; return 0;}
	}
}
cout<<"YES"<<endl;

//system("pause");
  return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

long long  _pow(long long base, long long exp)	{ return exp == 0 ? 1 : base * _pow(base, exp - 1);}
string int_to_str(int x)	{ string xs; stringstream ss; ss<<x; xs=ss.str(); return xs; }
int str_vec_find (string x,vector<string> y) {vector<string>::iterator it; it=find(y.begin(),y.end(),x); int pos = distance(y.begin(), it); return pos; }
int main()
{
//freopen("input.txt","r",stdin);

string temp1,temp2,q;
vector<string> a,b;
int n,index,pos,counter_up,counter_down;

cin>>n;
for(int i=0; i<n; i++)
{
	cin>>temp1>>temp2;
	a.push_back(temp1);
	b.push_back(temp2);
}

for(int i=0; i<n; i++)
{
	cin>>index;
	pos=i+1;
	counter_up=n-pos;
	counter_down=i;
	temp1=a[index-1];
	temp2=b[index-1];
	while(counter_up>0 || counter_down>0)
	{
		for(int i=0; i<n; i++)
		{
			if(i!=(index-1))
			{
				if(a[i]>temp1 || b[i]>temp1)
					counter_up--;
				if(a[i]<temp1 || b[i]<temp1)
					counter_down--;
			}
		}
		if(counter_up>0 || counter_down>0)
		{
			counter_up=n-pos;
			counter_down=i;
			for(int i=0; i<n; i++)
			{
				if(i!=(index-1))
				{
					if(a[i]>temp2 || b[i]>temp2)
						counter_up--;
					if(a[i]<temp2 || b[i]<temp2)
						counter_down--;
				}
			}
			if(counter_up>0 || counter_down>0)
			{cout<<"NO"<<endl; return 0;}
		}

	}
}
cout<<"YES"<<endl;

//system("pause");
  return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>

using namespace std;

long long  _pow(long long base, long long exp)	{ return exp == 0 ? 1 : base * _pow(base, exp - 1);}
string int_to_str(int x)	{ string xs; stringstream ss; ss<<x; xs=ss.str(); return xs; }
int str_vec_find (string x,vector<string> y) {vector<string>::iterator it; it=find(y.begin(),y.end(),x); int pos = distance(y.begin(), it); return pos; }
int main()
{
//freopen("input.txt","r",stdin);
long long n;
string temp1,temp2,q;
vector<string> a,b;
int index,pos,counter_up,counter_down;

cin>>n;
for(int i=0; i<n; i++)
{
	cin>>temp1>>temp2;
	a.push_back(temp1);
	b.push_back(temp2);
}

for(int i=0; i<n; i++)
{
	cin>>index;
	pos=i+1;
	counter_up=n-pos;
	counter_down=i;
	temp1=a[index-1];
	temp2=b[index-1];
	while(counter_up>0 || counter_down>0)
	{
		for(int i=0; i<n; i++)
		{
			if(i!=(index-1))
			{
				if(a[i]>temp1 || b[i]>temp1)
					counter_up--;
				if(a[i]<temp1 || b[i]<temp1)
					counter_down--;
			}
		}
		if(counter_up>0 || counter_down>0)
		{
			counter_up=n-pos;
			counter_down=i;
			for(int i=0; i<n; i++)
			{
				if(i!=(index-1))
				{
					if(a[i]>temp2 || b[i]>temp2)
						counter_up--;
					if(a[i]<temp2 || b[i]<temp2)
						counter_down--;
				}
			}
			if(counter_up>0 || counter_down>0)
			{cout<<"NO"<<endl; return 0;}
		}

	}
}
cout<<"YES"<<endl;

//system("pause");
  return 0;
}


#include <iostream>
#include <string>
using namespace std;

string f[100005], s[100005], t = "";

int main() {
	int n, p;
	cin >> n;
	for (int i = 1; i <= n; i++) { cin >> f[i] >> s[i]; if (f[i] > s[i]) swap(f[i], s[i]); }

	for (int i = 1; i <= n; i++) {
		cin >> p;

		if (f[p] > t)
			t = f[p];
		else if (s[p] > t)
			t = s[p];
		else {
			cout << "NO" << endl;
			return 0;
		}
	}

	cout << "YES" << endl;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

string f[100005], s[100005], t = "";

int main() {
	int n, p;
	cin >> n;
	for (int i = 1; i <= n; ++i) { cin >> f[i] >> s[i]; if (f[i] > s[i]) swap(f[i], s[i]); }

	for (int i = 1; i <= n; ++i) {
		cin >> p;

		if (f[p] > t)
			t = f[p];
		else if (s[p] > t)
			t = s[p];
		else {
			cout << "NO\n";
			return 0;
		}
	}

	cout << "YES\n";
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

string f[100005], s[100005];

int main() {
	int n, p;
	string t = "";
	cin >> n;
	for (int i = 1; i <= n; i++) { cin >> f[i] >> s[i]; if (f[i].compare(s[i]) > 0) swap(f[i], s[i]);}

	for (int i = 1; i <= n; i++) {
		cin >> p;

		if (f[p] > t)
			t = f[p];
		else if (s[p] > t)
			t = s[p];
		else {
			cout << "NO" << endl;
			return 0;
		}
	}

	cout << "YES" << endl;
	return 0;
}


/*
	Dear to me is sleep: still more, being made of stone,
	While pain and guilt still linger here below,
	Blindness and numbness--these please me alone;
	Then do not wake me, keep your voices low.
*/
#include	<iostream>
#include	<cstdio>
#include	<vector>
#include 	<set>
#include	<map>
#include	<queue>
#include	<stack>
#include	<algorithm>
#include	<cstring>
#include	<cfloat>
#include	<cmath>
#include	<cassert>
#include	<locale>
#include	<string>
#include	<bitset>
//#include	<ratio>
#include	<functional>
#include	<climits>
using namespace std;
#define READ(x)     freopen(x,"r",stdin)
#define WRITE(x)    freopen(x,"w",stdout)
#define all(x)      x.begin(),x.end()
#define cl(a,b)	    memset(a,b,sizeof(a))
#define rall(x)     x.rbegin(),x.rend()
#define ll          long long
#define vec         vector
#define vi          vec<int>
#define heap        priority_queue
#define res         reserve
#define pb          push_back
#define f(x,y,z)    for(int x=(y); x<(z); x++)
#define fd(x,y,z)   for(int x=(y); x>=(z); x--)
#define fit(x,y)    for(auto x: y)
#define srt(x)      sort(all(x))
#define rsrt(x)     sort(rall(x))
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define pii         pair<int,int>
#define ppi         pair<pii,int>
#define pip         pair<int,pii>
#define mp          make_pair
#define f1          first
#define s2          second
#define cdbg(x)     cout<<#x<<" = "<<x<<",\t";
#define cdbl        cout<<"\n----------\n";
#define pow2(x)     ((x)*(x))
#define edist(x1, y1, x2, y2) (sqrt((pow2(x1-x2)+pow2(y1-y2))))
#define mdist(x1, y1, x2, y2) (abs((x1)-(x2))+abs((y1)-(y2)))
#define y1          FullSensei
#define mid         ((ss+se)/2)
#define left        (si*2)
#define right       (si*2+1)
#define pi          3.141592653589793
#define popcount    __builtin_popcount
bool checkbit(int x, int y){return (x&(1<<y));}
int setbit(int x, int y){return (x^(1<<y));}
const int dirs[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int mod=(1e9+7);
const int INF=(0x7fffffff);
const int maxn=1e5+7;
int n;
struct person{
    int id;
    string first;
    string second;
    friend bool operator<(person ali, person veli){
        return (ali.id < veli.id);
    }
};
person st[maxn];
map<pair<int,string>,bool>dp;
set<pair<int,string>>vis;
bool check(string s1, string s2){
    //returns (s1 isn't lexicographically smaller than s2) statement is true of false
    int i=0;
    while(i<s1.size() && i<s2.size()){
        if(s1[i]>s2[i]) return true;
        if(s1[i]<s2[i]) return false;
        i++;
    }
    if(i==s2.size())    return true;
    return false;
}
bool fun(int i, string minst){
    //cout<<i<<" "<<minst<<"\n";
    if(i>n)
        return true;
    if(vis.find({i,minst})!=vis.end())
        return dp[{i,minst}];
    vis.insert({i,minst});
    bool fl1=check(st[i].f1, minst);
    bool fl2=check(st[i].s2, minst);
    if(!fl1 && !fl2)
        return dp[{i,minst}]=false;
    if(!fl1)
        return dp[{i,minst}]=fun(i+1, st[i].s2);
    if(!fl2)
        return dp[{i,minst}]=fun(i+1, st[i].f1);
    return dp[{i,minst}]=(fun(i+1, st[i].f1) || fun(i+1, st[i].s2));
}
int main(){
	cin>>n;
	f(i,1,n+1)  cin>>st[i].f1>>st[i].s2;
	f(i,1,n+1)  scanf("%d",&st[i].id);
	sort(st+1, st+n+1);
	if(fun(1,""))
        cout<<"YES\n";
    else
        cout<<"NO\n";
	return 0;
}


/*
	Dear to me is sleep: still more, being made of stone,
	While pain and guilt still linger here below,
	Blindness and numbness--these please me alone;
	Then do not wake me, keep your voices low.
*/
#include	<iostream>
#include	<cstdio>
#include	<vector>
#include 	<set>
#include	<map>
#include	<queue>
#include	<stack>
#include	<algorithm>
#include	<cstring>
#include	<cfloat>
#include	<cmath>
#include	<cassert>
#include	<locale>
#include	<string>
#include	<bitset>
//#include	<ratio>
#include	<functional>
#include	<climits>
using namespace std;
#define READ(x)     freopen(x,"r",stdin)
#define WRITE(x)    freopen(x,"w",stdout)
#define all(x)      x.begin(),x.end()
#define cl(a,b)	    memset(a,b,sizeof(a))
#define rall(x)     x.rbegin(),x.rend()
#define ll          long long
#define vec         vector
#define vi          vec<int>
#define heap        priority_queue
#define res         reserve
#define pb          push_back
#define f(x,y,z)    for(int x=(y); x<(z); x++)
#define fd(x,y,z)   for(int x=(y); x>=(z); x--)
#define fit(x,y)    for(auto x: y)
#define srt(x)      sort(all(x))
#define rsrt(x)     sort(rall(x))
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define pii         pair<int,int>
#define ppi         pair<pii,int>
#define pip         pair<int,pii>
#define mp          make_pair
#define f1          first
#define s2          second
#define cdbg(x)     cout<<#x<<" = "<<x<<",\t";
#define cdbl        cout<<"\n----------\n";
#define pow2(x)     ((x)*(x))
#define edist(x1, y1, x2, y2) (sqrt((pow2(x1-x2)+pow2(y1-y2))))
#define mdist(x1, y1, x2, y2) (abs((x1)-(x2))+abs((y1)-(y2)))
#define y1          FullSensei
#define mid         ((ss+se)/2)
#define left        (si*2)
#define right       (si*2+1)
#define pi          3.141592653589793
#define popcount    __builtin_popcount
bool checkbit(int x, int y){return (x&(1<<y));}
int setbit(int x, int y){return (x^(1<<y));}
const int dirs[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int mod=(1e9+7);
const int INF=(0x7fffffff);
const int maxn=1e5+7;
int n;
struct person{
    int id;
    string first;
    string second;
    friend bool operator<(person ali, person veli){
        return (ali.id < veli.id);
    }
};
person st[maxn];
map<pair<int,string>,bool>dp;
set<pair<int,string>>vis;
bool check(string s1, string s2){
    if(s1.size()>s2.size()) return true;
    if(s2.size()>s1.size()) return false;
    int i=0;
    while(i<s1.size()){
        if(s1[i]!=s2[i])    break;
        i++;
    }
    if(i==s1.size())    return true;
    if(s1[i]>=s2[i])    return true;
    return false;
}
bool fun(int i, string minst){
    //cout<<i<<" "<<minst<<"\n";
    if(i>n)
        return true;
    if(vis.find({i,minst})!=vis.end())
        return dp[{i,minst}];
    vis.insert({i,minst});
    bool fl1=check(st[i].f1, minst);
    bool fl2=check(st[i].s2, minst);
    if(!fl1 && !fl2)
        return dp[{i,minst}]=false;
    if(!fl1)
        return dp[{i,minst}]=fun(i+1, st[i].s2);
    if(!fl2)
        return dp[{i,minst}]=fun(i+1, st[i].f1);
    return dp[{i,minst}]=(fun(i+1, st[i].f1) || fun(i+1, st[i].s2));
}
int main(){
	cin>>n;
	f(i,1,n+1)  cin>>st[i].f1>>st[i].s2;
	f(i,1,n+1)  scanf("%d",&st[i].id);
	sort(st+1, st+n+1);
	if(fun(1,""))
        cout<<"YES\n";
    else
        cout<<"NO\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<lld> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i,a,b) for (int i = a; i < b; i++)
static lld r,j,l,x,y,b,m,n,s,q,i,z,p,k,c,d,t,u,e,g;string s1,s2,s3,s4;
lld link[10000001],size[10000001],visited[10000001];
lld find(lld x)
{
  while(x!=link[x])
    x=link[x];
  return x;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n;
  p=0;s=0;
  char a[n+4][n+4];lld b[n+4][n+4];
  rep(i,0,n+4)
  {
    rep(j,0,n+4)
    {
      if(i==0||i==1||j==n+2||j==n+3||i==n+2||i==n+3||j==0||j==1)
        a[i][j]='.';
      else
        {
          cin>>a[i][j];b[i][j]=0;
          if(a[i][j]=='#')
            s++;
        }
    }
  }
  rep(i,2,n+2)
  {
    rep(j,2,n+2)
    {
      if(a[i][j]=='#'&&a[i][j-1]=='#'&&a[i][j+1]=='#'&&a[i-1][j]=='#'&&a[i+1][j]=='#')
      {
        p++;
        b[i][j]=b[i-1][j]=b[i+1][j]=b[i][j-1]=b[i][j+1]=1;
      }
    }
  }
  rep(i,2,n+2)
  {
    rep(j,2,n+2)
    {
      if(a[i][j]=='#'&&b[i][j]==0)
      {
        cout<<"NO";return 0;
      }
    }
  }
  cout<<"YES";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<lld> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i,a,b) for (int i = a; i < b; i++)
static lld r,j,l,x,y,b,m,n,s,q,i,z,p,k,c,d,t,u,e,g;string s1,s2,s3,s4;
lld link[10000001],size[10000001],visited[10000001];
lld find(lld x)
{
  while(x!=link[x])
    x=link[x];
  return x;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin>>n;
  p=0;s=0;
  char a[n+4][n+4];
  rep(i,0,n+4)
  {
    rep(j,0,n+4)
    {
      if(i==0||i==1||j==n+2||j==n+3||i==n+2||i==n+3||j==0||j==1)
        a[i][j]='.';
      else
        {
          cin>>a[i][j];
          if(a[i][j]=='#')
            s++;
        }
    }
  }
  rep(i,2,n+2)
  {
    rep(j,2,n+2)
    {
      if(a[i][j]=='#'&&a[i][j-1]=='#'&&a[i][j+1]=='#'&&a[i-1][j]=='#'&&a[i+1][j]=='#')
      {
        p++;
      }
    }
  }
  p=p*5;
  if(p==s)
    cout<<"YES";
  else
    cout<<"NO";
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for (long long i = (a); i <= (b); i++)
#define ll long long
#define x1 asdasdsaas
#define y1 cdaca1231
#define fi first
#define se second
using namespace std;
const int MAXN = 1e6;
int n, res;
int dy[5] = {-1,0,1,0};
int dx[5] = {1,1,1,2};
char a[1000][1000];
int main(){
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	cin>>n;
	FOR(i,1,n){
		FOR(j,1,n) cin>>a[i][j];
	}
	FOR(i,1,n){
		FOR(j,1,n) if (a[i][j]=='#') {
			a[i][j]='.';
			FOR(k,0,3) {
				int tx=i+dx[k];
				int ty=j+dy[k];
				// cout<<i<<" "<<j<<endl;
				// cout<<tx<<" "<<ty<<endl;
				if (tx<1||tx>n||ty<1||ty>n){
					cout<<"NO"; return 0;
				}
				a[tx][ty]='.';
			}
		}
	}
	FOR(i,1,n){
		FOR(j,1,n) if (a[i][j]=='#') cout<<"NO";
	}
	// FOR(i,1,n){
	// 	FOR(j,1,n) cout<<a[i][j];
	// 	cout<<endl;
	// }
	cout<<"YES";
	return 0;
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for (long long i = (a); i <= (b); i++)
#define ll long long
#define x1 asdasdsaas
#define y1 cdaca1231
#define fi first
#define se second
using namespace std;
const int MAXN = 1e6;
int n, res;
int dy[5] = {-1,0,1,0};
int dx[5] = {1,1,1,2};
char a[1000][1000];
int main(){
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	cin>>n;
	FOR(i,1,n){
		FOR(j,1,n) cin>>a[i][j];
	}
	FOR(i,1,n){
		FOR(j,1,n) if (a[i][j]=='#') {
			a[i][j]='.';
			FOR(k,0,3) {
				int tx=i+dx[k];
				int ty=j+dy[k];
				// cout<<i<<" "<<j<<endl;
				// cout<<tx<<" "<<ty<<endl;
				if (tx<1||tx>n||ty<1||ty>n){
					cout<<"NO"; return 0;
				}
				if (a[tx][ty]=='#') a[tx][ty]='.'; else {
					cout<<"NO"; return 0;
				}
			}
		}
	}
	FOR(i,1,n){
		FOR(j,1,n) if (a[i][j]=='#') cout<<"NO";
	}
	// FOR(i,1,n){
	// 	FOR(j,1,n) cout<<a[i][j];
	// 	cout<<endl;
	// }
	cout<<"YES";
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int n;
char a[102][102];

int dr[]={1,0,-1,0};
int dc[]={0,1,0,-1};

bool check(int x,int y){
    if(a[x][y]=='#'){
        return false;
    }
    for(int i=0;i<4;i++){
        if(x+dr[i]>n-1||y+dc[i]>n-1||a[x+dr[i]][y+dc[i]]=='#'){
            return false;
        }
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>a[i][j];
        }
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(check(i,j)){
                return cout<<"YES",0;
            }
        }
    }
    cout<<"NO";
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int n;
char a[102][102];

int dr[]={1,0,-1,0};
int dc[]={0,1,0,-1};

bool check(int x,int y){
    if(a[x][y]=='#'){
        return false;
    }
    for(int i=0;i<4;i++){
        if(x+dr[i]<0||y+dc[i]<0||x+dr[i]>n-1||y+dc[i]>n-1||a[x+dr[i]][y+dc[i]]=='#'){
            return false;
        }
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>a[i][j];
        }
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(check(i,j)){
                return cout<<"YES",0;
            }
        }
    }
    cout<<"NO";
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int n;
char a[102][102];
bool v[102][102];
int dr[]={1,0,-1,0};
int dc[]={0,1,0,-1};

void check(int x,int y){
    if(a[x][y]=='.'||v[x][y]){
        return ;
    }
    for(int i=0;i<4;i++){
        if(x+dr[i]<0||y+dc[i]<0||x+dr[i]>n-1||y+dc[i]>n-1||a[x+dr[i]][y+dc[i]]=='.'||v[x+dr[i]][y+dc[i]]){
            return;
        }
    }
    v[x][y]=1;
    for(int i=0;i<4;i++){
        v[x+dr[i]][y+dc[i]]=1;
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>a[i][j];
        }
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            check(i,j);
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(a[i][j]=='#'&&!v[i][j])
            return cout<<"NO",0;
        }
    }
    cout<<"YES";
    return 0;
}


#include<iostream>
using namespace std;
int main(void)
{
    int i, j, n, sum1, sum2;
    char arr[110][110];
    while(cin>>n)
    {
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                cin>>arr[i][j];

        sum1 = sum2 = 0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(arr[i][j] == '#')
                    sum1++;
                else if(arr[i][j] == '.')
                    sum2++;
            }
        }

        if((sum1%5 == 0 && (n*sum1 < sum2) ) || (sum2 == n) || (sum1%5 == 0 && sum2%5 == 0))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main(void)
{
    int i, j, n, sum1, sum2;
    char arr[110][110];
    while(cin>>n)
    {
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                cin>>arr[i][j];

        sum1 = sum2 = 0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(arr[i][j] == '#')
                    sum1++;
                else if(arr[i][j] == '.')
                    sum2++;
            }
        }

        if(n%5==0 || (sum1+sum1)!=(n+sum2))
        {
            if(sum1%5==0 || sum2==n*n)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
        else
            cout<<"NO"<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <ctime>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <functional>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ld long double
#define y1 abc
#define endl '\n'
#define fi first
#define se second
#define m_p make_pair
#define pb push_back
#define MAXLL 9000000000000000000LL
#define MAXINT 2000000000
#define MINLL -9000000000000000000LL
#define MININT -2000000000
#define stoi aaaaavasd
#define pll pair < ll, ll >

using namespace std;
using namespace __gnu_pbds;

typedef tree< int, null_type, less< int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;

const ld pi = acos(-1);
const ll md = 1e9 + 7;

ll n, i, j;

bool field[151][151], used[151][151];

string s;

bool check(ll x, ll y)
{
    return field[x][y] && field[x - 1][y] && field[x][y - 1] && field[x + 1][y] && field[x][y + 1];
}

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

    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    for (i = 1; i <= n; i++){
        cin >> s;

        for (j = 0; j < n; j++){
            field[i][j + 1] = (s[j] == '#');
        }
    }

    for (i = 2; i < n; i++){
        for (j = 2; j < n; j++){
            if (check(i, j)){
                if (used[i - 1][j] || used[i][j] || used[i][j - 1] || used[i + 1][j] || used[i][j + 1]){
                    cout << "NO" << endl;

                    return 0;
                }

                used[i - 1][j] = used[i][j] = used[i][j - 1] = used[i + 1][j] = used[i][j + 1] = true;
            }
        }
    }

    cout << "YES" << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <ctime>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <functional>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ld long double
#define y1 abc
#define endl '\n'
#define fi first
#define se second
#define m_p make_pair
#define pb push_back
#define MAXLL 9000000000000000000LL
#define MAXINT 2000000000
#define MINLL -9000000000000000000LL
#define MININT -2000000000
#define stoi aaaaavasd
#define pll pair < ll, ll >

using namespace std;
using namespace __gnu_pbds;

typedef tree< int, null_type, less< int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;

const ld pi = acos(-1);
const ll md = 1e9 + 7;

ll n, i, j;

bool field[151][151], used[151][151];

string s;

bool check(ll x, ll y)
{
    return field[x][y] && field[x - 1][y] && field[x][y - 1] && field[x + 1][y] && field[x][y + 1];
}

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

    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    for (i = 1; i <= n; i++){
        cin >> s;

        for (j = 0; j < n; j++){
            field[i][j + 1] = (s[j] == '#');
        }
    }

    for (i = 2; i < n; i++){
        for (j = 2; j < n; j++){
            if (check(i, j)){
                if (used[i - 1][j] || used[i][j] || used[i][j - 1] || used[i + 1][j] || used[i][j + 1]){
                    cout << "NO" << endl;

                    return 0;
                }

                used[i - 1][j] = used[i][j] = used[i][j - 1] = used[i + 1][j] = used[i][j + 1] = true;
            }
        }
    }

    for (i = 1; i <= n; i++){
        for (j = 1; j <= n; j++){
            if (field[i][j] != used[i][j]){
                cout << "NO" << endl;

                return 0;
            }
        }
    }

    cout << "YES" << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <ctime>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <functional>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ld long double
#define y1 abc
#define endl '\n'
#define fi first
#define se second
#define m_p make_pair
#define pb push_back
#define MAXLL 9000000000000000000LL
#define MAXINT 2000000000
#define MINLL -9000000000000000000LL
#define MININT -2000000000
#define stoi aaaaavasd
#define pll pair < ll, ll >

using namespace std;
using namespace __gnu_pbds;

typedef tree< int, null_type, less< int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;

const ld pi = acos(-1);
const ll md = 1e9 + 7;

ll n, i, j;

bool field[151][151], used[151][151];

string s;

bool check(ll x, ll y)
{
    return field[x][y] && field[x - 1][y] && field[x][y - 1] && field[x + 1][y] && field[x][y + 1];
}

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

    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    for (i = 1; i <= n; i++){
        cin >> s;

        for (j = 0; j < n; j++){
            field[i][j + 1] = (s[j] == '#');
        }
    }

    for (i = 2; i < n; i++){
        for (j = 2; j < n; j++){
            if (check(i, j)){
                if (used[i - 1][j] || used[i][j] || used[i][j - 1] || used[i + 1][j] || used[i][j + 1]){
                    continue;
                }

                used[i - 1][j] = used[i][j] = used[i][j - 1] = used[i + 1][j] = used[i][j + 1] = true;
            }
        }
    }

    for (i = 1; i <= n; i++){
        for (j = 1; j <= n; j++){
            if (field[i][j] != used[i][j]){
                cout << "NO" << endl;

                return 0;
            }
        }
    }

    cout << "YES" << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int,int>
#define pll pair<lli, lli>
#define vi  vector<int>
#define vii vector<pair<int,int>>
#define vll vector<lli>
#define pb  push_back
#define mpr  make_pair
#define s  second
#define f first
#define INF 1000000001
#define inf1 1000000000000000001
#define pie 3.14159265358979323846264338327950L
#define all(x) x.begin(),x.end()
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define testcase() int tc;cin>>tc;while(tc--)
#define clean(x) memset(x,0,sizeof(x))

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
	cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
	cin >> p.first;
	return cin >> p.second;
}
/// RULESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS////
/// READ THEM ALOUD......................................................................................................
/// 1. Read the problem at least twice
/// 2. Brute force if contraints r low
/// 3. Check base cases or break case if found any T-T
/// 4. :D

lli const mod = 998244353;
lli const maxn= 1e6+7;
int n;
string a[1000];
bool good(int i,int j)
{
    if(i<0 or i>=n or j<0 or j>=n or a[i][j]=='.')return false;
    return true;
}
void solve()
{
    cin>>n;
    int vis[n][n];
    clean(vis);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(good(i,j) and good(i+1,j) and good(i-1,j) and good(i,j-1) and good(i,j+1))
        {
            if(vis[i][j]==1 or vis[i-1][j]==1 or vis[i+1][j]==1 or vis[i][j-1]==1 or vis[i][j+1]==1)
            {
                cout<<"NO\n";return;
            }
            vis[i][j]=1 , vis[i-1][j]=1 , vis[i+1][j]=1 ,vis[i][j-1]=1 ,vis[i][j+1]=1;
        }
        }
    }
    cout<<"YES\n";

}
int main()
{
    solve();
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int,int>
#define pll pair<lli, lli>
#define vi  vector<int>
#define vii vector<pair<int,int>>
#define vll vector<lli>
#define pb  push_back
#define mpr  make_pair
#define s  second
#define f first
#define INF 1000000001
#define inf1 1000000000000000001
#define pie 3.14159265358979323846264338327950L
#define all(x) x.begin(),x.end()
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define testcase() int tc;cin>>tc;while(tc--)
#define clean(x) memset(x,0,sizeof(x))

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
	cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
	cin >> p.first;
	return cin >> p.second;
}
/// RULESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS////
/// READ THEM ALOUD......................................................................................................
/// 1. Read the problem at least twice
/// 2. Brute force if contraints r low
/// 3. Check base cases or break case if found any T-T
/// 4. :D

const int MAX = 100 + 2;
const int MAXLOG = log2(MAX) + 2;
const int MAXN = 1e5 + 2;
const int MOD = 1e9 + 7;

string s[MAX];

int main()
{
//    READ;
    FASTIO;

    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> s[i];

    bool ok = true;
    for(int i = 0; i < n and ok; i++){
        for(int j = 0; j < n and ok; j++){
            if(s[i][j] == '#'){
                if(i+1>=n) ok = false;
                if(i+2>=n) ok = false;
                if(j+1>=n) ok = false;
                if(j-1>=n) ok = false;
                if(!ok) break;
                if(s[i+1][j]!='#') ok = false;
                if(s[i+2][j]!='#') ok = false;
                if(s[i+1][j+1]!='#') ok = false;
                if(s[i+1][j-1]!='#') ok = false;
                if(!ok) break;
                s[i+1][j] = '.';
                s[i+2][j] = '.';
                s[i+1][j+1] = '.';
                s[i+1][j-1] = '.';
            }
        }
    }

    if(ok) cout << "YES";
    else cout << "NO";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int,int>
#define pll pair<lli, lli>
#define vi  vector<int>
#define vii vector<pair<int,int>>
#define vll vector<lli>
#define pb  push_back
#define mpr  make_pair
#define s  second
#define f first
#define INF 1000000001
#define inf1 1000000000000000001
#define pie 3.14159265358979323846264338327950L
#define all(x) x.begin(),x.end()
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define testcase() int tc;cin>>tc;while(tc--)
#define clean(x) memset(x,0,sizeof(x))

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
	cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
	cin >> p.first;
	return cin >> p.second;
}
/// RULESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS////
/// READ THEM ALOUD......................................................................................................
/// 1. Read the problem at least twice
/// 2. Brute force if contraints r low
/// 3. Check base cases or break case if found any T-T
/// 4. :D

lli const mod = 998244353;
lli const maxn= 1e6+7;
int n;
string a[1000];
bool good(int i,int j)
{
    if(i<0 or i>=n or j<0 or j>=n or a[i][j]=='.')return false;
    return true;
}
void solve()
{
    cin>>n;
    int vis[n][n];
    clean(vis);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(good(i,j) and good(i+1,j) and good(i-1,j) and good(i,j-1) and good(i,j+1))
        {
            if(vis[i][j]==1 or vis[i-1][j]==1 or vis[i+1][j]==1 or vis[i][j-1]==1 or vis[i][j+1]==1)
            {
                cout<<"NO\n";return;
            }
            vis[i][j]=1 , vis[i-1][j]=1 , vis[i+1][j]=1 ,vis[i][j-1]=1 ,vis[i][j+1]=1;
        }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(a[i][j]=='#' and vis[i][j]==0)
            {
                cout<<"NO\n";return;
            }
        }
    }
    cout<<"YES\n";

}
int main()
{
    solve();
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int,int>
#define pll pair<lli, lli>
#define vi  vector<int>
#define vii vector<pair<int,int>>
#define vll vector<lli>
#define pb  push_back
#define mpr  make_pair
#define s  second
#define f first
#define INF 1000000001
#define inf1 1000000000000000001
#define pie 3.14159265358979323846264338327950L
#define all(x) x.begin(),x.end()
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define testcase() int tc;cin>>tc;while(tc--)
#define clean(x) memset(x,0,sizeof(x))

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
	cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
	cin >> p.first;
	return cin >> p.second;
}
/// RULESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS////
/// READ THEM ALOUD......................................................................................................
/// 1. Read the problem at least twice
/// 2. Brute force if contraints r low
/// 3. Check base cases or break case if found any T-T
/// 4. :D

const int MAX = 100 + 2;
const int MAXLOG = log2(MAX) + 2;
const int MAXN = 1e5 + 2;
const int MOD = 1e9 + 7;

string s[MAX];

int main()
{
//    READ;

    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> s[i];

    bool ok = true;
    for(int i = 0; i < n and ok; i++){
        for(int j = 0; j < n and ok; j++){
            if(s[i][j] == '#'){
                if(i+1>=n) ok = false;
                if(i+2>=n) ok = false;
                if(j+1>=n) ok = false;
                if(j-1>=n) ok = false;
                if(!ok) break;
                if(s[i+1][j]!='#') ok = false;
                if(s[i+2][j]!='#') ok = false;
                if(s[i+1][j+1]!='#') ok = false;
                if(s[i+1][j-1]!='#') ok = false;
                if(!ok) break;
                s[i+1][j] = '.';
                s[i+2][j] = '.';
                s[i+1][j+1] = '.';
                s[i+1][j-1] = '.';
            }
        }
    }

    if(ok) cout << "YES";
    else cout << "NO";

    return 0;
}