using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = Io.Read<int>();
            var P = string.Join("", Io.ReadList<int>());
            var Q = string.Join("", Io.ReadList<int>());

            var permutation = Permutation.All(Enumerable.Range(1, N))
                .Select(p => string.Join("", p));

            var i = 1;
            var a = 0;
            var b = 0;
            foreach (var p in permutation)
            {
                if (p == P)
                {
                    a = i;
                }

                if (p == Q)
                {
                    b = i;
                }

                i++;
            }

            var output = Math.Abs(a - b);
            Io.Write(output.ToString());
        }
        static class Permutation
        {
            private static void Search<T>(List<T[]> perms, Stack<T> stack, T[] a)
            {
                int N = a.Length;
                if (N == 0)
                {
                    perms.Add(stack.Reverse().ToArray());
                }
                else
                {
                    var b = new T[N - 1];
                    Array.Copy(a, 1, b, 0, N - 1);
                    for (int i = 0; i < a.Length; ++i)
                    {
                        stack.Push(a[i]);
                        Search(perms, stack, b);
                        if (i < b.Length) { b[i] = a[i]; }
                        stack.Pop();
                    }
                }
            }
            public static IEnumerable<T[]> All<T>(IEnumerable<T> src)
            {
                var perms = new List<T[]>();
                Search(perms, new Stack<T>(), src.ToArray());
                return perms;
            }
        }

        static int NumberOfDigits(long x)
        {
            if (x < 10) return 1;

            var result = 0;
            while (x > 0)
            {
                result++;
                x /= 10;
            }

            return result;
        }

        static int SumOfDigits(int x)
        {
            var sum = 0;
            while (x > 0)
            {
                sum += x % 10;
                x /= 10;
            }

            return sum;
        }

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

            public static IEnumerable<string> ReadRepeat(int count)
            {
                return Enumerable.Range(0, count).Select(_ => Read());
            }

            public static T Read<T>()
            {
                return (T)Convert.ChangeType(Read(), typeof(T));
            }

            public static IEnumerable<T> ReadRepeat<T>(int count)
            {
                return Enumerable.Range(0, count).Select(_ => Read<T>());
            }

            public static string[] ReadList(char devide = ' ')
            {
                return Read().Split(devide);
            }

            public static T[] ReadList<T>(char devide = ' ')
            {
                return ReadList(devide)
                    .Select(s => (T)Convert.ChangeType(s, typeof(T)))
                    .ToArray();
            }

            public static IEnumerable<T[]> ReadListRepeat<T>(int count, char devide = ' ')
            {
                return Enumerable.Range(0, count).Select(_ => ReadList<T>(devide));
            }

            public static void Write(string value)
            {
                Console.WriteLine(value);
            }
        }
    }
}


Measured memory_bytes: 21320