program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64)::a, i,j,k,ans
    read*, a

    ans=0
    do i = 1, a
        do j = 1, a
            do k = 1, a
                ans= ans+ gcd(i,gcd(j,k))
            end do
        end do
    end do
    print*, ans


contains
recursive function gcd(x,y) result(ret)
    integer(int64):: x,y,ret

    if (mod(x,y) == 0) then
        ret = y
        RETURN
    end if

    ret = gcd(y,mod(x,y))
    
end function
end program name