import sys

from math import gcd



sys.setrecursionlimit(10 ** 7)

rl = sys.stdin.readline





def solve():

    k = int(rl())

    ans = 0

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

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

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

                d = gcd(a, b)

                ans += gcd(c, d)

    print(ans)





if __name__ == '__main__':

    solve()

import sys

from math import gcd



sys.setrecursionlimit(10 ** 7)

rl = sys.stdin.readline





def solve():

    k = int(rl())

    ans = 0

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

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

            d = gcd(a, b)

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

                ans += gcd(c, d)

    print(ans)





if __name__ == '__main__':

    solve()

import sys
from math import gcd

sys.setrecursionlimit(10**7)
rl = sys.stdin.readline


def solve():
    k = int(rl())
    ans = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            for c in range(1, k + 1):
                d = gcd(a, b)
                ans += gcd(c, d)
    print(ans)


if __name__ == "__main__":
    solve()

import sys
from math import gcd

sys.setrecursionlimit(10**7)
rl = sys.stdin.readline


def solve():
    k = int(rl())
    ans = 0
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            d = gcd(a, b)
            for c in range(1, k + 1):
                ans += gcd(c, d)
    print(ans)


if __name__ == "__main__":
    solve()

+            d = gcd(a, b) -                d = gcd(a, b)