K = int(eval(input()))

s = 0



cache = {}





def gcd(a, b):

    key = (a, b)

    r = cache.get(key, None)

    if r:

        return r

    while b != 0:

        a, b = b, a % b

    cache[key] = a

    return a





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

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

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

            a, b, c = tuple(sorted([i, j, k]))

            s += gcd(a, gcd(b, c))



print(s)

import array



K = int(eval(input()))

s = 0



cache = [1] * K





def gcd(a, b):

    while b != 0:

        a, b = b, a % b

    return a





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

    cache[i-1] = [1] * K

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

        cache[i-1][j-1] = array.array('i', [1] * K)

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

            cache[i-1][j-1][k-1] = gcd(k, gcd(j, i))



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

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

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

            a, b, c = sorted([i, j, k])

            s += cache[a-1][b-1][c-1]



print(s)

K = int(eval(input()))
s = 0
cache = {}


def gcd(a, b):
    key = (a, b)
    r = cache.get(key, None)
    if r:
        return r
    while b != 0:
        a, b = b, a % b
    cache[key] = a
    return a


for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            a, b, c = tuple(sorted([i, j, k]))
            s += gcd(a, gcd(b, c))
print(s)

import array

K = int(eval(input()))
s = 0
cache = [1] * K


def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a


for i in range(1, K + 1):
    cache[i - 1] = [1] * K
    for j in range(i, K + 1):
        cache[i - 1][j - 1] = array.array("i", [1] * K)
        for k in range(j, K + 1):
            cache[i - 1][j - 1][k - 1] = gcd(k, gcd(j, i))
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            a, b, c = sorted([i, j, k])
            s += cache[a - 1][b - 1][c - 1]
print(s)

+import array + -cache = {} +cache = [1] * K -    key = (a, b) -    r = cache.get(key, None) -    if r: -        return r -    cache[key] = a +    cache[i - 1] = [1] * K +    for j in range(i, K + 1): +        cache[i - 1][j - 1] = array.array("i", [1] * K) +        for k in range(j, K + 1): +            cache[i - 1][j - 1][k - 1] = gcd(k, gcd(j, i)) +for i in range(1, K + 1): -            a, b, c = tuple(sorted([i, j, k])) -            s += gcd(a, gcd(b, c)) +            a, b, c = sorted([i, j, k]) +            s += cache[a - 1][b - 1][c - 1]