import Foundation

func gcd(_ A: Int, _ B: Int) -> Int {
  var a = A, b = B
  while b > 0 {
    var r = a % b; a = b; b = r
  }
  return a
}

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