fun main(args: Array<String>) {
    val k = readLine()!!.split(" ").map { it.toInt() }[0]
    var count = 0
    for (a in 1..k){
        for (b in 1..k){
            var gcdAandB = 0
            var x = a
            var y = b
            for (i in 0..300){
                if (x == 0){
                    gcdAandB = y
                    break
                } else if (y == 0){
                    gcdAandB = x
                    break
                } else if (x > y){
                    x = x % y
                } else if(x == y){
                    gcdAandB = x
                    break
                } else {
                    y = y % x
                }
            }
            for (c in 1..k){
                var z = c
                var xy = gcdAandB
                for (i in 0..300){
                    if (z == 0){
                        count += xy
                        break
                    } else if (xy == 0) {
                        count += z
                        break
                    } else if (z > xy){
                        z = z % xy
                    } else if (z == xy){
                        count += z
                        break
                    } else {
                        xy = xy % z
                    }
                }
            }
        }
    }
    println(count)
}