import math

# from itertools import product

from functools import reduce



def main():

#     def gcd(*numbers): return reduce(math.gcd, numbers)

    K = int(eval(input()))

    ans = 0

#     valiation = list(itertools.product(range(1, K+1), repeat=3))

#     for v in valiation:

#         ans += gcd(v)

    for i in range(1, K+1):

        for j in range(1, K+1):

            for k in range(1, K+1):

                ans += math.gcd(math.gcd(i, j), k)

    print(ans)

        

    

if __name__ == '__main__':

    main()
import math

# from itertools import product

from functools import reduce



def main():

#     def gcd(*numbers): return reduce(math.gcd, numbers)

    K = int(eval(input()))

    ans = 0

#     valiation = list(itertools.product(range(1, K+1), repeat=3))

#     for v in valiation:

#         ans += gcd(v)

    for i in range(1, K+1):

        for j in range(i, K+1):

            for k in range(j, K+1):

                x = math.gcd(math.gcd(i, j), k)

                if i == j == k:

                    ans += x

                elif i != j and j != k and k != i:

                    ans += 6 * x

                else:

                    ans += 3 * x

    print(ans)

        

    

if __name__ == '__main__':

    main()
import math

# from itertools import product
from functools import reduce


def main():
    #     def gcd(*numbers): return reduce(math.gcd, numbers)
    K = int(eval(input()))
    ans = 0
    #     valiation = list(itertools.product(range(1, K+1), repeat=3))
    #     for v in valiation:
    #         ans += gcd(v)
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                ans += math.gcd(math.gcd(i, j), k)
    print(ans)


if __name__ == "__main__":
    main()

import math

# from itertools import product
from functools import reduce


def main():
    #     def gcd(*numbers): return reduce(math.gcd, numbers)
    K = int(eval(input()))
    ans = 0
    #     valiation = list(itertools.product(range(1, K+1), repeat=3))
    #     for v in valiation:
    #         ans += gcd(v)
    for i in range(1, K + 1):
        for j in range(i, K + 1):
            for k in range(j, K + 1):
                x = math.gcd(math.gcd(i, j), k)
                if i == j == k:
                    ans += x
                elif i != j and j != k and k != i:
                    ans += 6 * x
                else:
                    ans += 3 * x
    print(ans)


if __name__ == "__main__":
    main()

-        for j in range(1, K + 1): -            for k in range(1, K + 1): -                ans += math.gcd(math.gcd(i, j), k) +        for j in range(i, K + 1): +            for k in range(j, K + 1): +                x = math.gcd(math.gcd(i, j), k) +                if i == j == k: +                    ans += x +                elif i != j and j != k and k != i: +                    ans += 6 * x +                else: +                    ans += 3 * x