
import kotlin.math.max
import kotlin.math.min


fun main(args: Array<String>) {

    val k = readLine()!!.toLong()

    var sum = 0L
    (1..k).forEach { a ->
        (1..k).forEach { b ->
            (1..k).forEach { c ->
                sum += gcd(gcd(a, b), c)
            }
        }
    }

    println(sum)
}

private val map = mapOf<Pair<Long, Long>, Long>()
private fun gcd(m: Long, n: Long): Long {
    val key = min(m, n) to max(m, n)
    return map.getOrElse(key, {
        when {
            m < n -> gcd(n, m)
            n == 0L -> m
            else -> gcd(n, m % n)
        }
    })
}