import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val K = sc.nextInt()

  @scala.annotation.tailrec
  def gcd(x: Int, y: Int): Int =
    if (x % y != 0) gcd(y, x % y)
    else y

  var x = 0
  for (a <- 1 to K) {
    for (b <- 1 to K) {
      for (c <- 1 to K) {
        x += gcd(gcd(a, b), c)
      }
    }
  }

  println(x)
}
