CodeEff/ECCO huggingface dataset:
# Original solution
from itertools import permutations

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

S=sorted(P)

S=list(permutations(S))
A=[]
for i in S:
    A.append(int("".join(i)))

A.sort()

for i in range(len(A)):
    if A[i]==int("".join(P)):
        pp=i
    if A[i]==int("".join(Q)):
        qq=i
print((abs(pp-qq)))

# Optimized solution
from itertools import permutations
n=int(eval(input()))
P=list(input().split())
Q=list(input().split())
A=[]
for i in list(permutations(P)):
   A.append(int("".join(i)))
A.sort()
print((abs(A.index(int("".join(P)))-A.index(int("".join(Q))))))
# Original solution
import itertools
import math
N = int(eval(input()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = list(map(list,list(itertools.permutations(list(i for i in range(1,N+1)),N))))
a = L.index(P)
b = L.index(Q)
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()))

perm = list(permutations(list(range(1, N+1)), N))
print((abs(perm.index(P) - perm.index(Q))))
# Original solution
import sys
import itertools
import math
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
P = rl()
Q = rl()
L = []
for x in itertools.permutations(list(range(1, N+1))):
    L.append(list(x))
a = 0
b = 0
for i in range(math.factorial(N)):
    if L[i] == P:
        a = i
    if L[i] == Q:
        b = i

print((abs(a-b)))


# Optimized solution
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
P = tuple(lr())
Q = tuple(lr())
a = -1; b = -1
for i, x in enumerate(itertools.permutations(list(range(1, N+1)), N)):
    if x == P:
        a = i
    if x == Q:
        b = i

answer = abs(a-b)
print(answer)
# Original solution
import itertools
n = int(eval(input()))
ls = []
for i in range(1,n+1):
    ls.append(i)
    
p = list(map(int,input().split()))
q = list(map(int,input().split()))

a = 0
b = 0
c = 1
for v in itertools.permutations(ls, n):
    if list(v) == p:
        a = c
    if list(v) == q:
        b = c
    c += 1
    
print((abs(a-b)))

# Optimized solution
#ABC150C
import itertools
n = int(eval(input()))
ls = [i+1 for i in range(n)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))
v = [ list(v) for v in itertools.permutations(ls, n)]
a, b = v.index(p), v.index(q)
print((abs(a-b)))
# Original solution
N = int(eval(input()))
P = [int(i) for i in input().split(' ')]
Q = [int(i) for i in input().split(' ')]

fact = [1] * 8
for i in range(1,8):
    fact[i] = fact[i-1] * i

a = 0
remain = list(range(1,N+1))

for i , v  in enumerate(P):
    num = remain.index(v)
    remain.remove(v)
    a += num * fact[(N - i - 1)]

b = 0
remain = list(range(1,N+1))

for i , v  in enumerate(Q):
    num = remain.index(v)
    remain.remove(v)
    b += num * fact[(N - i - 1)]

print((abs(b-a)))


# Optimized solution
N = int(eval(input()))
P = list(map(int,input().split()))
Q = list(map(int,input().split()))

fact = [1] *(N+1)
for i in range(1,N+1):
    fact[i] = fact[i-1] *i
a = 1
b = 1
P2 = sorted(P)
Q2 = sorted(Q)
for i,(p,q) in enumerate(zip(P,Q)):
    a += P2.index(p)*fact[N-i-1]
    b += Q2.index(q)*fact[N-i-1]
    P2.remove(p)
    Q2.remove(q)
print((abs(b-a)))
# Original solution
import itertools as i
eval(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
x=sorted(list(i.permutations(P)))
print((abs(-~x.index(P)--~x.index(Q))))

# Optimized solution
import itertools as i
eval(input())
f=lambda:tuple(map(int,input().split()))
P=f()
Q=f()
x=sorted(list(i.permutations(P)))
print((abs(-~x.index(P)--~x.index(Q))))
# Original solution
import itertools as i
eval(input())
f=lambda:tuple(map(int,input().split()))
P=f()
Q=f()
x=sorted(list(i.permutations(P)))
print((abs(-~x.index(P)--~x.index(Q))))

# Optimized solution
import itertools as i
f=lambda:tuple(map(int,input().split()))
x=list(i.permutations(list(range(1,int(eval(input()))+1))))
print((abs(-~x.index(f())--~x.index(f()))))
# Original solution
import itertools


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


for i, n in enumerate(itertools.permutations(list(range(1, N+1)), N)):
    if n == P:
        a = i
    if n == Q:
        b = i

ans = abs(b - a)
print(ans)


# Optimized solution
import itertools

n = int(eval(input()))
p = tuple(int(i) for i in input().split())
q = tuple(int(i) for i in input().split())

c = 0
for s in itertools.permutations(list(range(1, n+1)), n):
    c += 1
    if s == p:
        a = c
    if s == q:
        b = c

ans = abs(a - b)
print(ans)
# Original solution
import itertools
n = int(eval(input()))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
li = [i+1 for i in range(n)]
# print(p)
# print(q)

i = 0
for elm in itertools.permutations(li):
    #print(list(elm))
    if list(elm) == p:
        mm1 = i
    if list(elm) == q:
        mm2 = i
    i+=1
print((abs(mm1-mm2)))

# Optimized solution
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

import itertools

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

li = [i+1 for i in range(N)]

cnt = 0
for i in itertools.permutations(li):
    if list(i) == P:
        a = cnt
    if list(i) == Q:
        b = cnt
    cnt += 1

print((abs(a-b)))
# Original solution
import itertools
n = int(eval(input()))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
l = [i+1 for i in range(n)]
r = list(itertools.permutations(l))
j = 0
flagp = True
flagq = True
while flagp:
  if r[j] == p:
    x = j+1
    flagp = False
  j += 1
j = 0
while flagq:
  if r[j] == q:
    y = j+1
    flagq = False
  j += 1
print((abs(y-x)))

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

# print(p)
# print(q)

l = [str(i+1) for i in range(n)]

cnt = 1
for v in permutations(l,n):
    if p == v:
        a = cnt
    if q == v:
        b = cnt
    cnt += 1
print((abs(a-b)))
# Original solution
from itertools import permutations
N = int(eval(input()))
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
A = sorted(P)
cnt = 0
for x in permutations(A,N):
    x = list(x)
    if x==P:
        a = cnt
    if x==Q:
        b = cnt
    cnt += 1
print((abs(a-b)))

# Optimized solution
from itertools import permutations
N = int(eval(input()))
P = tuple(list(map(int,input().split())))
Q = tuple(list(map(int,input().split())))
cnt = 0
for x in permutations(list(range(1,N+1)),N):
    if P==x:
        indP = cnt
    if Q==x:
        indQ = cnt
    cnt += 1
print((abs(indP-indQ)))
# Original solution
import itertools
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
o=0
h=0
u=list(itertools.permutations(list(range(1,n+1))))
for i in range(len(u)):
    if u[i]==p:
        h=i
for i in range(len(u)):
    if u[i]==q:
        o=i
print((abs(h-o)))


# Optimized solution
import itertools
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
o=0
h=0
u=list(itertools.permutations(list(range(1,n+1))))
for i in range(len(u)):
    if u[i]==p:
        h=i
    if u[i]==q:
        o=i
print((abs(h-o)))
# Original solution
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools

def main():
    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)), n)))
    ans = []
    for i in range(len(l)):
        if l[i] == p:
            ans.append(i)
        elif l[i] == q:
            ans.append(i)
    if len(ans) == 1:
        print((0))
    else:
        print((abs(ans[0]-ans[1])))







if __name__=="__main__":
    main()


# Optimized solution
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools

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


if __name__=="__main__":
    main()
# Original solution
from itertools import permutations
n = int(eval(input()))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

a = [i+1 for i in range(n)]
for i,per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print((abs(x-y)))


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

a = [i+1 for i in range(n)]
for i,per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print((abs(x-y)))
# Original solution
from collections import deque

def solve(N, P, Q):
    stack = deque()
    vals = [i for i in range(1, N + 1)]
    for idx, next_val in enumerate(vals):
        stack.append([[next_val], vals[: idx] + vals[idx + 1:]])
    a, b = -1, -1
    counter = 0
    while stack:
        cur_list, rest = stack.popleft()
        if not rest:
            counter += 1
            a = counter if cur_list == P else a
            b = counter if cur_list == Q else b
            if 0 < a and 0 < b:
                print((abs(a - b)))
                return

        for idx, next_val in enumerate(rest):
            stack.append([cur_list + [next_val], rest[: idx] + rest[idx + 1:]])

if __name__ == "__main__":
    N = int(eval(input()))
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    solve(N, P, Q)


# Optimized solution
from itertools import permutations

def solve(N, P, Q):
    vals = [i for i in range(1, N + 1)]
    perms = list(permutations(vals))
    print((abs(perms.index(P) - perms.index(Q))))

if __name__ == "__main__":
    N = int(eval(input()))
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    solve(N, P, Q)
# Original solution
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 03:09:14 2020

@author: Kanaru Sato
"""
import itertools as it

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

count = 0
for sequence in it.permutations(l,n):
    if list(sequence) == p:
        pnum = count
    if list(sequence) == q:
        qnum = count
    count += 1

print((abs(pnum-qnum)))

# Optimized solution
# -*- coding: utf-8 -*-
"""
Created on Sun May 17 19:16:09 2020

@author: Kanaru Sato
"""
import itertools

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

nums = [i+1 for i in range(n)]

count = 0
for perm in itertools.permutations(nums):
    count += 1
    if p == perm:
        pcount = count
    if q == perm:
        qcount = count

print((abs(pcount-qcount)))
# Original solution
from itertools import permutations 

n = int(eval(input()))
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
a = 0
b = 0
cnt = 0
st = 0
for i in permutations(list(range(1,n+1))):
    cnt += 1
    if list(i) == p:
        a = cnt
        st += 1
    if list(i) == q:
        b = cnt
        st += 1
    if st == 2:
        break
    
print((abs(a-b)))

# Optimized solution
from itertools import permutations 

n = int(eval(input()))
p = tuple(int(i) for i in input().split())
q = tuple(int(i) for i in input().split())
a = 0
b = 0
cnt = 0
st = 0
for i in permutations(list(range(1,n+1))):
    cnt += 1
    if i == p:
        a = cnt
        st += 1
    if i == q:
        b = cnt
        st += 1
    if st == 2:
        break
    
print((abs(a-b)))
# Original solution
# -*- coding:utf-8 -*-
import copy

def solve():
    N = int(eval(input()))
    Ps = tuple(map(int, input().split()))
    Qs = tuple(map(int, input().split()))

    available_list = [i for i in range(1, N+1)]

    permutations = []  # 順列
    def _dfs(rest_list, p):
        if len(rest_list) == 0:
            permutations.append(tuple(p))
            return

        for i in range(1, N+1):
            if i in rest_list:
                tmp_rest_list = copy.deepcopy(rest_list)
                tmp_rest_list.remove(i)
                tmp_p = copy.deepcopy(p)
                tmp_p.append(i)
                _dfs(tmp_rest_list, tmp_p)


    _dfs(available_list, [])

    permutations.sort()

    for i, p in enumerate(permutations):
        if p == Ps:
            a = i+1
        if p == Qs:
            b = i+1

    print((abs(a-b)))


if __name__ == "__main__":
    solve()


# Optimized solution
# -*- coding:utf-8 -*-
# https://atcoder.jp/contests/abc150/tasks/abc150_c

def solve():
    import itertools
    import bisect

    N = int(eval(input()))
    Ps = tuple(map(int, input().split()))
    Qs = tuple(map(int, input().split()))

    lst = [i for i in range(1, N+1)]
    origins = list(itertools.permutations(lst, r=N))
    origins.sort()

    for i, p in enumerate(origins):
        if p == Ps:
            a = i
        if p == Qs:
            b = i

    print((abs(a-b)))



if __name__ == "__main__":
    solve()
# Original solution
n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

from collections import deque
tuples=[]
def f(que):
    if len(que)==n:
        tuples.append(tuple(que))
        return
    
    for v in range(1,n+1):
        if v not in que:
            que.append(v)
            f(que)
            que.pop()
f(deque([]))
#print(tuples)
#print(tuples.index(p))
#print(tuples.index(q))
print((abs(tuples.index(p)-tuples.index(q))))

# Optimized solution
import itertools
N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

a,b = 0,0
for i, perm in enumerate(itertools.permutations(list(range(1, N+1))), 1):
    if P==perm:
        a = i
    if Q==perm:
        b = i
    if a > 0 and b > 0:
        print((abs(a-b)))
        exit()
# Original solution
import itertools


N = int(eval(input()))

P = "".join(input().split())
Q = "".join(input().split())

# print(P)

p_index = -1
q_index = -1
permutation_list = list(itertools.permutations([x + 1 for x in range(N)]))
# print(permutation_list)
for i in range(len(permutation_list)):
    # print(permutation_list[i])
    param = "".join(map(str, permutation_list[i]))
    if P == param:
        p_index = i
    if Q == param:
        q_index = i
    if p_index != -1 and q_index != -1:
        result = p_index - q_index
        if result < 0:
            result = -result
        print(result)
        exit(0)


# Optimized solution
import itertools


N = int(eval(input()))

P = "".join(input().split())
Q = "".join(input().split())

p_index = -1
q_index = -1
permutation_list = list(itertools.permutations([x + 1 for x in range(N)]))
for i in range(len(permutation_list)):
    param = "".join(map(str, permutation_list[i]))
    if P == param:
        p_index = i
    if Q == param:
        q_index = i
    if p_index != -1 and q_index != -1:
        print((abs(p_index - q_index)))
        exit(0)
# Original solution
import itertools

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

L = [i for i in range(1, N+1)]
a = 0
b = 0

#順列作成
L_list = list(itertools.permutations(L))

for i in range(len(L_list)):
  if list(L_list[i]) == P:
    a = i+1

for i in range(len(L_list)):
  if list(L_list[i]) == Q:
    b = i+1
#  else:
#    continue

print((abs(a-b)))

# Optimized solution
import itertools

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

L = [i for i in range(1, N+1)]
a = 0
b = 0

#順列作成
L_list = list(itertools.permutations(L))

for i in range(len(L_list)):
  if list(L_list[i]) == P:
    a = i+1
  if list(L_list[i]) == Q:
    b = i+1

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

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

L = [i for i in range(1, N+1)]
a = 0
b = 0

#順列作成
L_list = list(itertools.permutations(L))

for i in range(len(L_list)):
  if list(L_list[i]) == P:
    a = i+1
  if list(L_list[i]) == Q:
    b = i+1

print((abs(a-b)))

# Optimized solution
#パクリ
import itertools

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

lis=list(itertools.permutations(P))
lis.sort()

#indexで要素の何番目か取得
print((abs(lis.index(P)-lis.index(Q))))
# Original solution
import itertools
import math

n = int(eval(input()))


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


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

b = []
for i in range(1,len(a)+1):
    b.append(i)

it = itertools.zip_longest(b, a)

for i in it:
    if p != d:
        if p == list(i[1]):
            pcount = int(i[0])

        elif d == list(i[1]):
            dcount = int(i[0])

    else:
        pcount = 0
        dcount = 0
        
print((abs(pcount-dcount)))


# Optimized solution
import itertools
import math

n = int(eval(input()))


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


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

b = []
for i in range(1,len(a)+1):
    b.append(i)

it = itertools.zip_longest(b, a)

pcount = 0
dcount = 0

for i in it:
    if p != d:
        if p == list(i[1]):
            pcount = int(i[0])
        if d == list(i[1]):
            dcount = int(i[0])


print((abs(pcount-dcount)))
# Original solution
#!/usr/bin/env python3
import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):

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

    p_at = 0
    for i, pr in enumerate(perm):
        if pr == tuple(P):
            p_at = i + 1
        if pr == tuple(Q):
            q_at = i + 1
    print((abs(p_at - q_at)))
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)


if __name__ == "__main__":
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
import itertools
import bisect


def solve(N: int, P: "List[int]", Q: "List[int]"):

    nums = [i for i in range(1, N + 1)]
    perm = list(itertools.permutations(nums, N))
    print((abs(perm.index(tuple(P)) - perm.index(tuple(Q)))))
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    P = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    Q = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, P, Q)


if __name__ == "__main__":
    main()
# Original solution
from itertools import permutations
n = int(eval(input()))
p = list(map(str, input().split()))
q = list(map(str, input().split()))
s = "".join(map(str, list(range(1, (n+1)))))
x = list(permutations(s))
x = [list(i) for i in x]
print((abs(x.index(p)-x.index(q))))


# Optimized solution
from itertools import permutations

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

for i, X in enumerate(permutations(list(range(1, N + 1)), N)):
    if P == X:
        a = i
    if Q == X:
        b = i
print((abs(a - b)))
# Original solution
import itertools
s = 1
a = 0
b = 0
N = int(eval(input()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == p:
        a = s
    s += 1
s = 1
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)

# Optimized solution
import itertools
N = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
a = 0
b = 0
s = 1
for i in ite:
    if i == p:
        a = s
    if i == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)
# Original solution
import itertools

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

list_ = list(range(1, n + 1))

for num ,v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        
for num ,v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1

ans = abs(res_p - res_q)

print(ans)

# Optimized solution
import itertools

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

list_ = list(range(1, n + 1))

for num ,v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        break
for num ,v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1
        break
ans = abs(res_p - res_q)

print(ans)
# Original solution
import itertools

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

list_ = list(range(1, n + 1))

for num ,v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        break
for num ,v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1
        break
ans = abs(res_p - res_q)

print(ans)

# Optimized solution
import itertools

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

list_ = list(range(1, n + 1))

def turn_num(list_num, arr, m):
    for num ,v in enumerate(itertools.permutations(list_num, m)):
        if arr == list(v):
            res = num + 1
            break
    return res

ans = abs(turn_num(list_, p, n) - turn_num(list_, q, n))

print(ans)
# Original solution
N = int(eval(input()))
p1 = tuple(map(int,input().split()))
p2 = tuple(map(int,input().split()))
def dfs(pair=[]):
  global lst
  if len(pair) == N:
    lst.append(tuple(pair))
  else:
    for j in range(1,N+1):
      if j not in pair:
        pair.append(j)
        dfs(pair)
        pair.pop()

lst = []
dfs()
lst.sort()
print((abs(lst.index(p1) - lst.index(p2))))

# Optimized solution
def LI():
  return list(map(int, input().split()))

N = int(eval(input()))
P = LI()
Q = LI()

result = []
def permutations(N, cur):
  if len(cur) == N:
    result.append(cur)
    return

  for i in range(1, N+1):
    if i not in cur:
      permutations(N, cur+[i])

permutations(N,[])
result.sort()
print((abs(result.index(P) - result.index(Q))))
# Original solution
from itertools import permutations
n=int(eval(input()))
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
a=sorted(list(permutations(p)))
print((abs(a.index(p)-a.index(q))))

# Optimized solution
from itertools import permutations
n=int(eval(input()))
p=tuple(input().split())
q=tuple(input().split())
a=sorted(list(permutations(p)))
print((abs(a.index(p)-a.index(q))))