program ABC162C
    implicit none
    integer(8)::i,j,k,N,ans
    read*,N
    ans=0

    do i=1,N
        do j=1,N
            do k=1,N
                ans=ans+gcd(i,gcd(j,k))
            end do
        end do
    end do

    print'(i0)',ans
contains
    integer(8) function gcd(a,b)
        implicit none
        integer(8),intent(IN)::a,b
        integer(8)::l,m,n

        m=a
        n=b

        do
            l=mod(m,n)
            if(l==0)then
                exit
            end if
            m=n
            n=l
        end do

        gcd=n
        return
    end function gcd
end program ABC162C