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))))
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))))
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))))

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))))

-N = int(eval(input())) -p1 = tuple(map(int, input().split())) -p2 = tuple(map(int, input().split())) +def LI(): +    return list(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() +N = int(eval(input())) +P = LI() +Q = LI() +result = [] -lst = [] -dfs() -lst.sort() -print((abs(lst.index(p1) - lst.index(p2)))) +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))))