import math

N = int(eval(input()))

all_gcd = [math.gcd(math.gcd(i, j), m) for i in range(1, N+1) for j in range(1, N+1) for m in range(1, N+1)]

print((sum(all_gcd)))

import math

N = int(eval(input()))

GCD = [[math.gcd(i, j) for i in range(1, N+1)] for j in range(1, N+1)]

#print(GCD)

all_gcd = [GCD[GCD[i][j]-1][m] for i in range(N) for j in range(N) for m in range(N)]

print((sum(all_gcd)))
import math

N = int(eval(input()))
all_gcd = [
    math.gcd(math.gcd(i, j), m)
    for i in range(1, N + 1)
    for j in range(1, N + 1)
    for m in range(1, N + 1)
]
print((sum(all_gcd)))

import math

N = int(eval(input()))
GCD = [[math.gcd(i, j) for i in range(1, N + 1)] for j in range(1, N + 1)]
# print(GCD)
all_gcd = [GCD[GCD[i][j] - 1][m] for i in range(N) for j in range(N) for m in range(N)]
print((sum(all_gcd)))

-all_gcd = [ -    math.gcd(math.gcd(i, j), m) -    for i in range(1, N + 1) -    for j in range(1, N + 1) -    for m in range(1, N + 1) -] +GCD = [[math.gcd(i, j) for i in range(1, N + 1)] for j in range(1, N + 1)] +# print(GCD) +all_gcd = [GCD[GCD[i][j] - 1][m] for i in range(N) for j in range(N) for m in range(N)]