object Main extends App {
    val k = scala.io.StdIn.readInt
    var output = 0
    for(count1 <- 1 to k){
        for(count2 <- 1 to k){
            for(count3 <- 1 to k){
                output = output + gcd(count1, gcd(count2, count3))
            }
        }
    }
    println(output)
    def gcd(x: Int, y: Int): Int = {
        if(y == 0) x
        else gcd(y, (x % y))
    }
}