num = gets.to_i
omo = [1,2,6,24,120,720,5040]
seed = []
num.times do |i|
	seed << i+1
end

arr = gets.split.map(&:to_i)
fir = 1
seed1 = seed.dup

num.times do |i|
	s = seed1.index(arr[i])
	fir = omo[num-2-i]*s + fir
	seed1.delete(arr[i])
end

arr = gets.split.map(&:to_i)
sec = 1
seed2 = seed.dup

num.times do |i|
	s = seed2.index(arr[i])
	sec = omo[num-2-i]*s + sec
	seed2.delete(arr[i])
end
ans = fir - sec
p ans.abs