import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader

fun main() {
    val k = readInt()
    var summation = 0L

    fun gcd(a: Long, b: Long): Long {
        if (b == 0L) return a
        return gcd(b, a % b)
    }

    for (i in 1..k) {
        for (j in 1..k) {
            for(l in 1..k) {
                summation += gcd(gcd(i.toLong(), j.toLong()), l.toLong())
            }
        }
    }

    println(summation)
}

// Input Reader
private fun readLn() = readLine()!!
// Read from file
//private val inputReader = InputStreamReader(FileInputStream(File("input.txt")))
//private val lines = inputReader.readLines()
//private var currLine = -1
//private fun readLn(): String {
//    currLine++
//    return lines[currLine]
//}

private fun readStrings() = readLn().trim().split(" ")
private fun readInt() = readLn().toInt()
private fun readInts() = readStrings().map { it.toInt() }
