fun main(args: Array<String>) {
  val K = readInt()
  var sum = 0L
  for (i in 1..K) {
    for (j in 1..K) {
      for (k in 1..K) {
        val list = listOf(i, j, k).sorted()
        val a = list[0]
        val b = list[1]
        val c = list[2]
        val gcdAB = gcd(a, b)
        val gcdAll = gcd(gcdAB, c)
        sum += gcdAll
      }
    }
  }
  println(sum)
}

fun gcd(a: Int, b: Int): Int {
  if (a < b) return gcd(b, a)
  if (b == 0) return a
  return gcd(b, a % b)
}

fun readString() = readLine()!!
fun readStrings() = readString().split(" ")
fun readInt() = readString().toInt()
fun readInts() = readStrings().map { it.toInt() }
fun readLong() = readString().toLong()
fun readLongs() = readStrings().map { it.toLong() }
