val h: MutableMap<Pair<Long, Long>, Long> = mutableMapOf()

fun main(args: Array<String>) {
    val line = readLine()!!.trim()
    val n = line.toLong()

    var result: Long = 0L

    for (a: Long in 1..n)  {
        for (b: Long in 1..n)  {
            for (c: Long in 1..n) {
                result += gcd3(a, b, c)
            }
        }
    }

    println("${result}")
}


fun gcd3(a: Long, b: Long, c: Long): Long {
    return gcdWithCache(c, gcdWithCache(a, b))
}

fun gcdWithCache(a: Long, b: Long): Long {
    val (a_, b_) = sortKey(a, b)
    val key = Pair(a_, b_)
    if (h.containsKey(key)) {
        return h.get(key)!!
    }

    val result = gcdSub(a_, b_)
    h.put(key, result)
    return result
}

fun sortKey(a: Long, b: Long): Pair<Long, Long> {
    if (a < b) {
        return Pair(a ,b)
    }

    return Pair(b, a)
}

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