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

func gcda(_ s: [Int]) -> Int {
    guard !s.isEmpty else { return 0 }
    guard s.count > 1 else { return s.first! }

    var tmpS = s
    let first = tmpS.removeFirst()
    return tmpS.reduce(first) { gcd($0, $1) }
}

func memoize(_ f: @escaping ([Int]) -> Int) -> ([Int]) -> Int {
    var dic = [[Int] : Int]()
    return { nums in
        if let value = dic[nums.sorted()] {
            return value
        } else {
            let result = f(nums)
            dic[nums] = result
            return result
        }
    }
}

let m_gcda = memoize(gcda)

let k = Int(readLine()!)!

var results = [Int]()
for a in 1...k {
    for b in 1...k {
        for c in 1...k {
            results.append(gcda([a, b, c]))
        }
    }
}

let sum = results.reduce(into: 0) { $0 += $1 }
print(sum)
