program main
    implicit none
    integer K, ans, a, b, c
    ans = 0
    read (*, *) K
    do a = 1, K
        do b = 1, K
            do c = 1, K
                ans = ans + gcd(a, gcd(b, c))
            end do
        end do
    end do
    write(*, '(I0)') ans
    stop
contains
    recursive integer function gcd(x, y) result(ret)
        implicit none
        integer x, y
        if (y == 0) then
            ret = x
        else
            ret = gcd(y, mod(x, y))
        end if
        return
    end function
end program main