from numba import njit

from sys import stdin





@njit(cache=True)

def gcd(x, y):

    if x % y == 0:

        return y

    return gcd(y, x % y)





@njit(cache=True)

def fact(x):

    if x == 1:

        return x

    return x * fact(x - 1)





def main():

    k = int(stdin.readline())



    ans = 0



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

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

            d = gcd(a, b)

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

                e = gcd(d, c)

                ans += e * fact(3) // fact(3 - len(set([a, b, c])) + 1)



    print(ans)





if __name__ == "__main__":

    main()

from sys import stdin, setrecursionlimit





def gcd(x, y):

    if x % y == 0:

        return y

    return gcd(y, x % y)





def main():

    input = stdin.buffer.readline

    k = int(eval(input()))

    g = []

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

        gi = []

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

            gi.append(gcd(i, j))

        g.append(gi)

    ans = 0

    for a in range(k):

        for b in range(k):

            for c in range(k):

                tmp = g[a][b] - 1

                ans += g[tmp][c]

    print(ans)





if __name__ == "__main__":

    setrecursionlimit(10000)

    main()

from numba import njit
from sys import stdin


@njit(cache=True)
def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


@njit(cache=True)
def fact(x):
    if x == 1:
        return x
    return x * fact(x - 1)


def main():
    k = int(stdin.readline())
    ans = 0
    for a in range(1, k + 1):
        for b in range(a, k + 1):
            d = gcd(a, b)
            for c in range(b, k + 1):
                e = gcd(d, c)
                ans += e * fact(3) // fact(3 - len(set([a, b, c])) + 1)
    print(ans)


if __name__ == "__main__":
    main()

from sys import stdin, setrecursionlimit


def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


def main():
    input = stdin.buffer.readline
    k = int(eval(input()))
    g = []
    for i in range(1, k + 1):
        gi = []
        for j in range(1, k + 1):
            gi.append(gcd(i, j))
        g.append(gi)
    ans = 0
    for a in range(k):
        for b in range(k):
            for c in range(k):
                tmp = g[a][b] - 1
                ans += g[tmp][c]
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

-from numba import njit -from sys import stdin +from sys import stdin, setrecursionlimit -@njit(cache=True) -@njit(cache=True) -def fact(x): -    if x == 1: -        return x -    return x * fact(x - 1) - - -    k = int(stdin.readline()) +    input = stdin.buffer.readline +    k = int(eval(input())) +    g = [] +    for i in range(1, k + 1): +        gi = [] +        for j in range(1, k + 1): +            gi.append(gcd(i, j)) +        g.append(gi) -    for a in range(1, k + 1): -        for b in range(a, k + 1): -            d = gcd(a, b) -            for c in range(b, k + 1): -                e = gcd(d, c) -                ans += e * fact(3) // fact(3 - len(set([a, b, c])) + 1) +    for a in range(k): +        for b in range(k): +            for c in range(k): +                tmp = g[a][b] - 1 +                ans += g[tmp][c] +    setrecursionlimit(10000)