import Foundation

let main: () = {
    let K = getInt()
    var ans = 0
    for a in 1...K {
    for b in 1...K {
    for c in 1...K {
        ans += gcd(gcd(a, b), c)
    }}}
    print(ans)
}()

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

func getString() -> String { readLine()! }
func getInt() -> Int { Int(getString())! }
func getDouble() -> Double { Double(getString())! }

