func gcd(_ a: Int, _ b: Int) -> Int {
    if a % b == 0 {
        return b
    }
    return gcd(b, a % b)
}

func main() {
    var sum = 0;
    let K = Int(readLine()!)!
    
    for a in 1...K {
        for b in 1...K {
            for c in 1...K {
                sum += gcd(gcd(a, b), c)
            }
        }
    }
    
    print(sum)
}

main()
