using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Atcoder
{
    public class Program
    {
        static void Main(string[] args)
        {
            var n = Read().ToInt();
            var list = new List<int>();
            for (int i = 1; i <= n; i++)
            {
                list.Add(i);
            }
            var r1 = ReadSplit();
            var p = new List<int>();
            for (int i = 0; i < n; i++)
            {
                p.Add(r1[i].ToInt());
            }

            var r2 = ReadSplit();
            var q = new List<int>();
            for (int i = 0; i < n; i++)
            {
                q.Add(r2[i].ToInt());
            }
            var c = Permutations<int>(list, n);
            var ans1 = 0;
            var ans2 = 0;
            for (int i = 0; i < c.Count; i++)
            {
                var isMatch = true;
                for (int j = 0; j < n; j++)
                {
                    if (p[j] != c[i][j])
                    {
                        isMatch = false;
                        break;
                    }
                }
                if (isMatch)
                {
                    ans1 = i + 1;
                    break;
                }
            }
            for (int i = 0; i < c.Count; i++)
            {
                var isMatch = true;
                for (int j = 0; j < n; j++)
                {
                    if (q[j] != c[i][j])
                    {
                        isMatch = false;
                        break;
                    }
                }
                if (isMatch)
                {
                    ans2 = i + 1;
                    break;
                }
            }

            var ans = Math.Abs(ans1 - ans2);
            WriteRead(ans);
        }

        public static string Read()
        {
            return Console.ReadLine();
        }

        public static string[] ReadSplit()
        {
            return Console.ReadLine().Split(' ');
        }

        public static void WriteRead(object str)
        {
            Console.WriteLine(str);
            Read();
        }

        public static void Write(object str)
        {
            Console.WriteLine(str);
        }

        public static List<List<T>> Permutations<T>(List<T> src, int n)
        {
            // nは1以上かつsrcの長さよりも短い必要がある。
            int len = src.Count;
            if (!(0 < n && n <= len))
            {
                throw new ArgumentOutOfRangeException();
            }

            var permutations = new List<List<T>>();
            var queue = new Queue<List<int>>(Enumerable.Range(0, len).Select(i => new List<int>() { i }));
            while (queue.Any())
            {
                var indexes = queue.Dequeue();

                if (indexes.Count == n)
                {
                    permutations.Add(indexes.Select(index => src[index]).ToList());
                    continue;
                }

                for (int index = 0; index < len; index++)
                {
                    if (indexes.Contains(index))
                    {
                        continue;
                    }

                    var newIndexes = new List<int>(indexes);
                    newIndexes.Add(index);
                    queue.Enqueue(newIndexes);
                }
            }

            return permutations;
        }
    }
}


public static class Extentions
{
    public static int ToInt(this object obj)
    {
        return int.Parse(obj.ToString());
    }

    public static decimal ToDecimal(this object obj)
    {
        return decimal.Parse(obj.ToString());
    }

    public static double ToDouble(this object obj)
    {
        return double.Parse(obj.ToString());
    }

    public static T2 GetValueZero<T1, T2>(this Dictionary<T1, T2> d, T1 key) where T2 : struct
    {
        T2 value;
        if (d.TryGetValue(key, out value))
        {
            return value;
        }
        return new T2();
    }
}



Measured memory_bytes: 34012