import Data.List
import Data.Maybe

solve n ps qs =
    abs (pi-qi)
    where 
        xs = sort $ permutations $ concatMap show [1..n]
        pi = fromJust $ elemIndex ps xs
        qi = fromJust $ elemIndex qs xs

main = do
    n <- readLn :: IO Int
    ps <- concat . words <$> getLine :: IO String
    qs <- concat . words <$> getLine :: IO String
    print $ solve n ps qs