function gcd2(a, b) {
  let m, n
  if (a >= b) {
    m = a
    n = b
  } else {
    m = b
    n = a
  }
  let bn = n
  n = m % n
  m = bn
  while(n !== 0) {
    bn = n
    n = m % n
    m = bn
  }
  return m
}
function gcd(a, b, c) {
  return gcd2(gcd2(a, b), c)
}

function main(input) {
  const K = Number(input)
  let sum = 0
  for(let i = 1; i <= K; i++) {
    for(let j = 1; j <= K; j++) {
      for(let k = 1; k <= K; k++) {
        // console.log(i, j, k, gcd(i, j, k))
        sum += gcd(i, j, k)
      }
    }
  }
  console.log(sum)
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));
