from math import gcd 

from functools import reduce

import itertools



def resolve():

  K = int(eval(input()))

  gcds = sum([ gcd(gcd(x[0], x[1]), x[2]) for x in itertools.product(list(range(1, K+1)), list(range(1, K+1)), list(range(1, K+1))) ])

  print(gcds)



if __name__ == "__main__":

    resolve()
from math import gcd

from functools import reduce

from itertools import product



def resolve():

  K = int(eval(input()))

  # result = sum([ gcd(gcd(x[0], x[1]), x[2]) for x in itertools.product(range(1, K+1), range(1, K+1), range(1, K+1)) ])

  step1 = [ gcd(x[0], x[1]) for x in product(list(range(1, K+1)), list(range(1, K+1))) ]

  result = sum([ gcd(x[0], x[1]) for x in product(step1, list(range(1, K+1))) ])

  print(result)



if __name__ == "__main__":

    resolve()
from math import gcd
from functools import reduce
import itertools


def resolve():
    K = int(eval(input()))
    gcds = sum(
        [
            gcd(gcd(x[0], x[1]), x[2])
            for x in itertools.product(
                list(range(1, K + 1)), list(range(1, K + 1)), list(range(1, K + 1))
            )
        ]
    )
    print(gcds)


if __name__ == "__main__":
    resolve()

from math import gcd
from functools import reduce
from itertools import product


def resolve():
    K = int(eval(input()))
    # result = sum([ gcd(gcd(x[0], x[1]), x[2]) for x in itertools.product(range(1, K+1), range(1, K+1), range(1, K+1)) ])
    step1 = [
        gcd(x[0], x[1]) for x in product(list(range(1, K + 1)), list(range(1, K + 1)))
    ]
    result = sum([gcd(x[0], x[1]) for x in product(step1, list(range(1, K + 1)))])
    print(result)


if __name__ == "__main__":
    resolve()

-import itertools +from itertools import product -    gcds = sum( -        [ -            gcd(gcd(x[0], x[1]), x[2]) -            for x in itertools.product( -                list(range(1, K + 1)), list(range(1, K + 1)), list(range(1, K + 1)) -            ) -        ] -    ) -    print(gcds) +    # result = sum([ gcd(gcd(x[0], x[1]), x[2]) for x in itertools.product(range(1, K+1), range(1, K+1), range(1, K+1)) ]) +    step1 = [ +        gcd(x[0], x[1]) for x in product(list(range(1, K + 1)), list(range(1, K + 1))) +    ] +    result = sum([gcd(x[0], x[1]) for x in product(step1, list(range(1, K + 1)))]) +    print(result)