from sys import stdin

import math

from functools import reduce



def main():



    input = stdin.readline



    K = int(eval(input()))



    ans_sum = 0



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

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

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

                check = [i,j,k]



                sum_type = len(set(check))

                if sum_type == 1:

                    ans_sum += gcd(i,j,k)

                elif sum_type == 2:

                    ans_sum += gcd(i,j,k) * 3

                else:

                    ans_sum += gcd(i,j,k) * 6



    print(ans_sum)





def gcd(*numbers):

    return reduce(math.gcd, numbers)





if __name__ == "__main__":

    main()
import math

K = int(eval(input()))

ans = 0

 

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

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

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

            ans += math.gcd(math.gcd(a,b),c)

print(ans)
from sys import stdin
import math
from functools import reduce


def main():
    input = stdin.readline
    K = int(eval(input()))
    ans_sum = 0
    for i in range(1, K + 1):
        for j in range(i, K + 1):
            for k in range(j, K + 1):
                check = [i, j, k]
                sum_type = len(set(check))
                if sum_type == 1:
                    ans_sum += gcd(i, j, k)
                elif sum_type == 2:
                    ans_sum += gcd(i, j, k) * 3
                else:
                    ans_sum += gcd(i, j, k) * 6
    print(ans_sum)


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


if __name__ == "__main__":
    main()

import math

K = int(eval(input()))
ans = 0
for a in range(1, K + 1):
    for b in range(1, K + 1):
        for c in range(1, K + 1):
            ans += math.gcd(math.gcd(a, b), c)
print(ans)

-from sys import stdin -from functools import reduce - -def main(): -    input = stdin.readline -    K = int(eval(input())) -    ans_sum = 0 -    for i in range(1, K + 1): -        for j in range(i, K + 1): -            for k in range(j, K + 1): -                check = [i, j, k] -                sum_type = len(set(check)) -                if sum_type == 1: -                    ans_sum += gcd(i, j, k) -                elif sum_type == 2: -                    ans_sum += gcd(i, j, k) * 3 -                else: -                    ans_sum += gcd(i, j, k) * 6 -    print(ans_sum) - - -def gcd(*numbers): -    return reduce(math.gcd, numbers) - - -if __name__ == "__main__": -    main() +K = int(eval(input())) +ans = 0 +for a in range(1, K + 1): +    for b in range(1, K + 1): +        for c in range(1, K + 1): +            ans += math.gcd(math.gcd(a, b), c) +print(ans)