fun main() = ABC162C.main()

object ABC162C {
    fun log(message: Any?) {
        //println(message)// TODO: ←消す
    }

    var gcdCount = 0
    fun main() {
        val K = readInt()
        /*
        val gcdTable = LazyMap<Set<Int>, Int> {
            val list = it.toList()
            if (this.containsKey(it)) get(it)
            else {
                when (it.size) {
                    1 -> list.first()
                    2 -> gcd(list[0], list[1])
                    else -> get(setOf(list[0], get(setOf(list[1], list[2]))))
                }
            }
        }

        val combinations =
            ((1..K).combination(3).map { it.toSet() } + (1..K).combination(2).map { it.toSet() } + (1..K).combination(1)
                .map { it.toSet() })
        //log(combinations.joinToString("\n"))
        val ans = when (K) {
            else -> combinations.map {
                when (it.size) {
                    1 ->  {
                        //log("$it: +${gcdTable[it]}")
                        gcdTable[it]
                    }
                    2 -> {
                        //log("$it: +${gcdTable[it]}*6")
                        gcdTable[it] * 6
                    }
                    else -> {
                        //log("$it: +${gcdTable[it]}*6")
                        gcdTable[it] * 6
                    }
                }
            }.sum()
        }
        */
        var ans = 0
        (1..K).forEach { a->
            (1..K).forEach { b->
                (1..K).forEach { c->
                    ans += gcd(a,gcd(b,c))
                }
            }
        }

        println(ans)
        log("gcdCount=$gcdCount")
    }


    /*####################################################################################*/
    fun gcd(a: Int, b: Int): Int {
        var (m, n) = if (a > b) listOf(a, b) else listOf(b, a)
        while (n != 0) {
            val r = m % n
            m = n
            n = r
        }
        gcdCount++
        return m
    }

    class LazyMap<K, V>(private val init: LazyMap<K, V>.(K) -> V) : Map<K, V> {
        private val memo = HashMap<K, V>()
        override fun get(key: K): V {
            return memo[key] ?: init(key).memorizingAt(key)
        }


        private fun V.memorizingAt(key: K): V {
            memo[key] = this
            return this
        }

        override val entries: Set<Map.Entry<K, V>>
            get() = memo.entries
        override val keys: Set<K>
            get() = memo.keys
        override val size: Int
            get() = memo.size
        override val values: Collection<V>
            get() = memo.values

        override fun containsKey(key: K): Boolean = memo.containsKey(key)
        override fun containsValue(value: V): Boolean = memo.containsValue(value)
        override fun isEmpty(): Boolean = memo.isEmpty()
    }

    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map { it.toInt() }


    class Combination<T> : BasicCombinator<T>() {
        override fun List<T>.chooseRemains(i: Int): List<T> = this.drop(i + 1)
    }

    fun IntRange.combination(k: Int): Sequence<List<Int>> = this.toList().combination(k)
    fun <T> List<T>.combination(k: Int): Sequence<List<T>> = Combination<T>().generate(this, k)

    abstract class BasicCombinator<T> {
        abstract fun List<T>.chooseRemains(i: Int): List<T>

        fun generate(items: List<T>, k: Int): Sequence<List<T>> {
            when {
                (k < 0) ->
                    throw IllegalArgumentException()
                (k == 0) ->
                    return emptySequence()
                (k == 1) ->
                    return items.asSequence().map { listOf(it) }
                else ->
                    return items.asSequence().mapIndexed { index, first ->
                        val remains = items.chooseRemains(index)
                        generate(remains, k - 1).map { first + it }
                    }.flatten()
            }
        }

        private operator fun T.plus(other: List<T>): List<T> {
            val list = mutableListOf(this)
            list.addAll(other)
            return list
        }
    }
}

Measured memory_bytes: 55544