from math import gcd



K = int(eval(input()))



ans = 0



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

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

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

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



print(ans)
import sys

from math import gcd



def solve(K: int):



    ans = 0



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

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

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

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



    print(ans)



    return





def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    K = int(next(tokens))  # type: int

    solve(K)



if __name__ == '__main__':

    main()

from math import gcd

K = int(eval(input()))
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ans += gcd(gcd(i, j), k)
print(ans)

import sys
from math import gcd


def solve(K: int):
    ans = 0
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            for k in range(1, K + 1):
                ans += gcd(gcd(i, j), k)
    print(ans)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    K = int(next(tokens))  # type: int
    solve(K)


if __name__ == "__main__":
    main()

+import sys -K = int(eval(input())) -ans = 0 -for i in range(1, K + 1): -    for j in range(1, K + 1): -        for k in range(1, K + 1): -            ans += gcd(gcd(i, j), k) -print(ans) + +def solve(K: int): +    ans = 0 +    for i in range(1, K + 1): +        for j in range(1, K + 1): +            for k in range(1, K + 1): +                ans += gcd(gcd(i, j), k) +    print(ans) +    return + + +def main(): +    def iterate_tokens(): +        for line in sys.stdin: +            for word in line.split(): +                yield word + +    tokens = iterate_tokens() +    K = int(next(tokens))  # type: int +    solve(K) + + +if __name__ == "__main__": +    main()