from math import gcd

import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines



def main():

    K = int(readline())



    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)





if __name__ == "__main__":

    main()

from math import gcd

import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines



K = int(readline())



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)
from math import gcd
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines


def main():
    K = int(readline())
    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)


if __name__ == "__main__":
    main()

from math import gcd
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
K = int(readline())
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 main(): -    K = int(readline()) -    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) - - -if __name__ == "__main__": -    main() +K = int(readline()) +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)