import scala.annotation.tailrec

object Main extends App {
  @tailrec
  def gcd(n: Int, m: Int): Int = {
    if (n < m) gcd(m, n)
    else {
      val r = n % m
      if (r > 0) gcd(m, r)
      else m
    }
  }

  var sum = 0
  val k = io.StdIn.readInt()
  for (a <- 1 to k)
    for (b <- 1 to k)
      for (c <- 1 to k)
        sum += gcd(a, gcd(b, c))

  println(sum)
}