program main
    implicit none
    integer(8) k
    integer(8) i, j, l, m
    integer(8) a, b, c, d
    integer(8), dimension(100) :: t,s
    integer(8) sum
    read(*,*) k

    do i = 1, 100
        t(i) = 0
        s(i) = 0
    end do
    
    sum = 0

    do i = 1, k
        do j = 1, k
            do l = 1, k
                if(i >= j) then
                    a = i
                    b = j
                else
                    a = j
                    b = i
                end if
                t(1) = b
                t(2) = mod(a,t(1))
                if(t(2) /= 0) then
                    m = 2
                    do while (t(m) /= 0)
                        t(m+1) = mod(t(m-1),t(m))
                        m = m + 1
                    end do
                else
                    m = 2
                end if

                if(l >= t(m-1)) then
                    c = l
                    d = t(m-1)
                else
                    c = t(m-1)
                    d = l
                end if

                s(1) = d
                s(2) = mod(c,s(1))
                if(s(2) /= 0) then
                    m = 2
                    do while (s(m) /= 0)
                        s(m+1) = mod(s(m-1),s(m))
                        m = m + 1
                    end do
                else
                    m = 2
                end if
                
                sum = sum + s(m-1)
            
            end do
        end do
    end do
    
    write(*,*) sum

end program