Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task.

Two matrices $A$ and $B$ are given, each of them has size $n \times m$. Nastya can perform the following operation to matrix $A$ unlimited number of times:   take any square square submatrix of $A$ and transpose it (i.e. the element of the submatrix which was in the $i$-th row and $j$-th column of the submatrix will be in the $j$-th row and $i$-th column after transposing, and the transposed submatrix itself will keep its place in the matrix $A$). 

Nastya's task is to check whether it is possible to transform the matrix $A$ to the matrix $B$.

 $\left. \begin{array}{|c|c|c|c|c|c|c|c|} \hline 6 & {3} & {2} & {11} \\ \hline 5 & {9} & {4} & {2} \\ \hline 3 & {3} & {3} & {3} \\ \hline 4 & {8} & {2} & {2} \\ \hline 7 & {8} & {6} & {4} \\ \hline \end{array} \right.$ Example of the operation 

As it may require a lot of operations, you are asked to answer this question for Nastya.

A square submatrix of matrix $M$ is a matrix which consist of all elements which comes from one of the rows with indeces $x, x+1, \dots, x+k-1$ of matrix $M$ and comes from one of the columns with indeces $y, y+1, \dots, y+k-1$ of matrix $M$. $k$ is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes).


-----Input-----

The first line contains two integers $n$ and $m$ separated by space ($1 \leq n, m \leq 500$) — the numbers of rows and columns in $A$ and $B$ respectively.

Each of the next $n$ lines contains $m$ integers, the $j$-th number in the $i$-th of these lines denotes the $j$-th element of the $i$-th row of the matrix $A$ ($1 \leq A_{ij} \leq 10^{9}$).

Each of the next $n$ lines contains $m$ integers, the $j$-th number in the $i$-th of these lines denotes the $j$-th element of the $i$-th row of the matrix $B$ ($1 \leq B_{ij} \leq 10^{9}$).


-----Output-----

Print "YES" (without quotes) if it is possible to transform $A$ to $B$ and "NO" (without quotes) otherwise.

You can print each letter in any case (upper or lower).


-----Examples-----
Input
2 2
1 1
6 1
1 6
1 1

Output
YES
Input
2 2
4 4
4 5
5 4
4 4

Output
NO
Input
3 3
1 2 3
4 5 6
7 8 9
1 4 7
2 5 6
3 8 9

Output
YES


-----Note-----

Consider the third example. The matrix $A$ initially looks as follows.

$$ \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix} $$

Then we choose the whole matrix as transposed submatrix and it becomes

$$ \begin{bmatrix} 1 & 4 & 7\\ 2 & 5 & 8\\ 3 & 6 & 9 \end{bmatrix} $$

Then we transpose the submatrix with corners in cells $(2, 2)$ and $(3, 3)$. 

$$ \begin{bmatrix} 1 & 4 & 7\\ 2 & \textbf{5} & \textbf{8}\\ 3 & \textbf{6} & \textbf{9} \end{bmatrix} $$

So matrix becomes

$$ \begin{bmatrix} 1 & 4 & 7\\ 2 & 5 & 6\\ 3 & 8 & 9 \end{bmatrix} $$

and it is $B$.

Difficulty: MEDIUM
Tags: Sorting

Constructive algorithms
Skill types: Sorting

Starter code:

Solutions:
(n, m) = map(int, input().split())
pa = [{} for _ in range(n + m - 1)]
pb = [{} for _ in range(n + m - 1)]
for i in range(n):
	tmp = list(map(int, input().split()))
	for j in range(m):
		if tmp[j] in pa[i + j]:
			pa[i + j][tmp[j]] += 1
		else:
			pa[i + j][tmp[j]] = 1
for i in range(n):
	tmp = list(map(int, input().split()))
	for j in range(m):
		if tmp[j] in pb[i + j]:
			pb[i + j][tmp[j]] += 1
		else:
			pb[i + j][tmp[j]] = 1
if pa == pb:
	print('YES')
else:
	print('NO')


import sys
from collections import defaultdict as dd
from itertools import permutations as pp
from itertools import combinations as cc
from collections import Counter as ccd
from random import randint as rd
from bisect import bisect_left as bl
import heapq
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
(n, m) = ri()
mat = []
mat1 = []
for i in range(n):
	mat.append(ri())
for i in range(n):
	mat1.append(ri())
one = dd(list)
for i in range(n):
	for j in range(m):
		one[i + j].append(mat[i][j])
two = dd(list)
for i in range(n):
	for j in range(m):
		two[i + j].append(mat1[i][j])
for i in one:
	one[i].sort()
for i in two:
	two[i].sort()
f = 1
for i in one:
	if one[i] != two[i]:
		f = 0
if f:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
let = 'abcdefghijklmnopqrstuvwxyz'
(n, m) = f()
m1 = [[] for i in range(n + m)]
m2 = [[] for i in range(n + m)]
for i in range(n):
	l = il()
	for j in range(m):
		m1[i + j].append(l[j])
for i in range(n):
	l = il()
	for j in range(m):
		m2[i + j].append(l[j])
for i in range(n + m):
	m1[i].sort()
	m2[i].sort()
	if m1[i] != m2[i]:
		print('NO')
		exit()
print('YES')


from collections import defaultdict
from collections import Counter
import sys
(r, c) = map(int, input().split())
m1 = []
m2 = []
for i in range(r):
	a = list(map(int, input().split()))
	m1.append(a)
for i in range(r):
	a = list(map(int, input().split()))
	m2.append(a)
d1 = defaultdict(list)
d2 = defaultdict(list)
for i in range(r):
	for j in range(c):
		d1[i + j].append(m1[i][j])
		d2[i + j].append(m2[i][j])
for (i, j) in zip(d1, d2):
	if not Counter(d1[i]) == Counter(d2[j]):
		print('NO')
		sys.exit()
print('YES')


(n, m) = [int(x) for x in input().split()]
A = []
B = []
for _ in range(n):
	A.append([int(x) for x in input().split()])
for _ in range(n):
	B.append([int(x) for x in input().split()])
AD = []
BD = []
for s in range(2, m + n + 1):
	at = []
	bt = []
	for r in range(1, n + 1):
		c = s - r
		if 1 <= c and c <= m:
			at.append(A[r - 1][c - 1])
			bt.append(B[r - 1][c - 1])
	at.sort()
	bt.sort()
	AD.append(at)
	BD.append(bt)
if AD == BD:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().strip().split())

def transpose(matrix):
	to_return = [[] for i in range(m)]
	for i in range(m):
		for j in range(n):
			to_return[i].append(matrix[j][i])
	return to_return
(A, B) = ([], [])
for i in range(n):
	A.append([int(i) for i in input().strip().split()])
for i in range(n):
	B.append([int(i) for i in input().strip().split()])
yes = True
pivot = 0
not_exceed = 0
if m < n:
	A = transpose(A)
	B = transpose(B)
	(m, n) = (n, m)
index = 0
while index < m:
	col_index = index
	row_index = 0
	numbers = dict()
	while row_index < n and col_index >= 0:
		if A[row_index][col_index] not in numbers:
			numbers[A[row_index][col_index]] = 1
		else:
			numbers[A[row_index][col_index]] += 1
		row_index += 1
		col_index -= 1
	col_index = index
	row_index = 0
	while row_index < n and col_index >= 0:
		if B[row_index][col_index] in numbers and numbers[B[row_index][col_index]] > 0:
			numbers[B[row_index][col_index]] -= 1
		else:
			yes = False
			break
		row_index += 1
		col_index -= 1
	index += 1
	if not yes:
		break
index = 1
while index < n:
	col_index = m - 1
	row_index = index
	numbers = dict()
	while row_index < n and col_index >= 0:
		if A[row_index][col_index] not in numbers:
			numbers[A[row_index][col_index]] = 1
		else:
			numbers[A[row_index][col_index]] += 1
		row_index += 1
		col_index -= 1
	col_index = m - 1
	row_index = index
	while row_index < n and col_index >= 0:
		if B[row_index][col_index] in numbers and numbers[B[row_index][col_index]] > 0:
			numbers[B[row_index][col_index]] -= 1
		else:
			yes = False
			break
		row_index += 1
		col_index -= 1
	index += 1
	if not yes:
		break
print('YES' if yes else 'NO')


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
grid1 = [list(input().split()) for _ in range(n)]
grid2 = [list(input().split()) for _ in range(n)]
for i in range(n + m - 1):
	(s1, s2) = ([], [])
	for j in range(n):
		(x, y) = (j, i - j)
		if 0 <= x < n and 0 <= y < m:
			s1.append(grid1[x][y])
			s2.append(grid2[x][y])
	if sorted(s1) != sorted(s2):
		print('NO')
		sys.exit()
print('YES')


import sys
input = iter(sys.stdin)
(n, m) = map(int, next(input).split())

def read_matrix():
	mat = []
	for _ in range(n):
		mat.append(tuple(map(int, next(input).split())))
	return build_diags(mat)

def build_diags(mat):
	import collections
	diags = collections.defaultdict(list)
	for i in range(n):
		for j in range(m):
			diags[i + j].append(mat[i][j])
	diags = sorted(((k, sorted(v)) for (k, v) in diags.items()))
	return diags
d1 = read_matrix()
d2 = read_matrix()
print(['NO', 'YES'][d1 == d2])


(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
dla = [list() for _ in range(n + m)]
dlb = [list() for _ in range(n + m)]
for i in range(n):
	for j in range(m):
		dla[i + j].append(a[i][j])
		dlb[i + j].append(b[i][j])
for i in range(n + m):
	if sorted(dla[i]) != sorted(dlb[i]):
		print('NO')
		exit()
print('YES')


(n, m) = [int(i) for i in input().split()]
a = [[] for i in range(n + m + 1)]
b = [[] for i in range(n + m + 1)]
s = []
st = []
for i in range(n):
	p = [int(i) for i in input().split()]
	s.append(p)
for i in range(n):
	p = [int(i) for i in input().split()]
	st.append(p)
for i in range(n):
	for j in range(m):
		a[i + j].append(s[i][j])
		b[i + j].append(st[i][j])
l = 1
for i in range(len(a)):
	a[i].sort()
	b[i].sort()
	if a[i] != b[i]:
		l = 0
		print('No')
		break
if l == 1:
	print('Yes')


(n, m) = [int(s) for s in input().split()]
a = []
b = []
c = []
ans = 0
c = []
d = []
for i in range(n):
	a.append([int(s) for s in input().split()])
for i in range(n):
	b.append([int(s) for s in input().split()])
for i in range(0, n + m - 1):
	c.append([])
	d.append([])
for i in range(n):
	for j in range(m):
		c[i + j].append(a[i][j])
		d[i + j].append(b[i][j])
for i in range(0, n + m - 1):
	e = sorted(c[i])
	f = sorted(d[i])
	if e == f:
		ans += 1
if ans == n + m - 1:
	print('YES')
else:
	print('NO')


import sys

def rint():
	return map(int, sys.stdin.readline().split())
(n, m) = rint()
a = [[] for j in range(n)]
b = [[] for j in range(n)]
for i in range(n):
	a[i] = list(rint())
for i in range(n):
	b[i] = list(rint())
for s in range(n + m - 1):
	adict = dict()
	bdict = dict()
	for r in range(s + 1):
		if r >= n:
			break
		c = s - r
		if c < 0:
			break
		if c >= m:
			continue
		if a[r][c] in adict:
			adict[a[r][c]] += 1
		else:
			adict[a[r][c]] = 1
		if b[r][c] in bdict:
			bdict[b[r][c]] += 1
		else:
			bdict[b[r][c]] = 1
	if adict != bdict:
		print('NO')
		exit()
print('YES')


(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = []
for i in range(n):
	b.append(list(map(int, input().split())))
diasA = [[] for i in range(m + n)]
diasB = [[] for i in range(m + n)]
for i in range(n):
	for j in range(m):
		diasA[i + j].append(a[i][j])
		diasB[i + j].append(b[i][j])
result = all((sorted(da) == sorted(db) for (da, db) in zip(diasA, diasB)))
if result:
	print('YES')
else:
	print('NO')


(n, m) = [int(item) for item in input().split()]
a = [[] for _ in range(n)]
b = [[] for _ in range(n)]
for i in range(n):
	a[i] = [int(item) for item in input().split()]
for i in range(n):
	b[i] = [int(item) for item in input().split()]
ok = True
for i in range(n):
	for l in range(m):
		if a[i][l] == b[i][l]:
			continue
		flag = False
		for col in range(max(0, l - n + i + 1), min(l, m)):
			if b[i][l] == a[i + l - col][col]:
				flag = True
				(a[i][l], a[i + l - col][col]) = (a[i + l - col][col], a[i][l])
		if not flag:
			ok = False
			break
	if ok == False:
		break
print('YES' if ok else 'NO')


from collections import Counter

def diagonals(a):
	a_diagonals = []
	for j in range(m):
		i = 0
		a_diagonals.append([])
		while j >= 0 and i < n:
			a_diagonals[-1].append(a[i][j])
			i += 1
			j -= 1
	for i in range(1, n):
		j = m - 1
		a_diagonals.append([])
		while i < n and j >= 0:
			a_diagonals[-1].append(a[i][j])
			i += 1
			j -= 1
	diagonals = [dict(Counter(i)) for i in a_diagonals]
	return diagonals
(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = []
for i in range(n):
	b.append(list(map(int, input().split())))
if diagonals(a) == diagonals(b):
	print('YES')
else:
	print('NO')


import collections as z
k = lambda : map(int, input().split())
l = lambda : list(k())
(n, m) = k()
(p, q, d) = ([l() for _ in range(n)], [l() for _ in range(n)], [z.defaultdict(int) for i in range(n + m)])
for i in range(n):
	for j in range(m):
		d[i + j][p[i][j]] += 1
		d[i + j][q[i][j]] -= 1
print('YES' if all((all((i[j] == 0 for j in i)) for i in d)) else 'NO')


the_string = input()
(rows, coloumns) = the_string.split()
row = int(rows)
coloumn = int(coloumns)
biggestdiag = min(row, coloumn)
A = []
B = []
for i in range(row):
	string = input()
	string = string.split()
	A.append(string)
for i in range(row):
	string = input()
	string = string.split()
	B.append(string)
for i in range(biggestdiag):
	for j in range(biggestdiag):
		A[i].append(None)
for i in range(max(row, coloumn)):
	temp = []
	for j in range(max(row, coloumn)):
		temp.append(None)
	A.append(temp)
DiagA = []
for i in range(coloumn):
	diagtemp = []
	for j in range(biggestdiag):
		if A[j][i - j] != None:
			diagtemp.append(A[j][i - j])
			A[j][i - j] = None
	DiagA.append(diagtemp)
	diagtemp = []
for i in range(row - 1):
	diagtemp = []
	for j in range(biggestdiag):
		if A[i + j + 1][coloumn - j - 1] != None:
			diagtemp.append(A[i + j + 1][coloumn - j - 1])
			A[i + j + 1][coloumn - j - 1] = None
	DiagA.append(diagtemp)
	diagtemp = []
DiagB = []
for i in range(biggestdiag):
	for j in range(biggestdiag):
		B[i].append(None)
for i in range(max(row, coloumn)):
	temp = []
	for j in range(max(row, coloumn)):
		temp.append(None)
	B.append(temp)
DiagB = []
for i in range(coloumn):
	diagtemp = []
	for j in range(biggestdiag):
		if B[j][i - j] != None:
			diagtemp.append(B[j][i - j])
			B[j][i - j] = None
	DiagB.append(diagtemp)
	diagtemp = []
for i in range(row - 1):
	diagtemp = []
	for j in range(biggestdiag):
		if B[i + j + 1][coloumn - j - 1] != None:
			diagtemp.append(B[i + j + 1][coloumn - j - 1])
			B[i + j + 1][coloumn - j - 1] = None
	DiagB.append(diagtemp)
	diagtemp = []
output = True
for i in range(row + coloumn - 1):
	DiagA[i].sort()
	DiagB[i].sort()
	if DiagA[i] != DiagB[i]:
		output = False
		break
if output == True:
	print('YES')
else:
	print('NO')


from collections import Counter

def input_matrix():
	res = tuple((Counter() for _ in range(n + m)))
	for i in range(n):
		for (j, a) in enumerate(map(int, input().split())):
			res[i + j][a] += 1
	return res
(n, m) = map(int, input().split())
if input_matrix() == input_matrix():
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
list1 = [list(map(int, input().split())) for _ in range(n)]
list2 = [list(map(int, input().split())) for _ in range(n)]
test1 = [[] for i in range(m + n)]
test2 = [[] for i in range(m + n)]
for i in range(n):
	for j in range(m):
		test1[i + j].append(list1[i][j])
		test2[i + j].append(list2[i][j])
result = all((sorted(i) == sorted(j) for (i, j) in zip(test1, test2)))
if result:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
sa = [[] for _ in range(n + m - 1)]
sb = [[] for _ in range(n + m - 1)]
for i in range(n):
	for j in range(m):
		sa[i + j].append(a[i][j])
		sb[i + j].append(b[i][j])
for i in range(n + m - 1):
	sa[i].sort()
	sb[i].sort()
	if sa[i] != sb[i]:
		print('NO')
		exit()
print('YES')


A = []
B = []
[N, M] = [int(x) for x in input().split()]
for i in range(N):
	A.append([int(x) for x in input().split()])
for i in range(N):
	B.append([int(x) for x in input().split()])
a_t = [[] for x in range(1010)]
b_t = [[] for x in range(1010)]
for n in range(N):
	for m in range(M):
		a_t[n + m].append(A[n][m])
		b_t[n + m].append(B[n][m])
for i in range(0, 1000):
	a_t[i] = sorted(a_t[i])
	b_t[i] = sorted(b_t[i])
	if a_t[i] != b_t[i]:
		print
		print('NO')
		exit()
print('YES')


(N, M) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = [list(map(int, input().split())) for i in range(N)]
ans = 'YES'
for i in range(N):
	t1 = sorted([A[i - j][j] for j in range(min(N, M)) if i - j >= 0])
	t2 = sorted([B[i - j][j] for j in range(min(N, M)) if i - j >= 0])
	if t1 != t2:
		ans = 'NO'
		break
for j in range(M):
	t1 = sorted([A[N - i - 1][j + i] for i in range(min(N, M)) if j + i < M])
	t2 = sorted([B[N - i - 1][j + i] for i in range(min(N, M)) if j + i < M])
	if t1 != t2:
		ans = 'NO'
		break
print(ans)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
(n, m) = mi()
a = [li() for i in range(n)]
b = [li() for i in range(n)]
p = [[] for i in range(n + m)]
q = [[] for i in range(n + m)]
for i in range(n):
	for j in range(m):
		p[i + j].append(a[i][j])
		q[i + j].append(b[i][j])
print('YES' if all((sorted(x) == sorted(y) for (x, y) in zip(p, q))) else 'NO')


import collections
(n, m) = map(int, input().split())
A = [[0 for i in range(m)] for j in range(n)]
B = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	X = input().split()
	for j in range(m):
		A[i][j] = int(X[j])
for i in range(n):
	X = input().split()
	for j in range(m):
		B[i][j] = int(X[j])
for nn in range(m + n - 1):
	AA = collections.Counter()
	BB = collections.Counter()
	for j in range(nn + 1):
		x = nn - j
		y = j
		if x < n and y < m:
			AA[A[x][y]] += 1
			BB[B[x][y]] += 1
	for i in AA.keys():
		if AA[i] != BB[i]:
			print('NO')
			exit()
print('YES')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split())))
for j in range(n):
	b.append(list(map(int, input().split())))
f = 0
for i in range(m):
	t = i
	r = []
	d = 0
	r1 = []
	for j in range(t + 1):
		r.append(a[d][i - j])
		r1.append(b[d][i - j])
		d += 1
		if d == n:
			break
	r.sort()
	r1.sort()
	if r != r1:
		f = 1
		break
for i in range(1, n):
	t = m - 1
	r = []
	d = i
	r1 = []
	for j in range(t + 1):
		r.append(a[d][t - j])
		r1.append(b[d][t - j])
		d += 1
		if d == n:
			break
	r.sort()
	r1.sort()
	if r != r1:
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def I():
	return int(sys.stdin.readline())

def neo():
	return map(int, sys.stdin.readline().split())

def Neo():
	return list(map(int, sys.stdin.readline().split()))
(n, m) = neo()
f = 0
(A, B) = ([Neo() for i in range(n)], [Neo() for i in range(n)])
(A1, B1) = ([[] for i in range(n + m)], [[] for i in range(n + m)])
for i in range(n):
	for j in range(m):
		A1[i + j].append(A[i][j])
		B1[i + j].append(B[i][j])
for (i, j) in zip(A1, B1):
	if sorted(i) != sorted(j):
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


(n, m) = tuple(map(int, input().strip().split()))
mat1 = []
for i in range(n):
	mat1.append(list(map(int, input().strip().split())))
mat2 = []
for i in range(n):
	mat2.append(list(map(int, input().strip().split())))
l = []
ll = []
for i in range(n + m - 1):
	l.append([])
	ll.append([])
for i in range(n):
	for ii in range(m):
		l[i + ii].append(mat1[i][ii])
		ll[i + ii].append(mat2[i][ii])
for k in range(n + m - 1):
	l[k].sort()
	ll[k].sort()
q = 0
for t in range(n + m - 1):
	for i in range(len(l[t])):
		if l[t][i] != ll[t][i]:
			print('NO')
			q = 1
			break
	if q == 1:
		break
if q == 0:
	print('YES')


inp = lambda : map(int, input().split())

def ans():
	global n, m
	a = [[] for _ in [0] * (n + m)]
	for i in range(n):
		for j in inp():
			a[i].append(j)
			i += 1
	return [*map(sorted, a)]
(n, m) = inp()
if ans() != ans():
	print('NO')
else:
	print('YES')


import math
A = []
B = []
C = []
D = []
a = list(map(int, input().split()))
n = a[0]
m = a[1]
for i in range(n + m - 1):
	C.append([])
for i in range(n + m - 1):
	D.append([])
for i in range(n):
	a = list(map(int, input().split()))
	A.append(a)
for i in range(n):
	a = list(map(int, input().split()))
	B.append(a)
for i in range(n):
	for j in range(m):
		C[i + j].append(A[i][j])
for i in range(n):
	for j in range(m):
		D[i + j].append(B[i][j])
for i in range(n + m - 1):
	C[i].sort()
	D[i].sort()
	if C[i] != D[i]:
		print('NO')
		quit()
print('YES')


def problem_1136c():
	gi = lambda : list(map(int, input().strip().split()))
	(n, m) = gi()
	a_list = [gi() for _ in range(n)]
	b_list = [gi() for _ in range(n)]
	for k in range(m):
		a = sorted([a_list[j][k - j] for j in range(min(n, k + 1))])
		b = sorted([b_list[j][k - j] for j in range(min(n, k + 1))])
		if a != b:
			print('NO')
			return
	for k in range(m):
		a = sorted([a_list[-j - 1][j - 1 - k] for j in range(min(n, k + 1))])
		b = sorted([b_list[-j - 1][-k + j - 1] for j in range(min(n, k + 1))])
		if a != b:
			print('NO')
			return
	if n <= m:
		print('YES')
		return
	for k in range(n):
		a = sorted([a_list[k - j][j] for j in range(min(m, k + 1))])
		b = sorted([b_list[k - j][j] for j in range(min(m, k + 1))])
		if a != b:
			print('NO')
			return
	for k in range(n):
		a = sorted([a_list[-k - 1 + j][-j - 1] for j in range(min(m, k + 1))])
		b = sorted([b_list[-k - 1 + j][-j - 1] for j in range(min(m, k + 1))])
		if a != b:
			print('NO')
			return
	print('YES')
	return
problem_1136c()


import sys
import math
from collections import defaultdict as dd
(n, m) = [int(i) for i in input().split(' ')]
a = []
b = []
for i in range(n):
	a.append([int(i) for i in input().split(' ')])
for i in range(n):
	b.append([int(i) for i in input().split(' ')])
d1 = dd(list)
d2 = dd(list)
for i in range(n):
	for j in range(m):
		d1[i + j].append(a[i][j])
		d2[i + j].append(b[i][j])
for i in d1.keys():
	if sorted(d1[i]) != sorted(d2[i]):
		print('NO')
		sys.exit(0)
print('YES')


MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n, m) = I()
a = [[] for i in range(n + m - 1)]
b = [[] for i in range(n + m - 1)]
for i in range(n):
	l = I()
	for j in range(m):
		a[i + j].append(l[j])
for i in range(n):
	l = I()
	for j in range(m):
		b[i + j].append(l[j])
ans = 'YES'
for i in range(n + m - 1):
	if sorted(a[i]) != sorted(b[i]):
		ans = 'NO'
		break
print(ans)


[r, c] = list(map(int, input().split()))
mat1 = []
mat2 = []
flag = True
for i in range(r):
	mat1.append(list(map(int, input().split())))
for i in range(r):
	mat2.append(list(map(int, input().split())))

def diag(matrix):
	diagmat = []
	for line in range(1, r + c):
		start_col = max(0, line - r)
		count = min(line, c - start_col, r)
		arr = []
		for j in range(0, count):
			arr.append(matrix[min(r, line) - j - 1][start_col + j])
		diagmat.append(arr)
	return diagmat
diag1 = diag(mat1)
diag2 = diag(mat2)
for i in range(len(diag1)):
	diag1[i].sort()
	diag2[i].sort()
	if diag1[i] != diag2[i]:
		flag = False
		break
if flag:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().strip().split())
A = []
B = []
for _ in range(n):
	a = list(map(int, input().strip().split()))
	A.append(a)
for _ in range(n):
	b = list(map(int, input().strip().split()))
	B.append(b)
flag = True
for i in range(n - 1):
	if i == 0:
		if A[0][0] != B[0][0]:
			flag = False
	else:
		a = i
		b = 0
		temp = {}
		temp2 = {}
		while a >= 0 and b < m:
			try:
				temp[B[a][b]] += 1
			except:
				temp[B[a][b]] = 1
			try:
				temp2[A[a][b]] += 1
			except:
				temp2[A[a][b]] = 1
			a -= 1
			b += 1
		for k in temp2.keys():
			try:
				if temp2[k] == temp[k]:
					nothing = 1
				else:
					flag = False
					break
			except:
				flag = False
				break
for j in range(m):
	a = n - 1
	b = j
	temp = {}
	temp2 = {}
	while a >= 0 and b < m:
		try:
			temp[B[a][b]] += 1
		except:
			temp[B[a][b]] = 1
		try:
			temp2[A[a][b]] += 1
		except:
			temp2[A[a][b]] = 1
		a -= 1
		b += 1
	for k in temp2.keys():
		try:
			if temp2[k] == temp[k]:
				nothing = 1
			else:
				flag = False
				break
		except:
			flag = False
			break
if flag:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]
B = [list(map(int, input().split())) for _ in range(n)]
ans = True
for dig in range(n):
	i = dig
	j = 0
	d1 = dict()
	d2 = dict()
	while i >= 0 and j < m:
		d1[A[i][j]] = d1.get(A[i][j], 0) + 1
		d2[B[i][j]] = d2.get(B[i][j], 0) + 1
		i -= 1
		j += 1
	if d1 != d2:
		ans = False
		break
for dig in range(1, m):
	i = n - 1
	j = dig
	d1 = dict()
	d2 = dict()
	while i >= 0 and j < m:
		d1[A[i][j]] = d1.get(A[i][j], 0) + 1
		d2[B[i][j]] = d2.get(B[i][j], 0) + 1
		i -= 1
		j += 1
	if d1 != d2:
		ans = False
		break
print('YES' if ans else 'NO')


R = lambda : map(int, input().split())
(n, m) = R()

def f():
	a = [[] for _ in [0] * (n + m)]
	for i in range(n):
		j = 0
		for x in R():
			a[i + j] += (x,)
			j += 1
	return a
print('YNEOS'[any((sorted(x) != sorted(y) for (x, y) in zip(f(), f())))::2])


def collect_tr(mat, n, m, i, j):
	tr = []
	while i >= 0 and j < m:
		tr.append(mat[i][j])
		i -= 1
		j += 1
	return tr

def main():
	(n, m) = map(int, input().split(' '))
	mat1 = [list(map(int, input().split(' '))) for _ in range(n)]
	mat2 = [list(map(int, input().split(' '))) for _ in range(n)]
	for i in range(n):
		tr1 = sorted(collect_tr(mat1, n, m, i, 0))
		tr2 = sorted(collect_tr(mat2, n, m, i, 0))
		if tr1 != tr2:
			print('NO')
			return 0
	for j in range(1, m):
		tr1 = sorted(collect_tr(mat1, n, m, n - 1, j))
		tr2 = sorted(collect_tr(mat2, n, m, n - 1, j))
		if tr1 != tr2:
			print('NO')
			return 0
	print('YES')
	return 0
exit(main())


from collections import defaultdict
(n, m) = list(map(int, input().split(' ')))
A = []
B = []
for r in range(n):
	A.append(list(map(int, input().split(' '))))
for r in range(n):
	B.append(list(map(int, input().split(' '))))

def compare_maps(map_a, map_b):
	if len(map_a) != len(map_b):
		return False
	for k in map_a.keys():
		if map_a[k] != map_b[k]:
			return False
	return True

def findOut():
	tmp_map_a = defaultdict(lambda : defaultdict(lambda : 0))
	tmp_map_b = defaultdict(lambda : defaultdict(lambda : 0))
	for k in range(n):
		for i in range(m):
			tmp_map_a[k + i][A[k][i]] += 1
			tmp_map_b[k + i][B[k][i]] += 1
	for k in tmp_map_a.keys():
		if not compare_maps(tmp_map_a[k], tmp_map_b[k]):
			return 'NO'
	return 'YES'
print(findOut())


def diagonal(a):
	global n, m
	max_col = n
	max_row = m
	fdiag = [[] for i in range(max_col + max_row - 1)]
	for y in range(max_col):
		for x in range(max_row):
			fdiag[x + y].append(a[y][x])
	return fdiag
(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	temp = list(map(int, input().split()))
	a.append(temp)
for i in range(n):
	temp = list(map(int, input().split()))
	b.append(temp)
ans_a = diagonal(a)
ans_b = diagonal(b)
for i in range(n + m - 1):
	if sorted(ans_a[i]) != sorted(ans_b[i]):
		print('NO')
		exit()
print('YES')


(n, m) = map(int, input().split())
A = [[] for i in range(n)]
B = [[] for i in range(n)]
for i in range(n):
	a = list(map(int, input().split()))
	A[i] = a
for i in range(n):
	a = list(map(int, input().split()))
	B[i] = a
A1 = [[] for i in range(n + m - 1)]
B1 = [[] for i in range(n + m - 1)]
for k in range(n + m - 1):
	for i in range(n):
		for j in range(m):
			if i + j == k:
				A1[k].append(A[i][j])
				B1[k].append(B[i][j])
	A1[k] = sorted(A1[k])
	B1[k] = sorted(B1[k])
	for l in range(len(A1[k])):
		if A1[k][l] != B1[k][l]:
			print('NO')
			exit()
print('YES')


(n, m) = map(int, input().split())
A = dict()
B = dict()
for i in range(n):
	a = list(map(int, input().split()))
	for j in range(m):
		if i + j in A:
			if a[j] in A[i + j]:
				A[i + j][a[j]] += 1
			else:
				A[i + j][a[j]] = 1
		else:
			A[i + j] = {a[j]: 1}
for i in range(n):
	b = list(map(int, input().split()))
	for j in range(m):
		if i + j in B:
			if b[j] in B[i + j]:
				B[i + j][b[j]] += 1
			else:
				B[i + j][b[j]] = 1
		else:
			B[i + j] = {b[j]: 1}
yes = True
for d in A:
	if A[d] != B[d]:
		yes = False
		break
print('YES') if yes else print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import *
from operator import itemgetter, attrgetter
from decimal import *
import bisect
import math
import heapq as hq
(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split())))
for j in range(n):
	b.append(list(map(int, input().split())))
f = 0
first = [[] for i in range(n + m)]
second = [[] for i in range(n + m)]
for i in range(n):
	for j in range(m):
		first[i + j].append(a[i][j])
		second[i + j].append(b[i][j])
for i in range(n + m):
	first[i].sort()
	second[i].sort()
	if first[i] != second[i]:
		f = 1
		print('NO')
		break
else:
	print('YES')


(n, m) = map(int, input().split())
l1 = [[] for i in range(2 * max(n, m))]
mat1 = []
f = 1
mat2 = []
l2 = [[] for i in range(2 * max(m, n))]
for i in range(n):
	mat1.append([int(i) for i in input().split()])
for i in range(n):
	mat2.append([int(i) for i in input().split()])
for i in range(n):
	for j in range(m):
		l1[i + j].append(mat1[i][j])
for i in range(n):
	for j in range(m):
		l2[i + j].append(mat2[i][j])
from collections import Counter
for i in range(len(l1)):
	if Counter(l1[i]) != Counter(l2[i]):
		f = 0
if f:
	print('YES')
else:
	print('NO')


(n, m) = list(map(int, input().split()))
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = []
for i in range(n):
	b.append(list(map(int, input().split())))
for i in range(m + n - 1):
	d1 = {}
	d2 = {}
	for j in range(n):
		l = j
		r = i - j
		if l >= 0 and l < n and (r >= 0) and (r < m):
			e = a[l][r]
			if e not in d1:
				d1[e] = 0
			d1[e] += 1
	for j in range(n):
		l = j
		r = i - j
		if l >= 0 and l < n and (r >= 0) and (r < m):
			e = b[l][r]
			if e not in d2:
				d2[e] = 0
			d2[e] += 1
	for k in d1:
		if k not in d2:
			print('NO')
			return
		if d1[k] != d2[k]:
			print('NO')
			return
print('YES')


import sys
import math as mt
input = sys.stdin.buffer.readline
t = 1
for _ in range(t):
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(list(map(int, input().split())))
	b = []
	for i in range(n):
		b.append(list(map(int, input().split())))
	d1 = {}
	d2 = {}
	for i in range(n):
		for j in range(m):
			if d1.get(i + j, -1) == -1:
				d1[i + j] = []
			if d2.get(i + j, -1) == -1:
				d2[i + j] = []
			d1[i + j].append(a[i][j])
			d2[i + j].append(b[i][j])
	ans = 1
	for i in d1:
		d1[i].sort()
		d2[i].sort()
	if d1 == d2:
		print('YES')
	else:
		print('NO')


(n, m) = [int(x) for x in input().split()]
A = [[int(x) for x in input().split()] for _ in range(n)]
B = [[int(x) for x in input().split()] for _ in range(n)]

def valid(i, j):
	return i >= 0 and j >= 0 and (i < n) and (j < m)
can = True
for i in range(n):
	(i1, j1) = (i, 0)
	a = []
	b = []
	while valid(i1, j1):
		a.append(A[i1][j1])
		b.append(B[i1][j1])
		i1 -= 1
		j1 += 1
	if sorted(a) != sorted(b):
		can = False
		break
if not can:
	print('NO')
	exit(0)
for j in range(m):
	(i1, j1) = (n - 1, j)
	a = []
	b = []
	while valid(i1, j1):
		a.append(A[i1][j1])
		b.append(B[i1][j1])
		i1 -= 1
		j1 += 1
	if sorted(a) != sorted(b):
		can = False
		break
if can:
	print('YES')
else:
	print('NO')


(n, m) = [int(s) for s in input().split()]
a = []
b = []
c = []
d = []
for i in range(n):
	a.append([int(s) for s in input().split()])
for i in range(n):
	b.append([int(s) for s in input().split()])
for i in range(0, n + m - 1):
	c.append([])
	d.append([])
for i in range(n):
	for j in range(m):
		c[i + j].append(a[i][j])
		d[i + j].append(b[i][j])
for i in range(0, n + m - 1):
	c[i].sort()
	d[i].sort()
if c == d:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = [[] for i in range(n + m)]
b = [[] for i in range(n + m)]
for i in range(n):
	j = 0
	for el in map(int, input().split()):
		a[i + j].append(el)
		j += 1
for i in range(n):
	j = 0
	for el in map(int, input().split()):
		b[i + j].append(el)
		j += 1
for i in range(n + m):
	a[i].sort()
	b[i].sort()
	if a[i] != b[i]:
		print('NO')
		exit(0)
print('YES')


gi = lambda : list(map(int, input().strip().split()))
(n, m) = gi()
A = [gi() for e in range(n)]
B = [gi() for e in range(n)]
for k in range(m):
	a = sorted([A[j][k - j] for j in range(min(n, k + 1))])
	b = sorted([B[j][k - j] for j in range(min(n, k + 1))])
	if a != b:
		print('NO')
		exit()
for k in range(m):
	a = sorted([A[-j - 1][j - 1 - k] for j in range(min(n, k + 1))])
	b = sorted([B[-j - 1][-k + j - 1] for j in range(min(n, k + 1))])
	if a != b:
		print('NO')
		exit()
if n <= m:
	print('YES')
	exit()
for k in range(n):
	a = sorted([A[k - j][j] for j in range(min(m, k + 1))])
	b = sorted([B[k - j][j] for j in range(min(m, k + 1))])
	if a != b:
		print('NO')
		exit()
for k in range(n):
	a = sorted([A[-k - 1 + j][-j - 1] for j in range(min(m, k + 1))])
	b = sorted([B[-k - 1 + j][-j - 1] for j in range(min(m, k + 1))])
	if a != b:
		print('NO')
		exit()
print('YES')


from collections import defaultdict
(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
flag = True
for i in range(n):
	d = defaultdict(int)
	for j in range(min(i + 1, m)):
		d[a[i - j][j]] += 1
		d[b[i - j][j]] -= 1
	for k in d.values():
		if k != 0:
			flag = False
for i in range(m):
	d = defaultdict(int)
	for j in range(min(m - i, n)):
		d[a[n - j - 1][i + j]] += 1
		d[b[n - j - 1][i + j]] -= 1
	for k in d.values():
		if k != 0:
			flag = False
if flag:
	print('YES')
else:
	print('NO')


from sys import stdin
from collections import Counter
input = stdin.readline
(n, m) = [int(x) for x in input().split()]
a = [[int(x) for x in input().split()] for _ in range(n)]
b = [[int(x) for x in input().split()] for _ in range(n)]
ans = 'YES'
counter_a = Counter()
counter_b = Counter()
i = 0
r = 0
c = 0
while r < n and ans == 'YES':
	counter_a.clear()
	counter_b.clear()
	while r >= 0 and c < m:
		counter_a[a[r][c]] += 1
		counter_b[b[r][c]] += 1
		r -= 1
		c += 1
	for (k, v) in counter_a.items():
		if v != counter_b[k]:
			ans = 'NO'
			break
	i += 1
	c = 0
	r = i
i = 0
r = n - 1
c = 0
while c < m and ans == 'YES':
	counter_a.clear()
	counter_b.clear()
	while r >= 0 and c < m:
		counter_a[a[r][c]] += 1
		counter_b[b[r][c]] += 1
		r -= 1
		c += 1
	for (k, v) in counter_a.items():
		if v != counter_b[k]:
			ans = 'NO'
			break
	i += 1
	c = i
	r = n - 1
print(ans)


(n, m) = map(int, input().split())
mat1 = []
mat2 = []
for i in range(n):
	mat1.append(list(map(int, input().split())))
for i in range(n):
	mat2.append(list(map(int, input().split())))
i = 0
temp1 = []
temp2 = []
while i < m:
	k = i
	j = 0
	while k >= 0 and j < n:
		temp1.append(mat1[j][k])
		temp2.append(mat2[j][k])
		k -= 1
		j += 1
	temp1.sort()
	temp2.sort()
	if temp1 == temp2:
		temp1.clear()
		temp2.clear()
	else:
		print('NO')
		exit()
	i += 1
i = 1
while i < n:
	k = m - 1
	j = i
	while j < n and k >= 0:
		temp1.append(mat1[j][k])
		temp2.append(mat2[j][k])
		k -= 1
		j += 1
	temp1.sort()
	temp2.sort()
	if temp1 == temp2:
		temp1.clear()
		temp2.clear()
	else:
		print('NO')
		exit()
	i += 1
print('YES')


(n, m) = map(int, input().split())
M1 = [list(map(int, input().split())) for i in range(n)]
M2 = [list(map(int, input().split())) for i in range(n)]
h = [0] * n
for i in range(n):
	M1[i].extend(h)
for i in range(n):
	M2[i].extend(h)
fl = 0
for i in range(1, m + n - 1):
	s1 = dict()
	s2 = dict()
	x = i
	y = i
	while m + n > x >= 0 and n > i - x >= 0:
		if M1[i - x][x] not in s1:
			s1[M1[i - x][x]] = 0
		s1[M1[i - x][x]] += 1
		x -= 1
	while m + n > y >= 0 and n > i - y >= 0:
		if M2[i - y][y] not in s2:
			s2[M2[i - y][y]] = 0
		s2[M2[i - y][y]] += 1
		y -= 1
	if s1 != s2:
		fl = 1
if M1[0][0] != M2[0][0] or M1[-1][-1] != M2[-1][-1]:
	fl = 1
if fl:
	print('NO')
else:
	print('YES')


(n, m) = [int(x) for x in input().split()]
A = []
B = []
for i in range(n):
	a = [int(x) for x in input().split()]
	A.append(a)
for i in range(n):
	a = [int(x) for x in input().split()]
	B.append(a)
a_list = []
for i in range(n):
	a = []
	j = 0
	a.append(A[i][j])
	while 1:
		if i - 1 >= 0 and j + 1 <= m - 1:
			i -= 1
			j += 1
			a.append(A[i][j])
		else:
			break
	a_list.append(a)
i = n - 1
for j in range(1, m):
	i = n - 1
	a = []
	a.append(A[i][j])
	while 1:
		if i - 1 >= 0 and j + 1 <= m - 1:
			i -= 1
			j += 1
			a.append(A[i][j])
		else:
			break
	a_list.append(a)
b_list = []
for i in range(n):
	a = []
	j = 0
	a.append(B[i][j])
	while 1:
		if i - 1 >= 0 and j + 1 <= m - 1:
			i -= 1
			j += 1
			a.append(B[i][j])
		else:
			break
	b_list.append(a)
i = n - 1
for j in range(1, m):
	a = []
	i = n - 1
	a.append(B[i][j])
	while 1:
		if i - 1 >= 0 and j + 1 <= m - 1:
			i -= 1
			j += 1
			a.append(B[i][j])
		else:
			break
	b_list.append(a)
for i in range(len(a_list)):
	a = sorted(a_list[i])
	b = sorted(b_list[i])
	if a != b:
		print('NO')
		break
else:
	print('YES')


(n, m, *_) = list(map(int, input().split()))
b1 = []
for i in range(n):
	b1.append(input().split())
b2 = []
for i in range(n):
	b2.append(input().split())

def works(n, m, b1, b2):
	for diag in range(n + m - 1):
		l1 = []
		l2 = []
		for col in range(diag + 1):
			point = [col, diag - col]
			if 0 <= point[0] < n and 0 <= point[1] < m:
				l1.append(b1[point[0]][point[1]])
				l2.append(b2[point[0]][point[1]])
		if sorted(l1) != sorted(l2):
			return False
	return True
if works(n, m, b1, b2):
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
newa = [list() for _ in range(m + n)]
newb = [list() for _ in range(m + n)]
for row in range(n):
	for col in range(m):
		newa[row + col].append(a[row][col])
		newb[row + col].append(b[row][col])
for i in newa:
	i.sort()
for i in newb:
	i.sort()
print('YES' if newb == newa else 'NO')


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest, heapify, heappop, heappush, heapreplace
from math import ceil, floor, log, log2, sqrt, gcd, factorial, pow, pi
from bisect import bisect_left, bisect_right

def binNumber(n, size=4):
	return bin(n)[2:].zfill(size)

def iar():
	return list(map(int, input().split()))

def ini():
	return int(input())

def isp():
	return map(int, input().split())

def sti():
	return str(input())

def par(a):
	print(' '.join(list(map(str, a))))

def tdl(outerListSize, innerListSize, defaultValue=0):
	return [[defaultValue] * innerListSize for i in range(outerListSize)]

def sts(s):
	s = list(s)
	s.sort()
	return ''.join(s)

def bis(a, x):
	i = bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return [i, True]
	else:
		return [-1, False]

class pair:

	def __init__(self, f, s):
		self.fi = f
		self.se = s

	def __lt__(self, other):
		return (self.fi, self.se) < (other.fi, other.se)
(n, m) = isp()
a = []
b = []
c1 = Counter()
c2 = Counter()
for i in range(n):
	a.append(iar())
	c1.update(a[i])
for i in range(n):
	b.append(iar())
	c2.update(b[i])
for i in range(n):
	for j in range(m):
		if c1[a[i][j]] != c2[a[i][j]]:
			print('NO')
			quit()
p1 = tdl(n + m, 1)
p2 = tdl(n + m, 1)
for i in range(n):
	for j in range(m):
		p1[i + j].append(a[i][j])
		p2[i + j].append(b[i][j])
for i in range(n + m):
	p1[i].sort()
	p2[i].sort()
for i in range(n + m):
	if p1[i] != p2[i]:
		print('NO')
		quit()
print('YES')


from collections import defaultdict
(n, m) = [int(r) for r in input().split()]
first_matrix = [[int(z) for z in input().split()] for _ in range(n)]
second_matrix = [[int(z) for z in input().split()] for _ in range(n)]
elements_1 = defaultdict(list)
elements_2 = defaultdict(list)
for i in range(n):
	for j in range(m):
		elements_1[i + j].append(first_matrix[i][j])
		elements_2[i + j].append(second_matrix[i][j])
printed = False
for i in range(n + m - 1):
	if sorted(elements_1[i]) != sorted(elements_2[i]):
		print('NO')
		printed = True
		break
if not printed:
	print('YES')


def readLine():
	a = input()
	return [int(i) for i in a.split(' ')]
nm = readLine()
(n, m) = (nm[0], nm[1])
a = [readLine() for _ in range(n)]
b = [readLine() for _ in range(n)]
ok = True
for i in range(n):
	(k, j) = (i, 0)
	(l1, l2) = ([], [])
	while k >= 0 and j < m:
		l1.append(a[k][j])
		l2.append(b[k][j])
		k = k - 1
		j = j + 1
	l1.sort()
	l2.sort()
	for (i, _) in enumerate(l1):
		if l1[i] != l2[i]:
			ok = False
for i in range(m):
	(k, j) = (n - 1, i)
	(l1, l2) = ([], [])
	while k >= 0 and j < m:
		l1.append(a[k][j])
		l2.append(b[k][j])
		k = k - 1
		j = j + 1
	l1.sort()
	l2.sort()
	for (i, _) in enumerate(l1):
		if l1[i] != l2[i]:
			ok = False
if ok:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
A = []
B = []
for a in range(n):
	A.append(list(map(int, input().split())))
for _ in range(n):
	B.append(list(map(int, input().split())))
for c in range(n + m):
	rinda = 0 if c < m else c - m
	kolona = c if c < m else m - 1
	Ad = []
	Bd = []
	while kolona >= 0 and rinda < n:
		Ad.append(A[rinda][kolona])
		Bd.append(B[rinda][kolona])
		rinda += 1
		kolona -= 1
	Ad.sort()
	Bd.sort()
	if Ad != Bd:
		print('NO')
		exit(0)
print('YES')


from sys import stdin
(n, m) = map(int, stdin.readline().strip().split())
mt1 = []
for i in range(n):
	mt1.append(list(map(int, stdin.readline().strip().split())))
mt2 = []
for i in range(n):
	mt2.append(list(map(int, stdin.readline().strip().split())))
t = True
for i in range(n):
	x = i
	y = 0
	s = []
	s1 = []
	while x >= 0 and y < m:
		s.append(mt1[x][y])
		s1.append(mt2[x][y])
		x -= 1
		y += 1
	s.sort()
	s1.sort()
	if s != s1:
		t = False
for i in range(1, m):
	y = i
	x = n - 1
	s = []
	s1 = []
	while y < m and x >= 0:
		s.append(mt1[x][y])
		s1.append(mt2[x][y])
		x -= 1
		y += 1
	s.sort()
	s1.sort()
	if s != s1:
		t = False
if t:
	print('YES')
else:
	print('NO')


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))
(n, m) = mints()
z = [None, None]
for k in range(2):
	x = [[] for i in range(n + m)]
	for i in range(n):
		a = list(mints())
		for j in range(m):
			x[i + j].append(a[j])
	z[k] = x
for i in range(n + m):
	a = z[0][i]
	b = z[1][i]
	a.sort()
	b.sort()
	if a != b:
		print('NO')
		return
print('YES')


(a, b) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(a)]
B = [list(map(int, input().split())) for _ in range(a)]
for i in range(a):
	A[i] += [0] * (b + a)
	B[i] += [0] * (b + a)
for i in range(a - 1):
	a1 = {}
	b1 = {}
	for j in range(i + 1):
		a1[A[j][i - j]] = a1.get(A[j][i - j], 0) + 1
		b1[B[j][i - j]] = b1.get(B[j][i - j], 0) + 1
	if a1 != b1:
		print('NO')
		exit(0)
for i in range(b):
	a1 = {}
	b1 = {}
	for j in range(a):
		a1[A[j][a + i - j - 1]] = a1.get(A[j][a + i - j - 1], 0) + 1
		b1[B[j][a + i - j - 1]] = b1.get(B[j][a + i - j - 1], 0) + 1
	if a1 != b1:
		print('NO')
		exit(0)
print('YES')


import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def solve():
	(n, m) = num_input()
	a = []
	for i in range(n):
		a.append(list_input())
	b = []
	for i in range(n):
		b.append(list_input())
	for I in range(n):
		temp_a = []
		temp_b = []
		j = 0
		i = I
		while j != m and i >= 0:
			temp_a.append(a[i][j])
			temp_b.append(b[i][j])
			j += 1
			i -= 1
		temp_b.sort()
		temp_a.sort()
		if temp_a != temp_b:
			print('NO')
			return
	for J in range(1, m):
		i = n - 1
		j = J
		temp_a = []
		temp_b = []
		while j != m and i >= 0:
			temp_a.append(a[i][j])
			temp_b.append(b[i][j])
			j += 1
			i -= 1
		temp_b.sort()
		temp_a.sort()
		if temp_a != temp_b:
			print('NO')
			return
	print('YES')
t = 1
for _ in range(t):
	solve()


def ria():
	return [int(i) for i in input().split()]
(h, w) = ria()
mt1 = []
mt2 = []
for y in range(h):
	mt1.append(ria())
for y in range(h):
	mt2.append(ria())
for x in range(w):
	st1 = []
	st2 = []
	y = 0
	while w > x >= 0 and h > y >= 0:
		st1.append(mt1[y][x])
		st2.append(mt2[y][x])
		x -= 1
		y += 1
	st1.sort()
	st2.sort()
	if st1 != st2:
		print('NO')
		exit(0)
for y in range(h):
	st1 = []
	st2 = []
	x = w - 1
	while w > x >= 0 and h > y >= 0:
		st1.append(mt1[y][x])
		st2.append(mt2[y][x])
		x -= 1
		y += 1
	st1.sort()
	st2.sort()
	if st1 != st2:
		print('NO')
		exit(0)
print('YES')


(n, m) = list(map(int, input().split()))
m1 = [list(map(int, input().split())) for i in range(n)]
m2 = [list(map(int, input().split())) for i in range(n)]
import sys
for j in range(m + n - 1):
	x1 = {}
	x2 = {}
	for i in range(j + 1):
		if j - i < n and i < m:
			x1[m1[j - i][i]] = 0
			x2[m2[j - i][i]] = 0
	for i in range(j + 1):
		if j - i < n and i < m:
			x1[m1[j - i][i]] += 1
			x2[m2[j - i][i]] += 1
	if x1 != x2:
		print('NO')
		return
print('YES')


(n, m) = map(int, input().split(' '))
ma = []
mb = []
for i in range(n):
	li = list(map(int, input().split(' ', m)[:m]))
	ma.append(li)
for i in range(n):
	li = list(map(int, input().split(' ', m)[:m]))
	mb.append(li)
lia = []
lib = []
(i, j) = (0, 0)
kl = 0
ad = 0
for l in range(n + m - 1):
	li = []
	while i >= 0 and j < m:
		li.append(ma[i][j])
		i -= 1
		j += 1
	lia.append(li)
	if kl == n - 1:
		i = n - 1
		ad += 1
		j = ad
	else:
		kl += 1
		i = kl
		j = 0
(i, j) = (0, 0)
kl = 0
ad = 0
for l in range(n + m - 1):
	li = []
	while i >= 0 and j < m:
		li.append(mb[i][j])
		i -= 1
		j += 1
	lib.append(li)
	if kl == n - 1:
		i = n - 1
		ad += 1
		j = ad
	else:
		kl += 1
		i = kl
		j = 0
fl = 0
for i in range(len(lia)):
	lia[i].sort()
	lib[i].sort()
	if lia[i] != lib[i]:
		fl = 1
		break
if fl == 0:
	print('YES')
else:
	print('NO')


(n, m) = list(map(int, input().split()))
A = []
B = []
for i in range(n):
	A.append(list(map(int, input().split())))
for i in range(n):
	B.append(list(map(int, input().split())))
dA = []
dB = []
for d in range(m):
	for i in range(d, -1, -1):
		if d - i >= n:
			break
		dA.append(A[d - i][i])
		dB.append(B[d - i][i])
	if sorted(dA) != sorted(dB):
		print('NO')
		exit()
	(dA, dB) = ([], [])
for d in range(1, n):
	for i in range(d, n):
		if i - d >= m:
			break
		dA.append(A[i][m - (i - d) - 1])
		dB.append(B[i][m - (i - d) - 1])
	if sorted(dA) != sorted(dB):
		print('NO')
		exit()
	(dA, dB) = ([], [])
print('YES')


(N, M) = list(map(int, input().split()))
A = [[int(a) for a in input().split()] for _ in range(N)]
B = [[int(a) for a in input().split()] for _ in range(N)]
AA = [[] for _ in range(N + M - 1)]
BB = [[] for _ in range(N + M - 1)]
for n in range(N):
	for m in range(M):
		AA[n + m].append(A[n][m])
		BB[n + m].append(B[n][m])
for i in range(N + M - 1):
	AA[i] = sorted(AA[i])
	BB[i] = sorted(BB[i])
	if AA[i] != BB[i]:
		print('NO')
		break
else:
	print('YES')


(n, m) = list(map(int, input().split()))
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = []
for i in range(n):
	b.append(list(map(int, input().split())))
ok = 'YES'
for i in range(m - 1 + n - 1 + 1):
	d = dict()
	for x in range(min(i + 1, m)):
		if i - x < n:
			if a[i - x][x] in d:
				d[a[i - x][x]] += 1
			else:
				d[a[i - x][x]] = 1
	for x in range(min(i + 1, m)):
		if i - x < n:
			if b[i - x][x] in d:
				d[b[i - x][x]] -= 1
				if d[b[i - x][x]] == 0:
					d.__delitem__(b[i - x][x])
			else:
				print('NO')
				return
print('YES')


from collections import defaultdict

def rli():
	return list(map(int, input().split()))

def main():
	(n, m) = rli()
	cnt = [defaultdict(int) for i in range(n + m + 10)]
	for i in range(n):
		nums = rli()
		for j in range(m):
			cnt[i + j][nums[j]] += 1
	for i in range(n):
		nums = rli()
		for j in range(m):
			cnt[i + j][nums[j]] -= 1
	for i in range(n + m + 10):
		for v in cnt[i].values():
			if v != 0:
				print('NO')
				return
	print('YES')
main()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m) = [int(i) for i in input().split()]
a = [[] for i in range(n + m + 1)]
b = [[] for i in range(n + m + 1)]
s = []
st = []
for i in range(n):
	p = [int(i) for i in input().split()]
	s.append(p)
for i in range(n):
	p = [int(i) for i in input().split()]
	st.append(p)
for i in range(n):
	for j in range(m):
		a[i + j].append(s[i][j])
		b[i + j].append(st[i][j])
l = 1
for i in range(len(a)):
	a[i].sort()
	b[i].sort()
	if a[i] != b[i]:
		l = 0
		print('No')
		break
if l == 1:
	print('Yes')


import heapq
import sys
from collections import defaultdict, Counter
from functools import reduce
(n, m) = list(map(int, input().split()))
A = []
B = []
for i in range(n):
	A.append(list(map(int, input().split())))
for i in range(n):
	B.append(list(map(int, input().split())))
answer = True
for r in range(n + m):
	s1 = Counter()
	s2 = Counter()
	for i in range(min(r + 1, n)):
		j = r - i
		if j < m:
			s1[A[i][j]] += 1
			s2[B[i][j]] += 1
	answer &= s1 == s2
if answer:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
i = 0
k = []
while i < n:
	s = list(map(int, input().split()))
	i += 1
	k += [s]
i = 0
x = []
while i < n:
	g = []
	j = 0
	p = i
	while p > -1 and j > -1 and (p < n) and (j < m):
		g += [k[p][j]]
		j += 1
		p -= 1
	g.sort()
	x += [g]
	i += 1
i = n - 1
j = 1
while j < m:
	p = i
	q = j
	g = []
	while p >= 0 and p < n and (q >= 0) and (q < m):
		g += [k[p][q]]
		q += 1
		p -= 1
	g.sort()
	x += [g]
	j += 1
y = []
i = 0
k = []
while i < n:
	s = list(map(int, input().split()))
	i += 1
	k += [s]
i = 0
while i < n:
	g = []
	j = 0
	p = i
	while p > -1 and j > -1 and (p < n) and (j < m):
		g += [k[p][j]]
		j += 1
		p -= 1
	g.sort()
	y += [g]
	i += 1
i = n - 1
j = 1
while j < m:
	p = i
	q = j
	g = []
	while p >= 0 and p < n and (q >= 0) and (q < m):
		g += [k[p][q]]
		q += 1
		p -= 1
	g.sort()
	y += [g]
	j += 1
if x == y:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
mat1 = [0] * n
mat2 = [0] * n
for i in range(n):
	mat1[i] = input().split()
for i in range(n):
	mat2[i] = input().split()
ans = 'YES'
for i in range(n + m - 1):
	if i < min(n - 1, m - 1):
		lst1 = [0] * (i + 1)
		lst2 = [0] * (i + 1)
		for (j, k) in enumerate(range(i + 1)):
			lst1[j] = mat1[min(i, n - 1) - k][i - min(i, n - 1) + k]
			lst2[j] = mat2[min(i, n - 1) - k][i - min(i, n - 1) + k]
	elif min(n - 1, m - 1) <= i <= max(n - 1, m - 1):
		lst1 = [0] * min(n, m)
		lst2 = [0] * min(n, m)
		for (j, k) in enumerate(range(min(n, m))):
			lst1[j] = mat1[min(i, n - 1) - k][i - min(i, n - 1) + k]
			lst2[j] = mat2[min(i, n - 1) - k][i - min(i, n - 1) + k]
	elif max(n - 1, m - 1) < i:
		lst1 = [0] * (min(n, m) - (i - max(n, m) + 1))
		lst2 = [0] * (min(n, m) - (i - max(n, m) + 1))
		for (j, k) in enumerate(range(min(n, m) - (i - max(n, m) + 1))):
			lst1[j] = mat1[min(i, n - 1) - k][i - min(i, n - 1) + k]
			lst2[j] = mat2[min(i, n - 1) - k][i - min(i, n - 1) + k]
	lst1.sort()
	lst2.sort()
	if lst1 != lst2:
		ans = 'NO'
		break
print(ans)


cnt = lambda s, x: s.count(x)
ii = lambda : int(input())
si = lambda : input()
f = lambda : map(int, input().split())
dgl = lambda : list(map(int, input()))
il = lambda : list(map(int, input().split()))
(n, m) = f()
(m1, m2) = ([], [])
(lm1, lm2) = ([[] for _ in range(n + m)], [[] for _ in range(n + m)])
for _ in range(n):
	m1.append(il())
for _ in range(n):
	m2.append(il())
for i in range(n):
	for j in range(m):
		lm1[i + j].append(m1[i][j])
		lm2[i + j].append(m2[i][j])
for i in range(n + m):
	if sorted(lm1[i]) != sorted(lm2[i]):
		exit(print('NO'))
print('YES')


a = []
(n, m) = map(int, input().split())
for i in range(n):
	a.append(input().split())
b = []
for i in range(n):
	b.append(input().split())
for k in range(m):
	i = k
	j = 0
	c = []
	d = []
	while i >= 0 and j <= n - 1:
		c.append(a[j][i])
		d.append(b[j][i])
		i = i - 1
		j = j + 1
	c = sorted(c)
	d = sorted(d)
	if c != d:
		print('No')
		exit(0)
for k in range(1, n):
	j = k
	i = m - 1
	c = []
	d = []
	while i >= 0 and j <= n - 1:
		c.append(a[j][i])
		d.append(b[j][i])
		i = i - 1
		j = j + 1
	c = sorted(c)
	d = sorted(d)
	if c != d:
		print('No')
		exit(0)
print('Yes')


(n, m) = map(int, input().split())
a = []
b = []
test = []
test_ = []
for i in range(n + m):
	test.append([0])
	test_.append([0])
for i in range(n):
	cache = list(map(int, input().split()))
	a.append(cache)
for i in range(n):
	cache = list(map(int, input().split()))
	b.append(cache)
for i in range(n):
	for j in range(m):
		test[i + j].append(a[i][j])
		test_[i + j].append(b[i][j])
k = 0
for i in range(n + m - 1):
	test[i].sort()
	test_[i].sort()
for i in range(n + m - 1):
	if test[i] == test_[i]:
		k += 1
if k == n + m - 1:
	print('YES')
else:
	print('NO')


import sys
ss = sys.stdin.readline
(n, m) = map(int, ss().split())
a = [[i for i in map(int, ss().split())] for i in range(n)]
b = [[i for i in map(int, ss().split())] for i in range(n)]
ans = 1
op = 1
for j in range(m):
	da = {}
	db = {}
	i = 0
	while j >= 0 and i < n:
		da[a[i][j]] = da.get(a[i][j], 0) + 1
		db[b[i][j]] = db.get(b[i][j], 0) + 1
		j -= 1
		i += 1
	if da != db:
		ans = 0
	if not ans:
		break
for i in range(1, n):
	da = {}
	db = {}
	j = m - 1
	while j >= 0 and i < n:
		da[a[i][j]] = da.get(a[i][j], 0) + 1
		db[b[i][j]] = db.get(b[i][j], 0) + 1
		j -= 1
		i += 1
	if da != db:
		ans = 0
	if not ans:
		break
if ans:
	print('YES')
else:
	print('NO')


EMPTY = 0
WHITE = 1
BLACK = 8
BOARDWIDTH = 7
BOARDHEIGHT = 6

def isWinner(board, tile):
	for x in range(BOARDWIDTH - 3):
		for y in range(3, BOARDHEIGHT):
			if board[x][y] == tile and board[x + 1][y - 1] == tile and (board[x + 2][y - 2] == tile) and (board[x + 3][y - 3] == tile):
				return True

def printDiagonal(num):
	aret = []
	bret = []
	y = d if d < M else M - 1
	x = 0 if d < M else d - M
	while x < N and y >= 0:
		aret.append(A[x][y])
		bret.append(B[x][y])
		y -= 1
		x += 1
	aret.sort()
	bret.sort()
	if aret == bret:
		return True
	else:
		return False
(N, M) = map(int, input().split())
A = []
for n in range(N):
	A.append(list(map(int, input().split())))
B = []
for n in range(N):
	B.append(list(map(int, input().split())))
for d in range(N + M):
	if not printDiagonal(d):
		print('NO')
		exit(0)
print('YES')


import sys
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
d = []
for i in range(m + n - 1):
	c = []
	for j in range(n):
		if 0 <= i - j < m:
			c.append(a[j][i - j])
	c.sort()
	d.append(c)
e = []
for i in range(m + n - 1):
	c = []
	for j in range(n):
		if 0 <= i - j < m:
			c.append(b[j][i - j])
	c.sort()
	e.append(c)
f = 0
for i in range(len(d)):
	if d[i] != e[i]:
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


(n, m) = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
(l1, l2) = ([[] for i in range(n + m)], [[] for i in range(n + m)])
for i in range(n):
	for j in range(m):
		l1[i + j].append(a[i][j])
		l2[i + j].append(b[i][j])
for i in l1:
	i.sort()
for i in l2:
	i.sort()
print('YES' if l1 == l2 else 'NO')


from collections import defaultdict
(n, m) = (int(t) for t in input().split(' '))
m1 = [[int(t) for t in input().split(' ')] for _ in range(n)]
m2 = [[int(t) for t in input().split(' ')] for _ in range(n)]

def get_csum(mx):
	csum = [defaultdict(int) for _ in range(n + m + 1)]
	for i in range(n):
		for j in range(m):
			csum[i + j][mx[i][j]] += 1
	return csum
csum1 = get_csum(m1)
csum2 = get_csum(m2)
print('YES' if csum1 == csum2 else 'NO')


import sys
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
a = [None] * n
for i in range(n):
	a[i] = input().split()
b = [None] * n
for i in range(n):
	b[i] = input().split()
flag = True
mi = min(m, n)
ma = max(m, n)
for i in range(mi):
	da = {}
	db = {}
	ax = [a[j][i - j] for j in range(i + 1)]
	bx = [b[j][i - j] for j in range(i + 1)]
	for x in ax:
		if x in da:
			da[x] += 1
		else:
			da[x] = 1
	for x in bx:
		if x in db:
			db[x] += 1
		else:
			db[x] = 1
	if da != db:
		flag = False
		break
if flag:
	for i in range(mi, mi + ma - 1):
		da = {}
		db = {}
		if m > n:
			ax = [a[j][i - j] for j in range(max(i + 1 - ma, 0), mi)]
			bx = [b[j][i - j] for j in range(max(i + 1 - ma, 0), mi)]
		else:
			ax = [a[i - j][j] for j in range(max(i + 1 - ma, 0), mi)]
			bx = [b[i - j][j] for j in range(max(i + 1 - ma, 0), mi)]
		for x in ax:
			if x in da:
				da[x] += 1
			else:
				da[x] = 1
		for x in bx:
			if x in db:
				db[x] += 1
			else:
				db[x] = 1
		if da != db:
			flag = False
			break
if flag:
	print('YES')
else:
	print('NO')


(N, M) = map(int, input().split())
(A, B) = ([[] for _ in range(1000)], [[] for _ in range(1000)])
for i in range(N):
	L = list(map(int, input().split()))
	for j in range(M):
		A[i + j].append(L[j])
for i in range(N):
	L = list(map(int, input().split()))
	for j in range(M):
		B[i + j].append(L[j])
for i in range(N + M):
	A[i].sort()
	B[i].sort()
	if A[i] != B[i]:
		print('NO')
		exit()
print('YES')


(n, m) = map(int, input().split())
d1 = [[] for t in range(n + m)]
d2 = [[] for t in range(m + n)]
for t in range(n):
	a = list(map(int, input().split()))
	for i in range(m):
		d1[t + i].append(a[i])
for t in range(n):
	a = list(map(int, input().split()))
	for i in range(m):
		d2[t + i].append(a[i])
flag = True
for t in range(n + m - 1):
	d1[t].sort()
	d2[t].sort()
	if d1[t] != d2[t]:
		print('NO')
		flag = False
		break
if flag:
	print('YES')


(n, m) = list(map(int, input().split()))
a = [0] * n
b = [0] * n
ok = 1
for i in range(n):
	a[i] = list(map(int, input().split()))
for i in range(n):
	b[i] = list(map(int, input().split()))
for i in range(m):
	tmp1 = []
	tmp2 = []
	deph = 0
	it = i
	while deph < n and it >= 0:
		tmp1.append(a[deph][it])
		tmp2.append(b[deph][it])
		deph += 1
		it -= 1
	tmp1.sort()
	tmp2.sort()
	if tmp1 != tmp2:
		ok = 0
for i in range(1, n):
	tmp1 = []
	tmp2 = []
	deph = i
	it = m - 1
	while deph < n and it >= 0:
		tmp1.append(a[deph][it])
		tmp2.append(b[deph][it])
		deph += 1
		it -= 1
	tmp1.sort()
	tmp2.sort()
	if tmp1 != tmp2:
		ok = 0
if ok:
	print('YES')
else:
	print('NO')


[n, m] = input().split(' ')
n = int(n)
m = int(m)
matrixA = []
matrixB = []
for i in range(n):
	matrixA.append(list(map(int, input().split(' '))))
for i in range(n):
	matrixB.append(list(map(int, input().split(' '))))

def buildDiagonals(matrix):
	r = [None] * (n + m - 1)
	for i in range(m):
		for j in range(n):
			if not r[i + j]:
				r[i + j] = []
			r[i + j].append(matrix[j][i])
	for x in range(len(r)):
		r[x].sort()
	return r
diagonalsA = buildDiagonals(matrixA)
diagonalsB = buildDiagonals(matrixB)
if str(diagonalsA) == str(diagonalsB):
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
s = max(m, n)
if n < s:
	for i in range(s - n):
		a.append([0] * s)
		b.append([0] * s)
if m < s:
	for i in range(s):
		a[i] += [0] * (s - m)
		b[i] += [0] * (s - m)
for i in range(s):
	al = []
	bl = []
	c = 0
	while c <= i:
		al.append(a[c][i - c])
		bl.append(b[c][i - c])
		c += 1
	if sorted(al) != sorted(bl):
		print('NO')
		break
	c = 0
	ar = []
	br = []
	while c < s - i:
		ar.append(a[s - 1 - c][i + c])
		br.append(b[s - 1 - c][i + c])
		c += 1
	if sorted(ar) != sorted(br):
		print('NO')
		break
else:
	print('YES')


(n, m) = map(int, input().split())
a_sets = {}
b_sets = {}
for i in range(n):
	ll = map(int, input().split())
	for (j, x) in enumerate(ll):
		a_sets.setdefault(i + j, []).append(x)
for i in range(n):
	ll = map(int, input().split())
	for (j, x) in enumerate(ll):
		b_sets.setdefault(i + j, []).append(x)
for k in a_sets:
	a_k_line = sorted(a_sets[k])
	b_k_line = sorted(b_sets[k])
	if a_k_line != b_k_line:
		print('NO')
		exit(0)
print('YES')


from collections import defaultdict
d1 = defaultdict(list)
d2 = defaultdict(list)
(n, m) = map(int, input().split())
for i in range(n):
	l = list(map(int, input().split()))
	for j in range(m):
		d1[i + j].append(l[j])
for i in range(n):
	l = list(map(int, input().split()))
	for j in range(m):
		d2[i + j].append(l[j])
f = 0
for i in range(1010):
	d1[i].sort()
	d2[i].sort()
	if d1[i] != d2[i]:
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


from collections import defaultdict

def main():
	(n, m) = list(map(int, input().split()))
	a = [list(map(int, input().split())) for _ in range(n)]
	b = [list(map(int, input().split())) for _ in range(n)]
	dict1 = defaultdict(list)
	for i in range(n):
		for j in range(m):
			dict1[i + j].append(a[i][j])
	dict2 = defaultdict(list)
	for i in range(n):
		for j in range(m):
			dict2[i + j].append(b[i][j])
	for i in range(n + m - 1):
		if sorted(dict1[i]) != sorted(dict2[i]):
			print('NO')
			return
	print('YES')

def __starting_point():
	main()
__starting_point()


from sys import stdin
from collections import Counter
(n, m) = map(int, stdin.readline().split())
A = []
for i in range(n):
	a = list(map(int, stdin.readline().split()))
	A.append(a)
B = []
for i in range(n):
	b = list(map(int, stdin.readline().split()))
	B.append(b)
ROW = n
COL = m

def diagonalOrder(matrix):
	final = []
	for line in range(1, ROW + COL):
		ans = []
		start_col = max(0, line - ROW)
		count = min(line, COL - start_col, ROW)
		for j in range(0, count):
			ans.append(matrix[min(ROW, line) - j - 1][start_col + j])
		final.append(ans)
	return final
x = diagonalOrder(A)
y = diagonalOrder(B)
cond = True
for i in range(len(x)):
	if Counter(x[i]) == Counter(y[i]):
		pass
	else:
		cond = False
		break
if cond == True:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
arr1 = []
for i in range(n):
	temp = list(map(int, input().split()))
	arr1.append(temp)
arr2 = []
for i in range(n):
	temp = list(map(int, input().split()))
	arr2.append(temp)
flag = 0
for i in range(n):
	temparr1 = []
	temparr2 = []
	indexi = i
	indexj = 0
	while indexi >= 0 and indexj < m:
		temparr1.append(arr1[indexi][indexj])
		temparr2.append(arr2[indexi][indexj])
		indexi -= 1
		indexj += 1
	temparr1.sort()
	temparr2.sort()
	for j in range(len(temparr1)):
		if temparr1[j] != temparr2[j]:
			flag = 1
			break
if flag == 1:
	print('NO')
else:
	for i in range(1, m):
		temparr1 = []
		temparr2 = []
		indexi = n - 1
		indexj = i
		while indexi >= 0 and indexj < m:
			temparr1.append(arr1[indexi][indexj])
			temparr2.append(arr2[indexi][indexj])
			indexi -= 1
			indexj += 1
		temparr1.sort()
		temparr2.sort()
		for j in range(len(temparr1)):
			if temparr1[j] != temparr2[j]:
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


import sys
import collections
from math import floor, ceil, log2
inp = lambda : sys.stdin.readline().strip()
(n, m) = map(int, inp().split())
a = [[0] * m for i in range(n)]
b = [[0] * m for i in range(n)]
for i in range(n):
	s = list(map(int, inp().split()))
	for j in range(m):
		a[i][j] = s[j]
for i in range(n):
	s = list(map(int, inp().split()))
	for j in range(m):
		b[i][j] = s[j]
(x, y) = (0, 0)
for k in range(n + m - 1):
	(i, j) = (x, y)
	(t1, t2) = ([], [])
	while i + j == k:
		if i < 0 or j >= m:
			break
		t1.append(a[i][j])
		t2.append(b[i][j])
		i -= 1
		j += 1
	t1.sort()
	t2.sort()
	ok = False
	for i in range(len(t1)):
		if t1[i] != t2[i]:
			ok = True
			print('NO')
			sys.exit(0)
	if x < n - 1:
		x += 1
	else:
		y += 1
print('YES')


tokens = input().split()
rows = int(tokens[0])
columns = int(tokens[1])
A = []
B = []
for i in range(rows):
	tokens = input().split()
	tokens = [int(w) for w in tokens]
	A.append(tokens)
for i in range(rows):
	tokens = input().split()
	tokens = [int(w) for w in tokens]
	B.append(tokens)
status = True
for count in range(rows):
	i = count
	j = 0
	a = []
	b = []
	while i >= 0 and j < columns:
		a.append(A[i][j])
		b.append(B[i][j])
		i -= 1
		j += 1
	a.sort()
	b.sort()
	for i in range(len(a)):
		if a[i] != b[i]:
			status = False
			break
for count in range(columns):
	i = rows - 1
	j = count
	a = []
	b = []
	while i >= 0 and j < columns:
		a.append(A[i][j])
		b.append(B[i][j])
		i -= 1
		j += 1
	a.sort()
	b.sort()
	for i in range(len(a)):
		if a[i] != b[i]:
			status = False
			break
if status:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	a1 = [int(x) for x in input().split()]
	a.append(a1)
for i in range(n):
	a1 = [int(x) for x in input().split()]
	b.append(a1)
ad = [[] for x in range(n * m)]
bd = [[] for x in range(n * m)]
for i in range(n):
	for j in range(m):
		ad[i + j].append(a[i][j])
for i in range(n):
	for j in range(m):
		bd[i + j].append(b[i][j])
for i in range(len(ad)):
	x = sorted(ad[i])
	y = sorted(bd[i])
	if x != y:
		print('NO')
		exit()
print('YES')


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(n, m) = I()
a = [I() for i in range(n)]
b = [I() for i in range(n)]
x = [[] for i in range(n + m - 1)]
y = [[] for i in range(n + m - 1)]
for i in range(n):
	for j in range(m):
		x[i + j].append(a[i][j])
		y[i + j].append(b[i][j])
for i in range(n + m - 1):
	if sorted(x[i]) != sorted(y[i]):
		print('NO')
		sys.exit()
print('YES')


from collections import Counter
(n, m) = (int(x) for x in input().split())
M1 = [[int(x) for x in input().split()] for _ in range(n)]
M2 = [[int(x) for x in input().split()] for _ in range(n)]
m1_diag___nums = [[] for x in range(n + m - 1)]
m2_diag___nums = [[] for x in range(n + m - 1)]
for r in range(n):
	for c in range(m):
		m1_diag___nums[r + c].append(M1[r][c])
		m2_diag___nums[r + c].append(M2[r][c])
for diag in range(n + m - 1):
	if Counter(m1_diag___nums[diag]) != Counter(m2_diag___nums[diag]):
		print('NO')
		quit()
print('YES')


(n, m) = map(int, input().split())

def mtr(r):
	for _ in range(r):
		temp = map(int, input().split())
		yield list(temp)

def diag(x, t):
	temp = [[] for _ in range(1, m + n)]
	for (i, j) in list(zip(*[[i for i in range(t)], [i for i in range(t)]])):
		for k in x[j]:
			temp[i].append(k)
			i += 1
	for i in range(0, m + n - 1):
		temp[i].sort()
	return temp
a = diag(list(mtr(n)), n)
b = diag(list(mtr(n)), n)
print('YES' if a == b else 'NO')


(n, m) = list(map(int, input().split()))
(a, b) = ([], [])
for _ in range(n):
	aux = [int(x) for x in input().split()]
	a.append(aux)
for _ in range(n):
	aux = [int(x) for x in input().split()]
	b.append(aux)

def diagonalOrder(a, b):
	for line in range(1, n + m):
		start_col = max(0, line - n)
		count = min(line, m - start_col, n)
		aux_a = []
		aux_b = []
		for j in range(count):
			aux_a.append(a[min(n, line) - j - 1][start_col + j])
			aux_b.append(b[min(n, line) - j - 1][start_col + j])
		if sorted(aux_a) != sorted(aux_b):
			return 'NO'
	return 'YES'
print(diagonalOrder(a, b))


(n, m) = map(int, input().split())
a = [[] for i in range(n)]
b = [[] for i in range(n)]
va = [[] for i in range(n + m)]
vb = [[] for i in range(n + m)]
for i in range(n):
	a[i] = list(map(int, input().split()))
	for j in range(m):
		va[i + j].append(a[i][j])
for i in range(n):
	b[i] = list(map(int, input().split()))
	for j in range(m):
		vb[i + j].append(b[i][j])
for i in range(n + m - 1):
	va[i].sort()
	vb[i].sort()
	for j in range(len(va[i])):
		if va[i][j] != vb[i][j]:
			print('NO')
			exit()
print('YES')


(n, m) = list(map(int, input().split()))
A = [list(map(int, input().split())) for i in range(n)]
B = [list(map(int, input().split())) for i in range(n)]
C = [sorted([A[j][i - j] for j in range(i + 1) if 0 <= j < n and i - j < m]) for i in range(n + m - 1)]
D = [sorted([B[j][i - j] for j in range(i + 1) if 0 <= j < n and i - j < m]) for i in range(n + m - 1)]
if C == D:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
from collections import defaultdict
diag_a = defaultdict(list)
diag_b = defaultdict(list)
for x in range(n):
	for (i, val) in enumerate(list(map(int, input().split()))):
		diag_a[x + i].append(val)
for x in range(n):
	for (i, val) in enumerate(list(map(int, input().split()))):
		diag_b[x + i].append(val)
diag_a = {k: sorted(v) for (k, v) in diag_a.items()}
diag_b = {k: sorted(v) for (k, v) in diag_b.items()}
for k in diag_a.keys():
	if diag_a[k] != diag_b[k]:
		print('NO')
		return
print('YES')


(n, m) = map(int, input().split())
a = []
b = []
c = []
d = []
for i in range(n):
	c = list(map(int, input().split()))
	a.append(c)
for i in range(n):
	d = list(map(int, input().split()))
	b.append(d)
ans = 0
if ans == 0:
	for i in range(m):
		c = []
		d = []
		j = i
		k = 0
		while k < n and j >= 0:
			c.append(a[k][j])
			d.append(b[k][j])
			k += 1
			j -= 1
		c.sort()
		d.sort()
		if c == d:
			continue
		else:
			ans = 1
			break
if ans == 0:
	for i in range(1, n):
		c = []
		d = []
		j = i
		k = m - 1
		while j <= n - 1 and k >= 0:
			c.append(a[j][k])
			d.append(b[j][k])
			k -= 1
			j += 1
		c.sort()
		d.sort()
		if c == d:
			continue
		else:
			ans = 1
			break
if ans == 0:
	print('YES')
else:
	print('NO')


(n, k) = map(int, input().split())
a = [[] * k] * n
for i in range(n):
	a[i] = list(map(int, input().split()))
b = [[] * k] * n
for i in range(n):
	b[i] = list(map(int, input().split()))
flag = True
tempa = []
tempb = []
for i in range(n + k):
	tempa.clear()
	tempb.clear()
	if flag == False:
		break
	for j in range(i + 1):
		if i - j > n - 1 or j > k - 1:
			continue
		tempa.append(a[i - j][j])
		tempb.append(b[i - j][j])
	tempa.sort()
	tempb.sort()
	sz = len(tempa)
	for j in range(sz):
		if flag and tempa[j] != tempb[j]:
			flag = False
			break
if flag:
	print('YES')
else:
	print('NO')


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import defaultdict
(n, m) = map(int, input().split())
grid = [list(map(int, input().split())) for i in range(n)]
target = [list(map(int, input().split())) for i in range(n)]
d1 = defaultdict(list)
d2 = defaultdict(list)
ok = True
for i in range(n):
	for j in range(m):
		d1[i + j].append(grid[i][j])
		d2[i + j].append(target[i][j])
for i in range(n + m - 1):
	if sorted(d1[i]) != sorted(d2[i]):
		ok = False
print('YES' if ok else 'NO')


(n, m) = map(int, input().split())
(a, b) = ([], [])
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
p = [[] for _ in range(n + m)]
q = [[] for _ in range(n + m)]
for i in range(n):
	for j in range(m):
		p[i + j].append(a[i][j])
		q[i + j].append(b[i][j])
print('YES' if all((sorted(x) == sorted(y) for (x, y) in zip(p, q))) else 'NO')


(n, m) = map(int, input().split())
lst = []
lst2 = []
for c in range(n):
	lst.append(list(map(int, input().split())))
for c in range(n):
	lst2.append(list(map(int, input().split())))
for c in range(n):
	for r in range(len(lst[c])):
		p = 0
		if lst[c][r] != lst2[c][r]:
			a = c
			b = r
			while a != n and b != -1:
				if lst2[a][b] == lst[c][r]:
					p = 1
					break
				a += 1
				b -= 1
			if p == 1:
				if True:
					(lst2[a][b], lst2[c][r]) = (lst2[c][r], lst2[a][b])
			else:
				print('NO')
				exit(0)
print('YES')


(n, m) = map(int, input().split())
l1 = []
l2 = []
for i in range(n):
	ka = list(map(int, input().split()))
	l1.append(ka)
for i in range(n):
	ka = list(map(int, input().split()))
	l2.append(ka)
ba = []
ha = []
for i in range(n):
	for j in range(m):
		ha.append(l1[i][j])
		ba.append(l2[i][j])
if sorted(ha) != sorted(ba):
	print('NO')
	exit()
for i in range(n + m - 1):
	ha = []
	ba = []
	x = i
	y = 0
	while x >= 0 and y < m:
		try:
			ha.append(l1[x][y])
			ba.append(l2[x][y])
		except:
			pass
		x -= 1
		y += 1
	if sorted(ha) != sorted(ba):
		print('NO')
		exit()
print('YES')


INF = 100000000
from collections import defaultdict
t = 1
for test in range(t):
	(n, m) = map(int, input().split())
	a = []
	b = []
	for i in range(n):
		a.append(list(map(int, input().split())))
	for i in range(n):
		b.append(list(map(int, input().split())))
	ans = 'YES'
	atmp = set()
	btmp = set()
	for i in range(n + m - 1):
		atmp = defaultdict(int)
		btmp = defaultdict(int)
		for j in range(i + 1):
			if i - j < n and j < m:
				atmp[a[i - j][j]] += 1
				btmp[b[i - j][j]] += 1
		if not atmp == btmp:
			ans = 'NO'
	print(ans)


def e(x, y, d):
	while y >= 0 and x < m:
		d[p[y][x]] = d[p[y][x]] + 1 if p[y][x] in d else 1
		(d[q[y][x]], x, y) = (d[q[y][x]] - 1 if q[y][x] in d else -1, x + 1, y - 1)
	for k in d:
		if d[k] != 0:
			print('NO')
			raise SystemExit()
(n, m) = map(int, input().split())
(p, q) = ([list(map(int, input().split())) for _ in range(n)], [list(map(int, input().split())) for _ in range(n)])
for i in range(n):
	e(0, i, {})
for i in range(m):
	(a, b) = (e(i, n - 1, {}), print('YES')) if i == m - 1 else (e(i, n - 1, {}), None)


from collections import defaultdict

def are_transposed(a, b, n, m):
	diags_a = defaultdict(list)
	diags_b = defaultdict(list)
	for i in range(n):
		for j in range(m):
			diags_a[i + j].append(a[i][j])
			diags_b[i + j].append(b[i][j])
	for d in range(n + m):
		if sorted(diags_a[d]) != sorted(diags_b[d]):
			return False
	return True
(n, m) = map(int, input().split())
a = []
b = []
for _ in range(n):
	line = list(map(int, input().split()))
	a.append(line)
for _ in range(n):
	line = list(map(int, input().split()))
	b.append(line)
print('YES' if are_transposed(a, b, n, m) else 'NO')


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
from math import *
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN():
	return list(numIN())

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	st = en = be = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
			st = be
			en = i
		if meh < 0:
			meh = 0
			be = i + 1
	return (msf, st, en)

def res(ans, t):
	print('Case #{}: {}'.format(t, ans))

def divi(n):
	l = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l += [i, n // i]
	l.sort()
	return l

def lcm(a, b):
	return a * b // gcd(a, b)
(n, m) = numIN()
a = []
b = []
d1 = defaultdict(list)
d2 = defaultdict(list)
x = set()
for _ in range(n):
	a.append(arrIN())
for _ in range(n):
	b.append(arrIN())
for i in range(n):
	for j in range(m):
		d1[i + j].append(a[i][j])
		d2[i + j].append(b[i][j])
		x.add(i + j)
x = sorted(x)
for i in x:
	d1[i].sort()
	d2[i].sort()
	if d1[i] != d2[i]:
		print('NO')
		exit(0)
print('YES')


(n, m) = map(int, input().strip().split())
A = [list(map(int, input().strip().split())) for _ in range(n)]
B = [list(map(int, input().strip().split())) for _ in range(n)]
diag_a = [[] for _ in range(n + m)]
diag_b = [[] for _ in range(n + m)]
for i in range(n):
	for j in range(m):
		diag_a[i + j].append(A[i][j])
		diag_b[i + j].append(B[i][j])
ans = all((sorted(a) == sorted(b) for (a, b) in zip(diag_a, diag_b)))
print('YES' if ans else 'NO')


(n, m) = list(map(int, input().split()))
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = []
flag = True
for i in range(n):
	b.append(list(map(int, input().split())))
for i in range(m + n - 1):
	cur = []
	curb = []
	for l in range(i + 1):
		try:
			cur.append(a[i - l][l])
			curb.append(b[i - l][l])
		except:
			pass
	cur.sort()
	curb.sort()
	if cur != curb:
		flag = False
if flag:
	print('YES')
else:
	print('NO')


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def ceil(a, b):
	return (a + b - 1) // b
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
test = 0

def solve():
	(n, m) = mi()
	m1 = dd(int)
	m2 = dd(int)
	A = [[] for i in range(n + m)]
	B = [[] for i in range(n + m)]
	for i in range(n):
		x = li()
		for j in range(m):
			A[i + j].append(x[j])
	for i in range(n):
		x = li()
		for j in range(m):
			B[i + j].append(x[j])
	f = 0
	for i in range(n + m - 1):
		if sorted(A[i]) != sorted(B[i]):
			f = 1
			break
	if f:
		print('NO')
	else:
		print('YES')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
t = 1
if test:
	t = ii()
for _ in range(t):
	solve()


import itertools
import bisect
import math
from collections import *
import os
import sys
from io import BytesIO, IOBase
ii = lambda : int(input())
lmii = lambda : list(map(int, input().split()))
li = lambda : list(input())
mii = lambda : map(int, input().split())
msi = lambda : map(str, input().split())

def main():
	(n, m) = mii()
	mat1 = []
	mat2 = []
	for i in range(n):
		mat1.append(lmii())
	for i in range(n):
		mat2.append(lmii())
	d = defaultdict(list)
	dd = defaultdict(list)
	for i in range(n):
		for j in range(m):
			d[i + j].append(mat1[i][j])
	for i in range(n):
		for j in range(m):
			dd[i + j].append(mat2[i][j])
	lst = []
	lst1 = []
	for i in d:
		lst.append(sorted(d[i]))
	for i in dd:
		lst1.append(sorted(dd[i]))
	if lst == lst1:
		print('YES')
	else:
		print('NO')
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(n, m) = map(int, input().split())
arr = {}
j = 0
for i in range(n):
	s = list(map(int, input().split()))
	for k in range(m):
		y = i + k
		x = s[k]
		ky = str(x) + 'a' + str(y)
		if ky in arr:
			arr[ky] += 1
		else:
			arr[ky] = 1
j = 0
flag = 1
for i in range(n):
	s = list(map(int, input().split()))
	for k in range(m):
		y = i + k
		x = s[k]
		ky = str(x) + 'a' + str(y)
		if ky in arr:
			arr[ky] -= 1
		else:
			flag = 0
if flag == 0:
	print('NO')
	exit()
for k in arr:
	if arr[k] != 0:
		print('NO')
		exit()
print('YES')


import math
(n, m) = [int(x) for x in input().split()]
mat1 = []
for i in range(n):
	mat1.append([int(x) for x in input().split()])
mat2 = []
for i in range(n):
	mat2.append([int(x) for x in input().split()])
sum1 = 0
sum2 = 0
for i in range(n):
	sum1 = sum1 + sum(mat1[i])
	sum2 = sum2 + sum(mat2[i])
if sum1 != sum2:
	print('NO')
else:
	count = 1
	for s in range(0, n + m - 1):
		check = 0
		temp1 = 0
		temp2 = 0
		for i in range(0, n):
			j = s - i
			if j > m - 1 or j < 0:
				continue
			else:
				check = check ^ mat1[i][j] ^ mat2[i][j]
				temp1 = max(temp1, mat1[i][j])
				temp2 = max(temp2, mat2[i][j])
		if check != 0:
			count = 0
			print('NO')
			break
		elif temp1 != temp2:
			count = 0
			print('NO')
			break
	if count == 1:
		print('YES')


from sys import stdin, stdout

def inp():
	return int(stdin.readline())

def minp():
	return map(int, stdin.readline().rstrip().split())

def linp():
	return list(minp())

def main():
	(n, m) = minp()
	a = []
	b = []
	for i in range(n):
		l = linp()
		a.append(l)
	for i in range(n):
		l = linp()
		b.append(l)
	p = [[] for i in range(n + m)]
	q = [[] for j in range(n + m)]
	f = 1
	for i in range(n):
		for j in range(m):
			p[i + j].append(a[i][j])
			q[i + j].append(b[i][j])
	for i in range(n + m):
		if sorted(p[i]) == sorted(q[i]):
			pass
		else:
			print('NO')
			f = 0
			break
	if f == 1:
		print('YES')
main()


(n, m) = map(int, input().split())
mat1 = [[0 for i in range(m)] for j in range(n)]
mat2 = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	mat1[i] = list(map(int, input().split()))
for i in range(n):
	mat2[i] = list(map(int, input().split()))
l1 = [dict() for i in range(n + m - 1)]
l2 = [dict() for i in range(n + m - 1)]
for i in range(n):
	for j in range(m):
		if mat1[i][j] in l1[i + j]:
			l1[i + j][mat1[i][j]] += 1
		else:
			l1[i + j][mat1[i][j]] = 1
		if mat2[i][j] in l2[i + j]:
			l2[i + j][mat2[i][j]] += 1
		else:
			l2[i + j][mat2[i][j]] = 1
flag = 0
for i in range(n + m - 1):
	if l1[i] != l2[i]:
		flag = 1
print(flag * 'NO' + (1 - flag) * 'YES')


(n, m) = map(int, input().split())

def check(a, b, x, y):
	i = 0
	j = 0
	while x + i < n and y - j >= 0:
		if a[x + i][y - j] == b[x][y]:
			(a[x][y], a[x + i][y - j]) = (a[x + i][y - j], a[x][y])
			return True
		i += 1
		j += 1
	return False
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = []
for i in range(n):
	b.append(list(map(int, input().split())))
for i in range(n):
	for j in range(m):
		if check(a, b, i, j) == False:
			print('NO')
			exit()
print('YES')


(n, m) = list(map(int, input().strip().split()))
a = []
b = []
for i in range(n):
	l = list(map(int, input().strip().split()))
	a.append(l)
for i in range(n):
	l = list(map(int, input().strip().split()))
	b.append(l)
test = True
for i in range(n + m - 1):
	c = []
	d = []
	if i < m - 1:
		for j in range(i, -1, -1):
			c.append(a[i - j][j])
			d.append(b[i - j][j])
			if i - j == n - 1:
				break
		c.sort()
		d.sort()
		if c != d:
			test = False
			break
	else:
		x = i - (m - 1)
		for j in range(x, n):
			c.append(a[j][m - 1 - (j - x)])
			d.append(b[j][m - 1 - (j - x)])
			if m - 1 - (j - x) == 0:
				break
		c.sort()
		d.sort()
		if c != d:
			test = False
			break
if test == True:
	print('YES')
else:
	print('NO')


(n, m) = list(map(int, input().split()))
(A, B) = ([list(map(int, input().split())) for _ in range(n)], [list(map(int, input().split())) for _ in range(n)])
_A = [sorted([A[j][i - j] for j in range(i + 1) if 0 <= j < n and i - j < m]) for i in range(n + m - 1)]
_B = [sorted([B[j][i - j] for j in range(i + 1) if 0 <= j < n and i - j < m]) for i in range(n + m - 1)]
print(['NO', 'YES'][_A == _B])


(n, m) = map(int, input().split())
a = [[] for _ in range(n + m)]
b = [[] for _ in range(n + m)]
for i in range(n):
	for (j, x) in enumerate(input().split()):
		a[i + j].append(x)
for i in range(n):
	for (j, x) in enumerate(input().split()):
		b[i + j].append(x)
for i in range(n + m):
	a[i].sort()
	b[i].sort()
	if a[i] != b[i]:
		print('NO')
		quit()
print('YES')


(n, m) = map(int, input().split())
a = []
b = []
d = dict()
td = dict()
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
for i in range(n):
	for j in range(m):
		idx = i + j
		try:
			d[idx].append(a[i][j])
		except KeyError:
			d[idx] = [a[i][j]]
		try:
			td[idx].append(b[i][j])
		except KeyError:
			td[idx] = [b[i][j]]
for i in d:
	d[i].sort()
	td[i].sort()
	if d[i] != td[i]:
		print('NO')
		exit(0)
print('YES')


import sys, math
input = sys.stdin.readline

def inp():
	n = int(input())
	return n

def inlt():
	a = list(map(int, input().split()))
	return a

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	(a, b) = map(int, input().split())
	return (a, b)
t = 1
for tt in range(t):
	(n, m) = invr()
	a = [inlt() for i in range(n)]
	b = [inlt() for i in range(n)]
	q = [[] for i in range(n + m + 1)]
	w = [[] for i in range(n + m + 1)]
	for i in range(n):
		for j in range(m):
			q[i + j].append(a[i][j])
			w[i + j].append(b[i][j])
	for i in range(n + m + 1):
		if sorted(q[i]) != sorted(w[i]):
			print('NO')
			sys.exit()
	print('YES')


(n, m) = (0, 0)
(a, b) = ([], [])

def get_diags(a):
	diags = []
	for c in range(len(a[0])):
		(x, y) = (0, c)
		diags.append([])
		while x < n and y >= 0:
			diags[-1].append(a[x][y])
			x += 1
			y -= 1
	for r in range(len(a)):
		(x, y) = (r, m - 1)
		diags.append([])
		while x < n and y >= 0:
			diags[-1].append(a[x][y])
			x += 1
			y -= 1
	return diags

def solve(a, b):
	if n == 1 or m == 1:
		return a == b
	a_diags = get_diags(a)
	b_diags = get_diags(b)
	a_diags = [sorted(r) for r in a_diags]
	b_diags = [sorted(r) for r in b_diags]
	for i in range(len(a_diags)):
		if a_diags[i] != b_diags[i]:
			return False
	return True

def _input():
	global n, m
	(n, m) = map(int, input().split())
	for _ in range(n):
		a.append(list(map(int, input().split())))
	for _ in range(n):
		b.append(list(map(int, input().split())))
_input()
print('YES' if solve(a, b) else 'NO')


(n, m) = map(int, input().split())
mata = [None] * n
matb = [None] * n
for i in range(n):
	mata[i] = list(map(int, input().split()))
for i in range(n):
	matb[i] = list(map(int, input().split()))
la = [None] * (n + m - 1)
lb = [None] * (n + m - 1)
for i in range(n + m - 1):
	la[i] = []
	lb[i] = []
for i in range(n):
	for j in range(m):
		la[i + j].append(mata[i][j])
		lb[i + j].append(matb[i][j])
ans = 'YES'
for i in range(n + m - 1):
	la[i].sort()
	lb[i].sort()
	if la[i] != lb[i]:
		ans = 'NO'
print(ans)


(n, m) = [int(x) for x in input().split()]
a = [[int(x) for x in input().split()] for j in range(n)]
b = [[int(x) for x in input().split()] for j in range(n)]
diag_a = [[] for i in range(n + m)]
diag_b = [[] for i in range(n + m)]
for i in range(n):
	for j in range(m):
		diag_a[i + j].append(a[i][j])
		diag_b[i + j].append(b[i][j])
good = 1
for i in range(n + m):
	if sorted(diag_a[i]) != sorted(diag_b[i]):
		good = 0
		break
if good:
	print('YES')
else:
	print('NO')


def main():
	(n, m) = (int(x) for x in input().split(' '))
	a = []
	b = []
	for _ in range(n):
		a.append([int(x) for x in input().split(' ')])
	for _ in range(n):
		b.append([int(x) for x in input().split(' ')])
	for s in range(n + m - 1):
		ma = {}
		for j in range(max(s - n + 1, 0), min(m - 1, s) + 1):
			if a[s - j][j] in ma:
				ma[a[s - j][j]] += 1
			else:
				ma[a[s - j][j]] = 1
		mb = {}
		for j in range(max(s - n + 1, 0), min(m - 1, s) + 1):
			if b[s - j][j] in mb:
				mb[b[s - j][j]] += 1
			else:
				mb[b[s - j][j]] = 1
		if len(ma) != len(mb):
			print('NO')
			return
		for (k, v) in ma.items():
			if k not in mb or mb[k] != v:
				print('NO')
				return
	print('YES')
main()


(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	c = list(map(int, input().split()))
	a.append(c)
for i in range(n):
	c = list(map(int, input().split()))
	b.append(c)
for i in range(n):
	for j in range(m):
		if a[i][j] == b[i][j]:
			continue
		else:
			k = i + 1
			l = j - 1
			flag = 0
			while k < n and 0 <= l:
				if a[k][l] == b[i][j]:
					flag = 1
					(a[i][j], a[k][l]) = (a[k][l], a[i][j])
					break
				l -= 1
				k += 1
			if flag == 0:
				print('NO')
				exit()
print('YES')


import sys

def equal(arr, arr_1):
	for i in range(len(arr)):
		if arr[i] != arr_1[i]:
			return False
	return True
(n, m) = [int(s) for s in input().split()]
array = []
array_2 = []
for i in range(n):
	array.append([int(s) for s in input().split()])
for i in range(n):
	array_2.append([int(s) for s in input().split()])
diags = []
diags_2 = []
for i in range(n):
	tmp = []
	tmp_2 = []
	j = 0
	while i - j > -1 and j < m:
		tmp.append(array[i - j][j])
		tmp_2.append(array_2[i - j][j])
		j += 1
	diags.append(tmp)
	diags_2.append(tmp_2)
for i in range(1, m):
	tmp = []
	tmp_2 = []
	j = 0
	while i + j < m and n - j - 1 > -1:
		tmp.append(array[n - j - 1][i + j])
		tmp_2.append(array_2[n - j - 1][i + j])
		j += 1
	diags.append(tmp)
	diags_2.append(tmp_2)
for i in range(len(diags)):
	diags[i] = sorted(diags[i])
	diags_2[i] = sorted(diags_2[i])
for i in range(len(diags)):
	if not equal(diags[i], diags_2[i]):
		print('NO')
		sys.exit(0)
print('YES')


(n, m) = map(int, input().split())
A = []
for i in range(n):
	row = list(map(int, input().split()))
	A.append(row)
B = []
for i in range(n):
	row = list(map(int, input().split()))
	B.append(row)
freqA = []
for i in range(n + m - 1):
	freqA.append([])
freqB = []
for i in range(n + m - 1):
	freqB.append([])
for i in range(n):
	for j in range(m):
		freqA[i + j].append(A[i][j])
for i in range(n):
	for j in range(m):
		freqB[i + j].append(B[i][j])
ok = True
for i in range(len(freqA)):
	freqA[i].sort()
	freqB[i].sort()
	if freqA[i] != freqB[i]:
		ok = False
		break
if ok:
	print('YES')
else:
	print('NO')


(n, m) = [int(k) for k in input().split()]
a = [[int(k) for k in input().split()] for _ in range(n)]
b = [[int(k) for k in input().split()] for _ in range(n)]
x = [[] for _ in range(n + m - 1)]
y = [[] for _ in range(n + m - 1)]
for i in range(n):
	for j in range(m):
		x[i + j].append(a[i][j])
		y[i + j].append(b[i][j])
bl = []
for i in range(n + m - 1):
	bl.append(sorted(x[i]) == sorted(y[i]))
if all(bl):
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().strip().split())
matrix1 = []
matrix2 = []
ans = 'YES'
for _ in range(n):
	matrix1.append(list(map(int, input().strip().split())))
for _ in range(n):
	matrix2.append(list(map(int, input().strip().split())))
for idx_sum in range(n + m - 1):
	line1 = []
	line2 = []
	for raw_id in range(idx_sum + 1):
		if raw_id < n and idx_sum - raw_id < m:
			line1.append(matrix1[raw_id][idx_sum - raw_id])
			line2.append(matrix2[raw_id][idx_sum - raw_id])
	line1 = sorted(line1)
	line2 = sorted(line2)
	if line1 != line2:
		ans = 'NO'
print(ans)


import sys
import heapq
import math
from enum import Enum
lines = sys.stdin.read().splitlines()
(n, m) = map(int, lines[0].split(' '))
matA = []
matB = []
for i in range(0, n):
	row = list(map(int, lines[i + 1].split(' ')))
	matA.append(row)
for i in range(0, n):
	row = list(map(int, lines[i + n + 1].split(' ')))
	matB.append(row)
allElemsA = [[] for i in range(0, n + m - 1)]
for i in range(0, n):
	for j in range(0, m):
		allElemsA[i + j].append(matA[i][j])
allElemsB = [[] for i in range(0, n + m - 1)]
for i in range(0, n):
	for j in range(0, m):
		allElemsB[i + j].append(matB[i][j])
can = True
for i in range(0, len(allElemsA)):
	can = can and sorted(allElemsA[i]) == sorted(allElemsB[i])
	if not can:
		break
if can:
	print('YES')
else:
	print('NO')


from sys import stdin, stdout
(n, m) = map(int, stdin.readline().split())
mata = [None] * n
matb = [None] * n
for i in range(n):
	mata[i] = list(map(int, stdin.readline().split()))
for i in range(n):
	matb[i] = list(map(int, stdin.readline().split()))
la = [None] * (n + m - 1)
lb = [None] * (n + m - 1)
for i in range(n + m - 1):
	la[i] = []
	lb[i] = []
for i in range(n):
	for j in range(m):
		la[i + j].append(mata[i][j])
		lb[i + j].append(matb[i][j])
flag = True
for i in range(n + m - 1):
	la[i].sort()
	lb[i].sort()
	if la[i] != lb[i]:
		flag = False
if flag:
	stdout.write('YES')
else:
	stdout.write('NO')


def main():
	(n, m) = map(int, input().split())
	a = [[] for i in range(n)]
	b = [[] for i in range(n)]
	aDicts = [dict() for i in range(n + m - 1)]
	bDicts = [dict() for i in range(n + m - 1)]
	for i in range(n):
		a[i] = list(map(int, input().split()))
	for i in range(n):
		b[i] = list(map(int, input().split()))
	for i in range(n):
		for j in range(m):
			if a[i][j] in aDicts[i + j].keys():
				aDicts[i + j][a[i][j]] += 1
			else:
				aDicts[i + j][a[i][j]] = 1
			if b[i][j] in bDicts[i + j].keys():
				bDicts[i + j][b[i][j]] += 1
			else:
				bDicts[i + j][b[i][j]] = 1
	for i in range(n + m - 1):
		if aDicts[i] != bDicts[i]:
			print('NO')
			return 0
	print('YES')
	return 0
main()


(n, m) = map(int, input().split())
a = []
b = []
lol = 0
la = [[] for i in range(n * m)]
lb = [[] for i in range(n * m)]
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
for i in range(n):
	for j in range(m):
		la[i + j].append(a[i][j])
		lb[i + j].append(b[i][j])
for i in range(n * m):
	la[i].sort()
	lb[i].sort()
	if la[i] != lb[i]:
		lol = 1
		break
if lol == 0:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = [None] * n
b = [None] * n
f = [None] * (n + m)
g = [None] * (n + m)
for i in range(n + m):
	f[i] = []
	g[i] = []
for i in range(n):
	a[i] = [int(o) for o in input().split()]
	for j in range(m):
		f[i + j].append(a[i][j])
for i in range(n):
	b[i] = [int(o) for o in input().split()]
	for j in range(m):
		g[i + j].append(b[i][j])
flag = 0
for i in range(m + n):
	f[i] = sorted(f[i])
	g[i] = sorted(g[i])
	if f[i] != g[i]:
		flag = 1
		break
if flag == 0:
	print('YES')
else:
	print('NO')


from sys import stdin, stdout
(n, m) = map(int, stdin.readline().split())
mata = [None] * n
matb = [None] * n
for i in range(n):
	mata[i] = list(map(int, stdin.readline().split()))
for i in range(n):
	matb[i] = list(map(int, stdin.readline().split()))
la = [None] * (n + m - 1)
lb = [None] * (n + m - 1)
for i in range(n + m - 1):
	la[i] = []
	lb[i] = []
for i in range(n):
	for j in range(m):
		la[i + j].append(mata[i][j])
		lb[i + j].append(matb[i][j])
ans = 'YES'
for i in range(n + m - 1):
	la[i].sort()
	lb[i].sort()
	if la[i] != lb[i]:
		ans = 'NO'
		break
stdout.write(ans)


(n, m) = map(int, input().split())
L = [list(map(int, input().split())) for i in range(n)]
L1 = [list(map(int, input().split())) for i in range(n)]
l = [[] for i in range(n + m - 1)]
l1 = [[] for i in range(n + m - 1)]
for i in range(n):
	for j in range(m):
		l[i + j].append(L[i][j])
		l1[i + j].append(L1[i][j])
for x in range(n + m - 1):
	v = l[x]
	v1 = l1[x]
	v = sorted(v)
	v1 = sorted(v1)
	if v != v1:
		print('NO')
		exit()
print('YES')


(n, m) = map(int, input().strip().split())
arr = []
for i in range(n):
	a = list(map(int, input().strip().split()))
	arr.append(a)
brr = []
for i in range(n):
	a = list(map(int, input().strip().split()))
	brr.append(a)
flag = 0
j = 0
for i in range(n):
	a = []
	b = []
	p = i
	q = 0
	while p >= 0 and q < m:
		a.append(arr[p][q])
		b.append(brr[p][q])
		p -= 1
		q += 1
	a = sorted(a)
	b = sorted(b)
	if a != b:
		flag = 1
for j in range(m):
	a = []
	b = []
	p = n - 1
	q = j
	while p >= 0 and q < m:
		a.append(arr[p][q])
		b.append(brr[p][q])
		p -= 1
		q += 1
	a = sorted(a)
	b = sorted(b)
	if a != b:
		flag = 1
if flag == 0:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = [[int(x) for x in input().split()] for _ in range(n)]
b = [[int(x) for x in input().split()] for _ in range(n)]
transa = [[] for _ in range(n + m)]
transb = [[] for _ in range(n + m)]
for i in range(n):
	for j in range(m):
		transa[i + j].append(a[i][j])
		transb[i + j].append(b[i][j])
for i in range(n + m):
	if sorted(transa[i]) != sorted(transb[i]):
		print('NO')
		exit()
print('YES')


num = [int(x) for x in input().split(' ')]
(n, m) = (num[0], num[1])
table1 = list()
table2 = list()
for i in range(n):
	num = [int(x) for x in input().split(' ')]
	table1.append(num)
test = table1
max_col = len(test)
max_row = len(test[0])
cols = [[] for i in range(max_col)]
rows = [[] for i in range(max_row)]
fdiag = [[] for i in range(max_col + max_row - 1)]
for y in range(max_col):
	for x in range(max_row):
		cols[y].append(test[y][x])
		rows[x].append(test[y][x])
		fdiag[x + y].append(test[y][x])
dig1 = fdiag
for i in range(n):
	num = [int(x) for x in input().split(' ')]
	table2.append(num)
test = table2
max_col = len(test)
max_row = len(test[0])
cols = [[] for i in range(max_col)]
rows = [[] for i in range(max_row)]
fdiag = [[] for i in range(max_col + max_row - 1)]
for y in range(max_col):
	for x in range(max_row):
		cols[y].append(test[y][x])
		rows[x].append(test[y][x])
		fdiag[x + y].append(test[y][x])
dig2 = fdiag
f = 0
for i in range(m + n - 1):
	if sorted(dig1[i]) != sorted(dig2[i]):
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


(m, n) = map(int, input().split())
a = []
b = []
for i in range(m):
	x = list(map(int, input().split()))
	a.append(x)
for i in range(m):
	x = list(map(int, input().split()))
	b.append(x)
data_a = [[] for _ in range(m + n)]
data_b = [[] for _ in range(m + n)]
for i in range(m):
	for j in range(n):
		data_a[i + j].append(a[i][j])
		data_b[i + j].append(b[i][j])
flag = True
for i in range(m + n - 1):
	data_a[i].sort()
	data_b[i].sort()
	if data_a[i] != data_b[i]:
		flag = False
		break
if flag:
	print('yes')
else:
	print('no')


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
b = [list(map(int, input().split())) for i in range(n)]
a1 = [[] for i in range(n + m + 100)]
b1 = [[] for i in range(n + m + 100)]
for i in range(n):
	for j in range(m):
		a1[i + j].append(a[i][j])
		b1[i + j].append(b[i][j])
for i in range(n + m + 5):
	a1[i].sort()
	b1[i].sort()
	if len(a1[i]) != len(b1[i]):
		print('NO')
		exit(0)
	for j in range(len(a1[i])):
		if a1[i][j] != b1[i][j]:
			print('NO')
			exit(0)
print('YES')


(n, m) = map(int, input().split())
m1 = []
m2 = []
for i in range(n):
	m1.append([int(j) for j in input().split()])
for i in range(n):
	m2.append([int(j) for j in input().split()])
d1 = [[] for i in range(n + m - 1)]
d2 = [[] for i in range(n + m - 1)]
for i in range(n):
	for j in range(m):
		d1[i + j].append(m1[i][j])
		d2[i + j].append(m2[i][j])
flag = True
for i in range(n + m - 1):
	d1[i].sort()
	d2[i].sort()
	if d1[i][:] != d2[i][:]:
		flag = False
if flag:
	print('YES')
else:
	print('NO')


import sys
(n, m) = map(int, input().split())
A = []
for i in range(0, n):
	A.append(input().split())
B = []
for i in range(0, n):
	B.append(input().split())
for i in range(1, n + m):
	(x, y) = (min(i - 1, n - 1), max(i - n, 0))
	(a, b) = ([], [])
	while x >= 0 and y < m:
		a.append(A[x][y])
		b.append(B[x][y])
		x -= 1
		y += 1
	a.sort()
	b.sort()
	for k in range(len(a)):
		if a[k] != b[k]:
			print('NO')
			sys.exit()
print('YES')


(n, m) = map(int, input().split())
A = []
B = []
for i in range(n):
	A.append([int(x) for x in input().split()])
for i in range(n):
	B.append([int(x) for x in input().split()])
flag = 0
for i in range(n + m - 1):
	a = []
	b = []
	if i < m:
		(r, c) = (0, i)
	else:
		(r, c) = (i - m + 1, m - 1)
	while r < n and c >= 0:
		a.append(A[r][c])
		b.append(B[r][c])
		r = r + 1
		c = c - 1
	a = sorted(a)
	b = sorted(b)
	if a != b:
		print('NO')
		flag = 1
		break
if flag == 0:
	print('YES')


(n, m) = map(int, input().split())
data_1 = []
data_2 = []
fl = True
diagonal_1 = [[] for i in range(n + m - 1)]
diagonal_2 = [[] for i in range(n + m - 1)]
for i in range(n):
	data_3 = list(map(int, input().split()))
	data_1.append(data_3)
for i in range(n):
	data_3 = list(map(int, input().split()))
	data_2.append(data_3)
if data_1[0][0] != data_2[0][0]:
	print('No')
elif data_1[0][0] != data_2[0][0]:
	print('NO')
else:
	for i in range(len(data_1)):
		for j in range(len(data_1[i])):
			diagonal_1[i + j - 1].append(data_1[i][j])
	for i in range(len(data_1)):
		for j in range(len(data_1[i])):
			diagonal_2[i + j - 1].append(data_2[i][j])
	for i in range(len(diagonal_1)):
		diagonal_1[i].sort()
	for i in range(len(diagonal_1)):
		diagonal_2[i].sort()
	if diagonal_1 == diagonal_2:
		print('Yes')
	else:
		print('No')


def func(maximum, list1, list2):
	for i in range(maximum + 1):
		list1[i].sort()
		list2[i].sort()
		if list1[i] != list2[i]:
			return 'NO'
	return 'YES'
(n, m) = map(int, input().split())
l = []
l1 = []
mm = m + n - 1
while len(l) < mm + 1:
	l += [[]]
while len(l1) < mm + 1:
	l1 += [[]]
for i in range(n):
	temp = list(map(int, input().split()))
	for j in range(m):
		l[i + j] += [temp[j]]
for i in range(n):
	temp = list(map(int, input().split()))
	for j in range(m):
		l1[i + j] += [temp[j]]
print(func(mm, l, l1))


(n, m) = map(int, input().split())
(m1, m2) = ([list(map(int, input().split())) for _ in range(n)], [list(map(int, input().split())) for _ in range(n)])
for i in range(n):
	(x, y) = (0, i)
	d = {}
	while y >= 0 and x < m:
		d[m1[y][x]] = d[m1[y][x]] + 1 if m1[y][x] in d else 1
		d[m2[y][x]] = d[m2[y][x]] - 1 if m2[y][x] in d else -1
		x += 1
		y -= 1
	for k in d:
		if d[k] != 0:
			print('NO')
			raise SystemExit()
for i in range(1, m):
	(x, y) = (i, n - 1)
	d = {}
	while x < m and y >= 0:
		d[m1[y][x]] = d[m1[y][x]] + 1 if m1[y][x] in d else 1
		d[m2[y][x]] = d[m2[y][x]] - 1 if m2[y][x] in d else -1
		x += 1
		y -= 1
	for k in d:
		if d[k] != 0:
			print('NO')
			raise SystemExit()
print('YES')


from sys import stdin, stdout
(R, C) = [int(x) for x in stdin.readline().split()]
mat1 = [[0] * C for _ in range(R)]
mat2 = [[0] * C for _ in range(R)]
for i in range(R):
	mat1[i] = [int(x) for x in stdin.readline().split()]
for i in range(R):
	mat2[i] = [int(x) for x in stdin.readline().split()]
for i in range(1, R + C):
	if i <= C:
		x = 0
		y = i - 1
	else:
		x = i - C
		y = C - 1
	d1 = []
	d2 = []
	while x >= 0 and x < R and (y >= 0) and (y < C):
		d1.append(mat1[x][y])
		d2.append(mat2[x][y])
		x += 1
		y -= 1
	d1.sort()
	d2.sort()
	for j in range(len(d1)):
		if d1[j] != d2[j]:
			print('NO')
			quit()
print('YES')


(n, m) = map(int, input().split())
A = []
B = []
for i in range(n):
	A.append(list(map(int, input().split())))
for i in range(n):
	B.append(list(map(int, input().split())))
lenkey = n - 1 + (m - 1) + 1
checkA = []
checkB = []
for i in range(lenkey):
	checkA.append([])
	checkB.append([])
for i in range(n):
	for j in range(m):
		checkA[i + j].append(A[i][j])
		checkB[i + j].append(B[i][j])
checkMat = True
for i in range(lenkey):
	if sorted(checkA[i]) != sorted(checkB[i]):
		checkMat = False
		break
if checkMat:
	print('YES')
else:
	print('NO')


(n, m) = [int(x) for x in input().split(' ')]
ROW = n
COL = m
data = []

def diagonalOrder(matrix):
	d = []
	for line in range(1, ROW + COL):
		start_col = max(0, line - ROW)
		count = min(line, COL - start_col, ROW)
		row = []
		for j in range(0, count):
			row.append(matrix[min(ROW, line) - j - 1][start_col + j])
		row.sort()
		d.append(row)
	return d
for _ in range(n):
	data.append([int(x) for x in input().split(' ')])
d1 = diagonalOrder(data)
data = []
for _ in range(n):
	data.append([int(x) for x in input().split(' ')])
d2 = diagonalOrder(data)
if d1 == d2:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
A = []
B = []
for i in range(n):
	A.append(list(map(int, input().split())))
for i in range(n):
	B.append(list(map(int, input().split())))
tmpa = []
tmpb = []
for i in range(n + m - 1):
	tmpa.append([])
	tmpb.append([])
for i in range(n):
	for j in range(m):
		tmpa[i + j].append(A[i][j])
		tmpb[i + j].append(B[i][j])
res = True
for i in range(n + m - 1):
	tmpa[i].sort()
	tmpb[i].sort()
	res = res and tmpa[i] == tmpb[i]
if res:
	print('YES')
else:
	print('NO')


def f(L):
	T = []
	j = 0
	for i in range(n):
		j = 0
		s = 0
		p = i
		while p >= 0 and j <= m - 1:
			s += L[p][j]
			p -= 1
			j += 1
		T.append(s)
	for j in range(1, m):
		i = n - 1
		s = 0
		q = j
		while q <= m - 1 and i >= 0:
			s += L[i][q]
			q += 1
			i -= 1
		T.append(s)
	return T
R = lambda : map(int, input().split())
(n, m) = R()
A = []
for i in range(n):
	A.append(list(R()))
B = []
for i in range(n):
	B.append(list(R()))
D1 = [j for i in A for j in i]
D2 = [j for i in B for j in i]
if sorted(D1) != sorted(D2):
	print('NO')
	quit()
print('YNEOS'[f(A) != f(B)::2])


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import sys

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return list(map(str, input().strip()))

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from itertools import groupby
from itertools import permutations as comb

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def nCr(n, r):
	return fact(n) // (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a):
	print('? {}'.format(a), flush=True)
	n = I()
	return n

def dfs(i, p):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	if n > 2:
		l.append(n)
	return l
(n, m) = lint()
a = []
for _ in range(n):
	a.append(lint())
b = []
for _ in range(n):
	b.append(lint())
flag = True
for i in range(n + m - 1):
	if i < n:
		j = 0
		z = i
	else:
		j = i - n + 1
		z = n - 1
	(tmp, tmp2) = ([], [])
	while z >= 0 and j < m:
		tmp.append(a[z][j])
		tmp2.append(b[z][j])
		z -= 1
		j += 1
	tmp.sort()
	tmp2.sort()
	if tmp != tmp2:
		flag = False
		break
if flag:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
ma = [[] for _ in range(n + m)]
mb = [[] for _ in range(n + m)]
for i in range(n):
	q = list(map(int, input().strip().split()))
	for j in range(m):
		ma[i + j].append(q[j])
for i in range(n):
	q = list(map(int, input().strip().split()))
	for j in range(m):
		mb[i + j].append(q[j])
for i in range(n + m):
	if sorted(ma[i]) != sorted(mb[i]):
		print('NO')
		exit(0)
print('YES')


import collections
temp = list(map(int, input().split(' ')))
(n, m) = (temp[0], temp[1])
setsA = [[] for i in range(n + m - 1)]
setsB = [[] for i in range(n + m - 1)]
for i in range(n):
	temp = list(map(int, input().split(' ')))
	for (ix, el) in enumerate(temp):
		setsA[i + ix].append(el)
for i in range(n):
	temp = list(map(int, input().split(' ')))
	for (ix, el) in enumerate(temp):
		setsB[i + ix].append(el)
poss = True
for i in range(n + m - 1):
	if collections.Counter(setsA[i]) != collections.Counter(setsB[i]):
		poss = False
		break
if poss:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split(' '))
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split(' '))))
for i in range(n):
	b.append(list(map(int, input().split(' '))))
for i in range(n):
	j = i
	k = 0
	c = []
	d = []
	while j >= 0 and k < m:
		c.append(a[j][k])
		d.append(b[j][k])
		j -= 1
		k += 1
	c.sort()
	d.sort()
	for j in range(len(c)):
		if c[j] != d[j]:
			print('NO')
			exit(0)
for i in range(m):
	j = n - 1
	k = i
	c = []
	d = []
	while j >= 0 and k < m:
		c.append(a[j][k])
		d.append(b[j][k])
		j -= 1
		k += 1
	c.sort()
	d.sort()
	for j in range(len(c)):
		if c[j] != d[j]:
			print('NO')
			exit(0)
print('YES')


from collections import Counter
(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
b = [list(map(int, input().split())) for i in range(n)]
ac = [Counter() for i in range(n + m - 1)]
bc = [Counter() for i in range(n + m - 1)]
for i in range(n):
	for j in range(m):
		ac[i + j][a[i][j]] += 1
		bc[i + j][b[i][j]] += 1
if ac == bc:
	print('YES')
else:
	print('NO')


import os
from io import BytesIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import defaultdict

def main():
	(n, m) = map(int, input().split())
	s1 = [defaultdict(int) for _ in range(n + m)]
	s2 = [defaultdict(int) for _ in range(n + m)]
	for s in (s1, s2):
		for i in range(n):
			for (j, x) in enumerate(input().split()):
				x = int(x)
				s[i + j][x] += 1
	if all((d1 == d2 for (d1, d2) in zip(s1, s2))):
		print('YES')
	else:
		print('NO')
main()


import sys
import math
from collections import Counter
from operator import itemgetter
import queue

def IO():
	sys.stdin = open('pyinput.txt', 'r')
	sys.stdout = open('pyoutput.txt', 'w')

def GCD(a, b):
	if b == 0:
		return a
	else:
		return GCD(b, a % b)

def LCM(a, b):
	return a * (b // GCD(a, b))

def scan(TYPE_1, TYPE_2=0):
	if TYPE_1 == int:
		return map(int, sys.stdin.readline().strip().split())
	elif TYPE_1 == float:
		return map(float, sys.stdin.readline().strip().split())
	elif TYPE_1 == list and TYPE_2 == float:
		return list(map(float, sys.stdin.readline().strip().split()))
	elif TYPE_1 == list and TYPE_2 == int:
		return list(map(int, sys.stdin.readline().strip().split()))
	elif TYPE_1 == str:
		return sys.stdin.readline().strip()
	else:
		print('ERROR!!!!')

def main():
	(n, m) = scan(int)
	a = []
	b = []
	for i in range(n):
		x = scan(list, int)
		a.append(x)
	for i in range(n):
		x = scan(list, int)
		b.append(x)
	for x in range(n):
		i = x
		j = 0
		mp = {}
		while i >= 0 and j < m:
			if mp.get(a[i][j], 's') == 's':
				mp[a[i][j]] = 1
			else:
				mp[a[i][j]] += 1
			if mp.get(b[i][j], 'sss') == 'sss':
				mp[b[i][j]] = -1
			else:
				mp[b[i][j]] -= 1
			i -= 1
			j += 1
		for i in mp:
			if mp[i] != 0:
				print('NO')
				return
	for x in range(1, m):
		i = n - 1
		j = x
		mp = {}
		while i >= 0 and j < m:
			if mp.get(a[i][j], 's') == 's':
				mp[a[i][j]] = 1
			else:
				mp[a[i][j]] += 1
			if mp.get(b[i][j], 'sss') == 'sss':
				mp[b[i][j]] = -1
			else:
				mp[b[i][j]] -= 1
			i -= 1
			j += 1
		for i in mp:
			if mp[i] != 0:
				print('NO')
				return
	print('YES')
main()


from collections import defaultdict
(n, m) = map(int, input().split())
record1 = defaultdict(list)
record2 = defaultdict(list)
for i in range(n):
	k = list(map(int, input().split()))
	for j in range(m):
		record1[i + j].append(k[j])
for i in range(n):
	k = list(map(int, input().split()))
	for j in range(m):
		record2[i + j].append(k[j])
flag = 0
for i in range(n + m - 1):
	if sorted(record1[i]) != sorted(record2[i]):
		print('NO')
		flag = 1
		break
if flag == 0:
	print('YES')


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(n)]
B = [list(map(int, input().split())) for i in range(n)]
LISTA = [[] for i in range(n + m + 1)]
LISTB = [[] for i in range(n + m + 1)]
for i in range(n):
	for j in range(m):
		LISTA[i + j].append(A[i][j])
		LISTB[i + j].append(B[i][j])
for i in range(n + m + 1):
	if sorted(LISTA[i]) != sorted(LISTB[i]):
		print('NO')
		sys.exit()
else:
	print('YES')


(n, m) = map(int, input().strip().split())

def inrange(x, y):
	return x >= 0 and y >= 0 and (x < n) and (y < m)
a = []
b = []
for _ in range(n):
	a.append(list(map(int, input().split())))
for _ in range(n):
	b.append(list(map(int, input().split())))
s = True
for i in range(n + m - 1):
	ii = i
	j = 0
	aa = {}
	bb = {}
	while ii >= 0 and j < m:
		if inrange(ii, j):
			if str(a[ii][j]) in aa.keys():
				aa[str(a[ii][j])] += 1
			else:
				aa[str(a[ii][j])] = 0
			if str(b[ii][j]) in bb.keys():
				bb[str(b[ii][j])] += 1
			else:
				bb[str(b[ii][j])] = 0
		ii -= 1
		j += 1
	if not aa == bb:
		s = False
		break
if s:
	print('YES')
else:
	print('NO')


import sys
(n, m) = tuple(map(int, input().split(' ')))
A = []
for i in range(n):
	A.append(list(map(int, input().split(' '))))
B = []
for i in range(n):
	B.append(list(map(int, input().split(' '))))
if A[0][0] != B[0][0] or A[-1][-1] != B[-1][-1]:
	print('NO')
	sys.exit(0)
for i in range(n):
	for j in range(m):
		if i == 0 and j == 0 or (i == n - 1 and j == m - 1):
			continue
		sum1 = i + j
		found = False
		for k in range(sum1 + 1):
			if k < n and sum1 - k < m and (A[i][j] == B[k][sum1 - k]):
				found = True
				B[k][sum1 - k] = -1
				break
		if not found:
			print('NO')
			sys.exit(0)
print('YES')


(n, m) = map(int, input().split())
ans = 'YES'
M = [list(map(int, input().split())) for j in range(n)]
M2 = [list(map(int, input().split())) for j in range(n)]
for hehe in range(1, n + m):
	start_m = max(0, hehe - n)
	count = min(hehe, m - start_m, n)
	(dic1, dic2) = ({}, {})
	for j in range(0, count):
		if M[min(n, hehe) - j - 1][start_m + j] in dic1:
			dic1[M[min(n, hehe) - j - 1][start_m + j]] += 1
		else:
			dic1[M[min(n, hehe) - j - 1][start_m + j]] = 1
		if M2[min(n, hehe) - j - 1][start_m + j] in dic2:
			dic2[M2[min(n, hehe) - j - 1][start_m + j]] += 1
		else:
			dic2[M2[min(n, hehe) - j - 1][start_m + j]] = 1
	if dic1 != dic2:
		ans = 'NO'
print(ans)


size = [int(number) for number in input().split()]
matrixa = []
for i in range(size[0]):
	matrixa.append([int(number) for number in input().split()])
matrixb = []
for i in range(size[0]):
	matrixb.append([int(number) for number in input().split()])

def checkDiagonal(cords):
	if cords[0] > 0 and cords[1] < size[1] - 1:
		return checkDiagonal([cords[0] - 1, cords[1] + 1])
	else:
		done = False
		alist = []
		blist = []
		while not done:
			if cords[0] < size[0] and cords[1] >= 0:
				alist.append(matrixa[cords[0]][cords[1]])
				blist.append(matrixb[cords[0]][cords[1]])
				cords[0] += 1
				cords[1] -= 1
			else:
				done = True
		alist.sort()
		blist.sort()
		if alist == blist:
			return True
		else:
			return False
truth = 'yes'
for i in range(size[0]):
	if checkDiagonal([i, size[1] - 1]) == False:
		truth = 'no'
for i in range(size[1]):
	if checkDiagonal([0, i]) == False:
		truth = 'no'
print(truth)


import sys, math, queue
sys.setrecursionlimit(1000000)
(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
valid = True
for i in range(n):
	d1 = {}
	d2 = {}
	(j, k) = (0, i)
	while j < m and k >= 0:
		if a[k][j] in d1:
			d1[a[k][j]] += 1
		else:
			d1[a[k][j]] = 1
		if b[k][j] in d2:
			d2[b[k][j]] += 1
		else:
			d2[b[k][j]] = 1
		j += 1
		k -= 1
	if d1 != d2:
		valid = False
		break
if not valid:
	print('NO')
	exit()
for j in range(m):
	d1 = {}
	d2 = {}
	(i, k) = (n - 1, j)
	while i >= 0 and k < m:
		if a[i][k] in d1:
			d1[a[i][k]] += 1
		else:
			d1[a[i][k]] = 1
		if b[i][k] in d2:
			d2[b[i][k]] += 1
		else:
			d2[b[i][k]] = 1
		i -= 1
		k += 1
	if d1 != d2:
		valid = False
		break
if not valid:
	print('NO')
	exit(0)
print('YES')


(n, m) = map(int, input().split())
a = [[i for i in map(int, input().split())] for i in range(n)]
b = [[i for i in map(int, input().split())] for i in range(n)]
ans = 1
op = 1
for j in range(m):
	da = {}
	db = {}
	i = 0
	while j >= 0 and i < n:
		da[a[i][j]] = da.get(a[i][j], 0) + 1
		db[b[i][j]] = db.get(b[i][j], 0) + 1
		j -= 1
		i += 1
	if da != db:
		ans = 0
	if not ans:
		break
for i in range(1, n):
	da = {}
	db = {}
	j = m - 1
	while j >= 0 and i < n:
		da[a[i][j]] = da.get(a[i][j], 0) + 1
		db[b[i][j]] = db.get(b[i][j], 0) + 1
		j -= 1
		i += 1
	if da != db:
		ans = 0
	if not ans:
		break
if ans:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
diag_A = []
diag_B = []
for i in range(n + m):
	diag_A.append([])
	diag_B.append([])
A = []
for i in range(n):
	row = input().split()
	A.append(row)
B = []
for i in range(n):
	row = input().split()
	B.append(row)
for row in range(n):
	for col in range(m):
		diag_A[row + col].append(A[row][col])
		diag_B[row + col].append(B[row][col])
for i in range(n + m):
	if sorted(diag_A[i]) != sorted(diag_B[i]):
		print('NO')
		break
else:
	print('YES')


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
aa = [[] for _ in range(n + m)]
bb = [[] for _ in range(n + m)]
for i in range(n):
	for k in range(m):
		aa[i + k].append(a[i][k])
		bb[i + k].append(b[i][k])
print('YES' if all(map(lambda x, y: sorted(x) == sorted(y), aa, bb)) else 'NO')


from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C, deque
from math import ceil, gcd, sqrt, factorial, log2, floor
(n, m) = mp()
(a, b) = ([], [])
for _ in range(n):
	a.append(mp())
for _ in range(n):
	b.append(mp())
ans = 'YES'
d = dd(list)
for i in range(n):
	for j in range(m):
		d[i + j].append(a[i][j])
t = dd(list)
for i in range(n):
	for j in range(m):
		t[i + j].append(b[i][j])
for i in range(m + n):
	if sorted(d[i]) != sorted(t[i]):
		ans = 'NO'
		break
print(ans)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict
import math

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()

def diagonalOrder(matrix, ROW, COL):
	ans = []
	for line in range(1, ROW + COL):
		start_col = max(0, line - ROW)
		count = min(line, COL - start_col, ROW)
		li = []
		for j in range(0, count):
			ele = matrix[min(ROW, line) - j - 1][start_col + j]
			li.append(ele)
		ans.append(sorted(li))
	return ans
(n, m) = listIn()
A = []
B = []
for i in range(n):
	a = listIn()
	A.append(a)
for i in range(n):
	a = listIn()
	B.append(a)
if A[0][0] == B[0][0] and A[n - 1][m - 1] == B[n - 1][m - 1]:
	res = diagonalOrder(A, n, m)
	res2 = diagonalOrder(B, n, m)
	if res == res2:
		print('YES')
	else:
		print('NO')
else:
	print('NO')


n_m = input().split(' ')
n = int(n_m[0])
m = int(n_m[1])
A = []
for i in range(n):
	t = input().split(' ')
	A.append(t)
B = []
for i in range(n):
	t = input().split(' ')
	B.append(t)
for i in range(m + n - 1):
	text_1 = []
	text_2 = []
	for j in range(i + 1):
		if i - j < n and j < m:
			text_1.append(A[i - j][j])
			text_2.append(B[i - j][j])
		if i - j >= n and j < m:
			continue
		if i - j < n and j >= m:
			continue
		if i - j >= n and j >= m:
			continue
	text_1.sort()
	text_2.sort()
	f = True
	if text_1 == text_2:
		f == True
	else:
		f = False
		break
if f:
	print('YES')
else:
	print('NO')


R = lambda : map(int, input().split())
(n, m) = R()

def f():
	a = [()] * (n + m)
	for i in range(n):
		for x in R():
			a[i] += (x,)
			i += 1
	return [*map(sorted, a)]
print('YNEOS'[f() != f()::2])


(n, m) = map(int, input().split())
A = []
B = []
flag = 1
for i in range(n):
	A.append(list(map(int, input().split())))
for i in range(n):
	B.append(list(map(int, input().split())))
for i in range(n + m - 1):
	j = 0
	Alist = []
	Blist = []
	while i >= 0:
		try:
			Alist.append(A[i][j])
			Blist.append(B[i][j])
		except IndexError:
			pass
		j += 1
		i -= 1
	Alist.sort()
	Blist.sort()
	if Alist != Blist:
		flag = 0
if flag == 1:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(n)]
B = [list(map(int, input().split())) for i in range(n)]
X = [[] for i in range(n + m)]
Y = [[] for i in range(n + m)]
for i in range(n):
	for j in range(m):
		X[i + j].append(A[i][j])
for i in range(n):
	for j in range(m):
		Y[i + j].append(B[i][j])
for k in range(n + m):
	x = X[k]
	y = Y[k]
	x.sort()
	y.sort()
	if x != y:
		print('NO')
		exit()
else:
	print('YES')


from sys import stdin, stdout
from math import *
(n, m) = map(int, input().split())
m1 = []
for i in range(n):
	r = list(map(int, input().split()))
	m1.append(r)
m2 = []
for i in range(n):
	r = list(map(int, input().split()))
	m2.append(r)
d = [0] * (n - 1 + m)
for i in range(n - 1 + m):
	d[i] = {}
for i in range(n):
	for j in range(m):
		if d[i + j].get(m2[i][j]) == None:
			d[i + j][m2[i][j]] = 1
		else:
			d[i + j][m2[i][j]] += 1
ans = 'YES'
for i in range(n):
	for j in range(m):
		e = m1[i][j]
		if d[i + j].get(e) == 0 or d[i + j].get(e) == None:
			ans = 'NO'
		else:
			d[i + j][e] -= 1
print(ans)


(n, m) = [int(s) for s in input().split()]
a = []
b = []
for i in range(n):
	a.append([int(s) for s in input().split()])
for i in range(n):
	b.append([int(s) for s in input().split()])
diagsa = []
diagsb = []
for i in range(n - 1):
	k = i
	j = 0
	diagsa.append([])
	diagsb.append([])
	while j < m and k > -1:
		diagsa[i].append(a[k][j])
		diagsb[i].append(b[k][j])
		j = j + 1
		k = k - 1
	diagsa[i].sort()
	diagsb[i].sort()
for j in range(m):
	k = j
	i = n - 1
	diagsa.append([])
	diagsb.append([])
	while k < m and i > -1:
		diagsa[n - 1 + j].append(a[i][k])
		diagsb[n - 1 + j].append(b[i][k])
		i = i - 1
		k = k + 1
	diagsa[n - 1 + j].sort()
	diagsb[n - 1 + j].sort()
for i in range(len(diagsa)):
	if diagsa[i] != diagsb[i]:
		print('NO')
		break
else:
	print('YES')


(n, m) = map(int, input().split())
ma = [[] for i in range(n + m)]
mb = [[] for i in range(n + m)]
w = []
for i in range(n):
	w = list(map(int, input().split()))
	for j in range(m):
		ma[i + j].append(w[j])
w = []
for i in range(n):
	w = list(map(int, input().split()))
	for j in range(m):
		mb[i + j].append(w[j])
for i in range(n + m):
	if sorted(ma[i]) != sorted(mb[i]):
		print('NO')
		exit(0)
print('YES')


from collections import defaultdict
(n, m) = map(int, input().split())
R = n
C = m

def diagonalOrder(matrix):
	config = []
	for line in range(1, R + C):
		d = defaultdict(int)
		lineconfig = []
		start_col = max(0, line - R)
		count = min(line, C - start_col, R)
		for j in range(0, count):
			d[matrix[min(R, line) - j - 1][start_col + j]] += 1
		for (i, j) in d.items():
			lineconfig.append((i, j))
		config.append(sorted(lineconfig))
	return config
a = []
b = []
for _ in range(n):
	a.append(list(map(int, input().split())))
for _ in range(n):
	b.append(list(map(int, input().split())))
set_a = diagonalOrder(a)
set_b = diagonalOrder(b)
if set_a == set_b:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
ar = [0] * 2
diag = [0] * 2
for i in range(2):
	ar[i] = [[int(t) for t in input().split()] for j in range(n)]
	diag[i] = [{} for j in range(n + m - 1)]
for num in range(2):
	for i in range(n):
		for j in range(m):
			cur = ar[num][i][j]
			it = diag[num][j + i]
			if it.get(cur) == None:
				it[cur] = 0
			it[cur] += 1
for i in range(n + m - 1):
	if diag[0][i] != diag[1][i]:
		print('NO')
		exit()
print('YES')


(n, m) = map(int, input().split())
a = []
b = []
f = 0
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
for i in range(n):
	k = 0
	ta = []
	tb = []
	while 1:
		if i - k >= 0 and k < m:
			ta.append(a[i - k][k])
			tb.append(b[i - k][k])
		else:
			break
		k += 1
	ta.sort()
	tb.sort()
	for i in range(len(ta)):
		if ta[i] != tb[i]:
			f = 1
for j in range(m):
	k = 0
	ta = []
	tb = []
	while 1:
		if j + k <= m - 1 and n - 1 - k >= 0:
			ta.append(a[n - 1 - k][j + k])
			tb.append(b[n - 1 - k][j + k])
		else:
			break
		k += 1
	ta.sort()
	tb.sort()
	for i in range(len(ta)):
		if ta[i] != tb[i]:
			f = 1
if f == 0:
	print('YES')
else:
	print('NO')


(r, c) = list(map(int, input().split()))
f = [None] * (r + c - 1)
l = [None] * (r + c - 1)
for i in range(0, r + c - 1):
	f[i] = []
	l[i] = []
for i in range(0, r):
	ip = list(map(int, input().split()))
	for j in range(0, c):
		f[i + j].append(ip[j])
for i in range(0, r):
	ip = list(map(int, input().split()))
	for j in range(0, c):
		l[i + j].append(ip[j])
for i in range(0, r + c - 1):
	f[i].sort()
	l[i].sort()
	if f[i] == l[i]:
		continue
	else:
		print('NO')
		exit(0)
print('YES')


from collections import defaultdict
(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
cnt = [defaultdict(int) for _ in range(1000)]
for i in range(n):
	for j in range(m):
		cnt[i + j][a[i][j]] += 1
		cnt[i + j][b[i][j]] -= 1
for i in range(n + m):
	for key in cnt[i].keys():
		if cnt[i][key] != 0:
			print('NO')
			exit(0)
print('YES')


(sizeI, sizeJ) = map(int, input().split())
have = [list(map(int, input().split())) for _ in range(sizeI)]
need = [list(map(int, input().split())) for _ in range(sizeI)]

def solve():
	for k in range(sizeI + sizeJ - 1):
		(haveDict, needDict) = ({}, {})
		for j in range(k + 1):
			i = k - j
			if i < sizeI and j < sizeJ:
				(haveVal, needVal) = (have[i][j], need[i][j])
				haveDict.setdefault(haveVal, 0)
				haveDict[haveVal] += 1
				needDict.setdefault(needVal, 0)
				needDict[needVal] += 1
		if haveDict != needDict:
			return 'NO'
	return 'YES'
print(solve())


(n, m) = list(map(int, input().split()))
a = []
b = []
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	b.append(list(map(int, input().split())))
a1 = [[] for i in range(n + m - 1)]
for i in range(n):
	j = 0
	k = i
	while k >= 0 and j < m:
		a1[i].append(a[k][j])
		k -= 1
		j += 1
for i in range(1, m):
	j = n - 1
	k = i
	while k < m and j >= 0:
		a1[n + i - 1].append(a[j][k])
		k += 1
		j -= 1
b1 = [[] for i in range(n + m - 1)]
for i in range(n):
	j = 0
	k = i
	while k >= 0 and j < m:
		b1[i].append(b[k][j])
		k -= 1
		j += 1
for i in range(1, m):
	j = n - 1
	k = i
	while k < m and j >= 0:
		b1[n + i - 1].append(b[j][k])
		k += 1
		j -= 1
for i in range(n + m - 1):
	q1 = a1[i]
	q2 = b1[i]
	q1.sort()
	q2.sort()
	if q1 != q2:
		print('NO')
		exit()
print('YES')


(n, m) = map(int, input().split())
a = []
for j in range(n):
	te = [int(v) for v in input().split()]
	a.append(te)
b = []
f = 0
for j in range(n):
	te = [int(v) for v in input().split()]
	b.append(te)
j = 0
while j < m:
	k = 0
	p = j
	x = []
	y = []
	while k < n and p >= 0:
		x.append(a[k][p])
		y.append(b[k][p])
		k = k + 1
		p = p - 1
	x.sort()
	y.sort()
	j = j + 1
	if x[:] != y[:]:
		f = 1
		break
if f == 0:
	j = 1
	while j < n:
		k = j
		p = m - 1
		x = []
		y = []
		while k < n and p >= 0:
			x.append(a[k][p])
			y.append(b[k][p])
			k = k + 1
			p = p - 1
		x.sort()
		y.sort()
		j = j + 1
		if x[:] != y[:]:
			f = 1
			break
if f == 0:
	print('YES')
else:
	print('NO')


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
(n, m) = value()
row_a = defaultdict(list)
row_b = defaultdict(list)
for i in range(n):
	a = array()
	for j in range(m):
		row_a[i + j].append(a[j])
for i in range(n):
	a = array()
	for j in range(m):
		row_b[i + j].append(a[j])
ok = 'YES'
for i in range(m * n):
	if sorted(row_a[i]) != sorted(row_b[i]):
		ok = 'NO'
print(ok)


from sys import *
(n, m) = map(int, stdin.readline().split())
a = []
for i in range(n):
	x = list(map(int, stdin.readline().split()))
	a.append(x)
b = []
for i in range(n):
	x = list(map(int, stdin.readline().split()))
	b.append(x)
c1 = []
for i in range(n + m):
	c1.append([])
c2 = []
for i in range(n + m):
	c2.append([])
for i in range(n):
	for j in range(m):
		c1[i + j].append(a[i][j])
		c2[i + j].append(b[i][j])
f = 0
for i in range(n + m):
	c1[i].sort()
	c2[i].sort()
	if c1[i] != c2[i]:
		f = 1
		break
if f == 1:
	print('NO')
else:
	print('YES')


import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
(nb_rows, nb_cols) = RMI()
mat_A = [RMI() for _ in range(nb_rows)]
mat_B = [RMI() for _ in range(nb_rows)]
mat_AB = [[] for _ in range(nb_rows + nb_cols)]
mat_BA = [[] for _ in range(nb_rows + nb_cols)]
for i in range(nb_rows):
	for j in range(nb_cols):
		mat_AB[i + j].append(mat_A[i][j])
		mat_BA[i + j].append(mat_B[i][j])
mat_ABS = [sorted(x) for x in mat_AB]
mat_BAS = [sorted(x) for x in mat_BA]
print(['NO', 'YES'][mat_ABS == mat_BAS])


from sys import stdin
from collections import defaultdict
input = stdin.readline
(n, m) = map(int, input().split())
mat = [list(map(int, line.split())) for line in stdin.read().splitlines()]
a = defaultdict(list)
b = defaultdict(list)
for i in range(n):
	for j in range(m):
		a[i + j].append(mat[i][j])
		b[i + j].append(mat[i + n][j])
if all((sorted(a[i]) == sorted(b[i]) for i in a.keys())):
	print('YES')
else:
	print('NO')