program prob3
    implicit none
    integer(8)::a,b,c, aa, bb, cc, rem, k, e, gcd
    integer(16)::ans
    read(*,*) k
    ans = 0

    do a = 1, k
        do b = 1, k
            aa = max(a,b)
            bb = min(a,b)
            rem = mod(aa, bb)

            do while(rem /= 0)
                aa = bb
                bb = rem
                rem = mod(aa, bb)
            end do
            gcd = bb
            
            do c = 1, k
                aa = max(c,gcd)
                bb = min(c,gcd)
                rem = mod(aa, bb)
                do while(rem /= 0)
                    aa = bb
                    bb = rem
                    rem = mod(aa,bb)
                end do
                ans = ans + bb
            end do
        end do
    end do

    write(*,*) ans
    stop
end program prob3