import java.util.*

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val k = Integer.parseInt(scanner.next()!!)
    var sum = 0
    (1..k).forEach {a ->
        (1..k).forEach {b ->
            (1..k).forEach { c ->
                sum += gcd3(a,b,c)
            }
        }
    }
    println(sum)
}
tailrec fun gcd2(a: Int, b: Int): Int {
    if (b == 0) {
        return a
    }
    return gcd2(b, a % b)
}
fun gcd3(a: Int, b: Int, c: Int): Int {
    var ans = a
    listOf(b,c).forEach {
        ans = gcd2(ans, it)
    }
    return ans
}
