program count_order
    implicit none
    integer(4):: n, i, t, pl, ql
    integer(4), allocatable:: p(:), q(:)
    integer(4), parameter:: bi(8) = [1, 2, 6, 24, 120, 720, 5040, 40320]

    read*, n
    allocate(p(n), q(n))
    read*, p(:)
    read*, q(:)
    pl = 0

    do i = 1, n-1
        where(p(i+1:) > p(i))
            p(i+1:) = p(i+1:) - 1
        end where
        p(i) = p(i) * bi(n-i)
    end do

    do i = 1, n-1
        where( q(i+1:) > q(i))
            q(i+1:) = q(i+1:) - 1
        end where
        q(i) = q(i) * bi(n-i)
    end do

    print*, abs(sum(p-q))
end program count_order