'use strict'

function main(input) {
  input = input.split('\n')
  const P = input[1].split(' ').map(str => parseInt(str))
  const Q = input[2].split(' ').map(str => parseInt(str))

  // P, Qを辞書順に並べる
  const pDic = [...P].sort()
  const qDic = [...Q].sort()

  // 順列の何番目かを取得
  const a = permutation(pDic).findIndex(e => e.toString() === P.toString()) + 1
  const b = permutation(qDic).findIndex(e => e.toString() === Q.toString()) + 1

  console.log(Math.abs(a-b))
}

// 順列作成
const permutation = (list) => {
  if(!list.length) return;

  if(list.length === 1) {
    return [list]
  }

  let res = []
  let tmpList
  list.forEach((e, i) => {
    tmpList = [...list]
    tmpList.splice(i, 1) // 先頭にする数値を取り除く

    let perm = permutation(tmpList) // 再帰で順列作成
    
    perm.forEach((f) => {
      f.unshift(e)
      res.push(f)
    })
  })

  return res
}

main(require("fs").readFileSync("/dev/stdin", "utf8"))