CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))


import itertools

L = list(itertools.permutations([i for i in range(1, N + 1)]))

a = b = 1

for l in L:
    if l == P:
        break
    a += 1
for l in L:
    if l == Q:
        break
    b += 1
print((abs(a-b)))

# Optimized solution
n = int(eval(input()))
from itertools import *
l = list(permutations(list(range(1, n + 1)), n))
l.sort()
p = l.index(tuple(map(int, input().split())))
q = l.index(tuple(map(int, input().split())))
print((abs(p - q)))
# Original solution
import itertools,math
n = int(eval(input()))
a = sorted(list(itertools.permutations([ i for i in range(1,n+1)])))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
print((abs(a.index(p) - a.index(q))))

# Optimized solution
import itertools
n = int(eval(input()))
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
L = list(itertools.permutations([i for i in range(1,n+1)]))
print((
    abs((L.index(P)+1)-((L.index(Q)+1)))
    ))
# Original solution
import itertools
N = int(eval(input()))
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
num = 0
for v in list(itertools.permutations(list(range(1,N+1)))):
    num += 1
    if list(v) == P:
        A = num
    elif list(v) == Q:
        B = num
    else:
        pass
try:
    print((abs(A-B)))
except:
    print((0))

# Optimized solution
import itertools
N = int(eval(input()))
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
A = B = 0
for i,v in enumerate(list(itertools.permutations(list(range(1,N+1)))), 1):
    if list(v) == P:
        A = i
    elif list(v) == Q:
        B = i
    else:
        pass

print((abs(A-B) if A != 0 and B != 0 else 0))
# Original solution
import bisect

N = int(eval(input()))
L = int(''.join([str(n) for n in range(1, N+1)]))
R = int(''.join([str(n) for n in range(1, N+1)][::-1]))

P = int(''.join(input().split()))
Q = int(''.join(input().split()))

C = [str(c) for c in range(1, N+1)]
A = []
for i in range(L, R+1):
    flg = True
    for c in C:
        if c not in str(i):
            flg = False
            break
    if flg:
        A.append(i)
print((abs(bisect.bisect(A, P)-bisect.bisect(A, Q))))


# Optimized solution
from itertools import permutations

N = int(eval(input()))
P = tuple(int(n) for n in input().split())
Q = tuple(int(n) for n in input().split())

a = [x for x in permutations(list(range(1,N+1)), N)]

print((abs(a.index(P)-a.index(Q))))
# Original solution
n = int(eval(input()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
import itertools
i = 1
for ptn in itertools.permutations(list(range(1,n+1))):
    if list(ptn)==p:
      a = i
    if list(ptn)==q:
      b = i
    i += 1
print((abs(a-b)))

# Optimized solution
N = int(eval(input()))
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
from itertools import permutations
l = list(permutations(list(range(1,N+1))))
print((abs(l.index(P) - l.index(Q))))
# Original solution
#python3
from itertools import permutations
n=int(eval(input()))
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]

def f(lis):
  cnt=0
  for i in permutations(list(range(1,n+1))):
    cnt+=1
    if i == tuple(lis):
      return cnt

print((abs(f(p)-f(q))))


# Optimized solution
def main():
    n = int(eval(input()))
    p = tuple(int(i) for i in input().split())
    q = tuple(int(i) for i in input().split())
    from itertools import permutations 
    a = [i for i in range(1, n+1)]
    o1 = -1
    o2 = -1
    cnt = 0
    for i in permutations(a):
      cnt += 1
      if i == p:
          o1 = cnt
      if i == q:
          o2 = cnt
      ans = abs(o1-o2)
    print(ans)
main()
# Original solution
def main():
    n = int(eval(input()))
    p = tuple(int(i) for i in input().split())
    q = tuple(int(i) for i in input().split())
    from itertools import permutations 
    a = [i for i in range(1, n+1)]
    o1 = -1
    o2 = -1
    cnt = 0
    for i in permutations(a):
      cnt += 1
      if i == p:
          o1 = cnt
      if i == q:
          o2 = cnt
      ans = abs(o1-o2)
    print(ans)
main()

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from itertools import permutations 
def main():
    n = int(readline())
    p = tuple([int(i) for i in readline().split()])
    q = tuple([int(i) for i in readline().split()])
    num = [i for i in range(1, n+1)]

    c1 = 0
    c2 = 0
    cnt = 0
    for perm in permutations(num):
        cnt += 1
        if perm == p:
            c1 = cnt
        if perm == q:
            c2 = cnt
    print((abs(c1-c2)))   
if __name__ == '__main__':
    main()
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import permutations as perm
n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))
lst = list(perm(list(range(1, n+1)),n))
lst.sort()
pn=qn=0
for i,j in enumerate(lst, 1):
  if p == j:
    pn = i
  if q == j:
    qn = i
  if pn*qn!=0:
    break
print((abs(pn-qn)))

# Optimized solution
from itertools import permutations as perm
import sys
readline = sys.stdin.buffer.readline

n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))

p_lst = list(perm(list(range(1, n + 1)), n))  # 排列
pn = qn = 0
for i,j in enumerate(p_lst, 1):
  if p == j:
    pn = i
  if q == j:
    qn = i
  if pn * qn != 0:
    break
print((abs(pn - qn)))
# Original solution
N = int(eval(input()))

def factorial(num):
    if num == 1:
        return 1
    return num * factorial(num - 1)

def count_order(group):
    numbers = [i for i in range(1, N + 1)]
    res = 1
    for i in range(len(group) - 1):
        num = group[i]
        res += numbers.index(num) * pattern[-(i + 1)]
        numbers.pop(numbers.index(num))
    return res


P = list(map(int, input().split()))
Q = list(map(int, input().split()))

pattern = [factorial(i) for i in range(1, N)]
res_p = count_order(P)
res_q = count_order(Q)

print((abs(res_p - res_q)))


# Optimized solution
from itertools import permutations
n  = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
comb = list(permutations([i for i in range(1, n + 1)]))
comb.sort()
a = 0
b = 0

for i in range(len(comb)):
    if list(comb[i]) == P:
        a = i
    if list(comb[i]) == Q:
        b = i
ans = abs(a - b)
print(ans)
# Original solution
from itertools import permutations
N = int(eval(input()))
P, Q = input().replace(' ', ''), input().replace(' ', '')
a = b = x = 0
for d in permutations(''.join(map(str, list(range(1, N+1)))), N):
  x += 1
  s = ''.join(d)
  if s == P:
    a = x
  if s == Q:
    b = x
print((abs(a - b)))

# Optimized solution
from itertools import permutations
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
i = a = b = 0
for x in permutations(list(range(1, N+1)), N):
  i += 1
  if x == P:
    a = i
  if x == Q:
    b = i
print((abs(a - b)))
# Original solution
import itertools
n = int(eval(input()))
p = tuple(list(map(int, input().split())))
q = tuple(list(map(int, input().split())))
po = 0
qo = 0
l = sorted(list(itertools.permutations(list(range(1,n+1)))))
for i in range(len(l)):
    if l[i] == p:
        po = i
    if l[i]== q:
        qo = i
print((abs(po-qo)))

# Optimized solution
import itertools
n = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
l = sorted(itertools.permutations([i for i in range(1,n+1)]))
print((abs(l.index(tuple(p))-l.index(tuple(q)))))
# Original solution
import itertools
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
Nlist = []
for i in range(N):
    Nlist.append(i+1)

J = list(itertools.permutations(Nlist, N))

count = 0
for i in range(len(J)):
    if J[i] == P:
        a = i+1
        count += 1
        if count == 2:
            break
    if J[i] == Q:
        b = i+1
        count += 1
        if count == 2:
            break

print((abs(a-b)))


# Optimized solution
import itertools


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


N = int(eval(input()))
P = LI()
Q = LI()
Nlist = list(range(1, N+1))
count = 1
for v in itertools.permutations(Nlist, N):
    if P == v:
        p = count
    if Q == v:
        q = count
    count += 1
print((abs(p-q)))
# Original solution
import math

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = len(l)
        index = l.index(a)
        # print(a, index, math.factorial(len_l - 1), order)
        order += index * math.factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)

if __name__ == '__main__':
    main()


# Optimized solution
def factorial(n):
    ret = 1
    for i in range(n):
        ret *= (i + 1)
    return ret

def calc_order(A, N):
    l = [i + 1 for i in range(N)]
    order = 1
    for i in range(N):
        a = A[i]
        len_l = N - i
        index = l.index(a)
        # print(a, index, factorial(len_l - 1), order)
        order += index * factorial(len_l - 1)
        del l[index]
    return order

def main():
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    ord_p = calc_order(P, N)
    ord_q = calc_order(Q, N)
    ans = abs(ord_p - ord_q)
    # print(ord_p, ord_q)
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
import itertools
N = int(eval(input()))
P = [int(a)-1 for a in input().split()]
Q = [int(a)-1 for a in input().split()]
i = 0
ans = 0
for a in itertools.permutations(list(range(N)), N):
    a = [b for b in a]
    if a == P:
        ans -= i
    if a == Q:
        ans += i
    i += 1

print((abs(ans)))

# Optimized solution
fact = [1]
for i in range(20):
    fact.append(fact[-1] * (i+1))

def calc(L):
    n = len(L)
    if n == 1: return 0
    a =  len([l for l in L if l < L[0]])
    return a * fact[n-1] + calc(L[1:])

N = int(eval(input()))
P = [int(a)-1 for a in input().split()]
Q = [int(a)-1 for a in input().split()]

print((abs(calc(P) - calc(Q))))
# Original solution
import itertools
N=int(eval(input()))
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
K=list(itertools.permutations(list(range(1,N+1))))
print((abs(K.index(a)-K.index(b))))

# Optimized solution
import itertools as it
N=int(eval(input()))
W=list(range(1,N+1))
c1=-1
c2=-1
c=0
L=tuple(map(int,input().split()))
M=tuple(map(int,input().split()))
if L==M:
  print((0))
  exit()
for i in it.permutations(W,N):
  c+=1
  if i==L:
    c1=c
  elif i==M:
    c2=c
print((abs(c1-c2)))
# Original solution
import collections

n = int(input())
p = list(map(int,input().split(' ')))
q = list(map(int,input().split(' ')))


elems = list(range(1, n+1))
res = []
c = [0]
def perms(i, elems,res):

	if i == len(elems):
		res.append(tuple(elems)) 
		return 
	for j in range(i, len(elems)):
		elems[j], elems[i] = elems[i], elems[j]
		perms(i+1, elems, res)
		elems[j], elems[i] = elems[i], elems[j]

perms(0, elems, res)

tp = tuple(p)
tq = tuple(q)
res.sort()
for i in range(len(res)):
	if res[i] == tp:
		ii = i

	if res[i] == tq:
		jj = i

print(abs(jj - ii))

# Optimized solution
import collections
n = int(input())
p,q = list(map(int,input().split(' '))), list(map(int,input().split(' ')))
res = []

def perms(i, elems,res):

	if i == len(elems):
		res.append(tuple(elems)) 
		return 
	for j in range(i, len(elems)):
		elems[j], elems[i] = elems[i], elems[j]
		perms(i+1, elems, res)
		elems[j], elems[i] = elems[i], elems[j]

perms(0, list(range(1, n+1)), res)
tp,tq = tuple(p),tuple(q)
res.sort()
for i in range(len(res)):
	if res[i] == tp: ii = i
	if res[i] == tq: jj = i

print(abs(jj - ii))
# Original solution
#<C>
import math
import itertools
n = int(eval(input()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
ans1=n
ans2=n
x = [i for i in range(1, n+1)]
for i in itertools.permutations(x,n):
    if p < list(i):
        ans1 -= 1
    if q < list(i):
        ans2 -= 1

print((abs(ans1-ans2)))
    

# Optimized solution
#<順列全探索>
#n!通りの順列の組み合わせを全探索する方法。
#pythonにはitertoolsが存在するからそれを利用
#<ex.1(ABC150-C)>
import itertools 

n = int(eval(input()))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
ls = list(itertools.permutations(list(range(1, n + 1)))) #順列作成

print((abs(ls.index(p) - ls.index(q))))
# Original solution
#abc150c
from itertools import *

n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
l=list(permutations(list(range(1,n+1))))
print((abs(l.index(p)-l.index(q))))


# Optimized solution
#abc150c
from itertools import *

n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
l=list(permutations(list(range(1,n+1))))
print(abs(l.index(p)-l.index(q)))
# Original solution
from itertools import permutations

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

for i, pat in enumerate(permutations(list(range(1, n + 1)), n)):
    if pat == p:
        a = i + 1
    if pat == q:
        b = i + 1

print((abs(a - b)))


# Optimized solution
from itertools import permutations

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

for i, pat in enumerate(permutations(list(range(1, n + 1)), n)):
    if pat == p:
        a = i

    if pat == q:
        b = i

ans = abs(a - b)
print(ans)
# Original solution
from itertools import permutations
stdin = open(0).read().split('\n')
N = int(stdin[0])

P=int("".join(list(stdin[1].split())))
Q=int("".join(list(stdin[2].split())))

l = []
for pp in permutations(list(range(1, N+1))):
    ll = []
    for _p in pp:
        ll.append(str(_p))
    l.append(int("".join(ll)))
a = l.index(P)
b = l.index(Q)
print((abs(a - b)))

# Optimized solution
def perm(N):
    if N==0:return 0
    a=1
    for i in range(1,N+1):
        a*=i
    return a

def getposi(P):
    N=len(P)
    l=list(range(1,N+1))
    a = 0
    for i,num in enumerate(P):
        a+=perm(N-i-1)*l.index(num)
        l.remove(num)
    return a

stdin = open(0).read().split('\n')
N = int(stdin[0])
P = list(map(int,stdin[1].split()))
Q = list(map(int,stdin[2].split()))

a=getposi(P)
b=getposi(Q)

print((abs(a-b)))
# Original solution
from itertools import permutations
n = int(eval(input()))
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))

dict = {}
d = [i for i in range(1,n+1)]
i = 1
for x in permutations(d,n):
    dict[x] = i
    i += 1

print((abs(dict[P]-dict[Q])))

# Optimized solution
from itertools import permutations
n = int(eval(input()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))

a,b = -1,-1
for i,x in enumerate(permutations([i+1 for i in range(n)])):
    c,d = True,True
    for j in range(n):
        if x[j] != p[j]:
            c = False
        if x[j] != q[j]:
            d = False
    if c:a = i+1
    if d:b = i+1
print((abs(a-b)))
# Original solution
N=int(eval(input()))
P=list(map(int,input().split()))
ps=""
for i in range(N):
  ps+=str(P[i])
Q=list(map(int,input().split()))
qs=""
for i in range(N):
  qs+=str(Q[i])

stack=[]
stack.append("")
res=[]
while stack:
  d=stack.pop()
  if len(d)==N:
    res.append(d)
    continue
  for i in range(1,N+1):
    if str(i) not in d:
      stack.append(d+str(i))
  
res=res[::-1]
a=0
b=0
for i in range(len(res)):
  if res[i]==ps:
    a=i+1
  if res[i]==qs:
    b=i+1

print((abs(a-b)))


# Optimized solution
import sys
readline = sys.stdin.readline

N = int(readline())
P = tuple(map(int,readline().split()))
Q = tuple(map(int,readline().split()))

import itertools
a = 0
b = 0
cnt = 0
for perm in itertools.permutations(list(range(1, N + 1))):
  cnt += 1
  if perm == P:
    a = cnt
  if perm == Q:
    b = cnt
    
print((abs(a - b)))
# Original solution
import itertools
N = int(eval(input()))
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
n_list = [i for i in range(1,N+1)]
n_perm = sorted(list(itertools.permutations(n_list, N)))
a = 0
b = 0

for i in range(len(n_perm)):
    if list(n_perm[i]) == P:
        a = i
        break

for k in range(len(n_perm)):
    if list(n_perm[k]) == Q:
        b = k
        break
        
print((abs(a-b)))    

# Optimized solution
import itertools
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
l=sorted(list(itertools.permutations(list(range(1,n+1)))))
print((abs(l.index(p)-l.index(q))))
# Original solution
from itertools import permutations
n=int(eval(input()))
p=list(map(int,input().split()))
q=list(map(int,input().split()))
c=sorted(list(permutations(list(range(1,n+1)))))
a,b,=0,0
for i,c in enumerate(c):
    if p==list(c):
        a=i
    if q==list(c):
        b=i
print((abs(a-b)))

# Optimized solution
from itertools import permutations
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
P=sorted(list(permutations(list(range(1,n+1)),n)))
print((abs(P.index(p)-P.index(q))))
# Original solution
n = int(eval(input()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
import itertools
lst = [i+1 for i in range(n)]
l = [0]*len(lst)
for i in itertools.permutations(lst):
    l.append(list(i))
for i in range(len(l)):
    if p == l[i]:
        a = i
    if q == l[i]:
        b = i
print((abs(b-a)))

# Optimized solution
from itertools import permutations as pm
n = int(eval(input()))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
i = 0

for l in pm([i+1 for i in range(n)],n):
    if l == p:
        a = i
    if l == q:
        b = i
    i += 1
print((abs(a-b)))
# Original solution
from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

perm = list(permutations(list(range(1, N + 1))))

print((abs(perm.index(P) - perm.index(Q))))


# Optimized solution
import sys
from itertools import permutations

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N = int(readline())
    P = tuple(map(int, readline().split()))
    Q = tuple(map(int, readline().split()))

    perms = list(permutations(list(range(1, N + 1))))
    print((abs(perms.index(P) - perms.index(Q))))
    return


if __name__ == '__main__':
    main()
# Original solution
import math
n=int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a=0
b=0
x=[i+1 for i in range(n)]
y=[i+1 for i in range(n)]

for i in range(n-1):
    a+=x.index(p[i])*math.factorial(n-1-i)
    b+=y.index(q[i])*math.factorial(n-1-i)
    x.remove(p[i])
    y.remove(q[i])


print((abs(a-b)))

# Optimized solution
import itertools

n=int(eval(input()))
p= list(map(int, input().split()))
q= list(map(int, input().split()))
x=[i+1 for i in range(n)]

x=list(itertools.permutations(x))
a=0
b=0

for i in range(len(x)):
    if list(x[i])==p:
        a=i
    if list(x[i])==q:
        b=i

print((abs(a-b)))
# Original solution
import itertools

n = int(eval(input()))
p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
num_list = []
num = 0
a = 0
b = 0

for i in itertools.permutations(list(range(1,n+1))):
    num_list = list(i)
    num += 1
    if num_list == p_list:
        a = num
    if num_list == q_list:
        b = num

print((abs(a-b)))

# Optimized solution
import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

num_list = list(itertools.permutations(list(range(1,n+1))))

print((abs(num_list.index(p)-num_list.index(q))))
# Original solution
import itertools


def checkArrays(arr1, arr2):
    if len(arr1) != len(arr2):
        return False

    for i in range(len(arr1)):
        if arr1[i] != arr2[i]:
            return False

    return True


n = int(eval(input()))

arrays = list(itertools.permutations([i for i in range(1, n + 1)]))

p = list(map(int, input().split()))
q = list(map(int, input().split()))

a = b = 1
for i in range(len(arrays)):
    ar = list(arrays[i])
    if checkArrays(ar, p):
        a = i + 1
    if checkArrays(ar, q):
        b = i + 1

print((abs(a - b)))



# Optimized solution
import itertools

n = int(eval(input()))

array = [i for i in range(1, n + 1)]
gen_array = itertools.permutations(array, n)

a = list(map(int, input().split()))
b = list(map(int, input().split()))

idx = 1
for lis in gen_array:
    if list(lis) == a:
        num_a = idx
    if list(lis) == b:
        num_b = idx
    idx += 1

print((abs(num_a - num_b)))
# Original solution
from sys import stdin
import math

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
lin = list(map(int,stdin.readline().rstrip().split()))
from itertools import permutations
lis = permutations(li)

liv = []

for i in lis:
    liv.append(list(i))
liv.sort()
a = liv.index(li)
b = liv.index(lin)

print((abs(a-b)))

# Optimized solution
from itertools import permutations

n = int(eval(input()))
li = [i for i in range(1,n+1)]
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

for i,j in enumerate(permutations(li)):
    if j == p:
        num_p = i
    if j == q:
        num_q = i

print((abs(num_p-num_q)))
# Original solution
import itertools
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
A = [i for i in range(1, N+1)]
B = list(itertools.permutations(A))
ans = abs(B.index(P)-B.index(Q))
print(ans)


# Optimized solution
import itertools
N = int(eval(input()))
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
X = list(itertools.permutations(list(range(1, N+1))))
a = X.index(A)
b = X.index(B)
print((abs(a-b)))
# Original solution
n = int(input())
mxs, minm = 0, 1000
for i in range(n):
    m, s  = list(map(int, input().split()))
    if s > mxs:
        mxs = s
        minm = m
    elif s == mxs:
        if m < minm:
            minm = m
print(minm, mxs)

# Optimized solution
n = int(input())
mxs, minm = 0, 21
for i in range(n):
    m, s  = list(map(int, input().split()))
    if s > mxs:
        mxs = s
        minm = m
    elif s == mxs:
        if m < minm:
            minm = m
print(minm, mxs)
# Original solution
import bisect
n,m = list(map(int,input().split()))
xy = sorted([list(map(int,input().split())) + [i] for i in range(n)])
yx = sorted(xy, key = lambda x:x[1])
X = [i[0] for i in xy]
Y = [i[1] for i in yx]
xy = [str(i[0]) + "," + str(i[1]) + "_" + str(i[2]) for i in xy]
yx = [str(i[0]) + "," + str(i[1]) + "_" + str(i[2]) for i in yx]
ans = {}
for xi in range(n):
	for xj in range(xi,n+1):
		for yi in range(n):
			for yj in range(yi,n+1):
				ans[str(xi) + "_" +  str(xj) + "_" + str(yi) + "_" + str(yj)] = len(set(xy[xi:xj])&set(yx[yi:yj]))
for i in range(m):
	x1,y1,x2,y2 = list(map(int,input().split()))
	x1 = bisect.bisect_left(X,x1)
	y1 = bisect.bisect_left(Y,y1)
	x2 = bisect.bisect_right(X,x2)
	y2 = bisect.bisect_right(Y,y2)
	try: print(ans[str(x1) + "_" +  str(x2) + "_" + str(y1) + "_" + str(y2)])
	except: print(0)

# Optimized solution
import bisect
inf = 1e10
n,m = list(map(int,input().split()))
xy = [list(map(int,input().split())) for i in range(n)]
X = sorted([i[0] for i in xy] + [-inf-1] + [inf+1])
Y = sorted([i[1] for i in xy] + [-inf-1] + [inf+1])

s = [[0]*(n+10) for i in range(n+10)]
for i in range(n):
	a = bisect.bisect_left(X,xy[i][0])
	b = bisect.bisect_left(Y,xy[i][1])
	s[a][b] += 1

for i in range(n+2):
	for j in range(n+2):
		s[i+1][j+1] += s[i+1][j] + s[i][j+1] - s[i][j]

for i in range(m):
	x1,y1,x2,y2 = list(map(int,input().split()))
	x1 = bisect.bisect_left(X,x1) - 1
	y1 = bisect.bisect_left(Y,y1) - 1
	x2 = bisect.bisect_left(X,x2+1) - 1
	y2 = bisect.bisect_left(Y,y2+1) - 1
	print(s[x2][y2] - s[x1][y2] - s[x2][y1] + s[x1][y1])