CodeEff/ECCO huggingface dataset:
# Original solution


import sys

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction
from math import gcd
# fractions.gcd() を用いること
# math.log2()はatcoderでは対応していない．留意せよ．

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


def lcm(n, m):
    return int(n * m / gcd(n, m))


def coprimize(p, q):
    common = fractions.gcd(p, q)
    return (p // common, q // common)


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, list(range(n, n - r, -1)), 1)
    denom = reduce(mul, list(range(1, r + 1)), 1)
    return numer // denom


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


def main():
    k = int(input().strip())
    ans=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for k in range(1,k+1):
                ans += find_gcd([i,j,k])
    print(ans)


if __name__ == '__main__':
    main()

# Optimized solution


import sys
from math import gcd
from functools import reduce

input = sys.stdin.readline


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


def main():
    k = int(input().strip())
    ans=0
    for i in range(1,k+1):
        for j in range(1,k+1):
            for k in range(1,k+1):
                ans += find_gcd([i,j,k])
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
#ABC162c
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(eval(input()))
abc = 0

for i in range(1,k+1):
    for i2 in range(1,k+1):
        ab = gcd(i,i2)
        for i3 in range(1,k+1):
            abc += gcd(ab, i3)
print(abc)


# Optimized solution
#ABC162c
from math import gcd
K = int(eval(input()))+1
Sum = 0
for i in range(1,K):
    for j in range(1,K):
        d = gcd(i,j)
        for k in range(1,K):
            Sum+=gcd(d,k)

print(Sum)
# Original solution
from math import gcd
K=int(eval(input()))
a=0
for i in range(1,K+1):
    for j in range(1,K+1):
        t=gcd(i,j)
        for k in range(1,K+1):
            a+=gcd(t,k)
print(a)


# Optimized solution
K=int(eval(input()))
N=3
pr=10**9+7
a=0
l=[None]*(K+1)
for i in range(K,0,-1):
    t=pow(K//i,N,pr)
    c=i*2
    while c<=K:
        t-=l[c]
        c+=i
    l[i]=t
    a=(a+t*i)%pr
print(a)
# Original solution
from math import gcd
from functools import reduce
k = int(eval(input()))

l = [[a,b,c] for a in range(1,k+1) for b in range(1,k+1) for c in range(1,k+1)]
#print(sum(reduce(gcd,l)))
ans = 0
for i in l:
  ans += reduce(gcd,i)
print(ans)

# Optimized solution
from math import gcd
k = int(eval(input()))

ans = 0
for i in range(1, k+1):
  for j in range(1, k+1):
    x = gcd(i,j)
    for l in range(1,k+1):
      ans += gcd(x,l)
print(ans)
# Original solution
import math

K = int(eval(input()))

sum = 0
for a in range(K):
  for b in range(K):
    for c in range(K):
      sum += math.gcd(math.gcd(a+1, b+1), c+1)
      
print(sum)

# Optimized solution
import math

K = int(eval(input()))

sum = 0
gcd = 0
for a in range(K):
  for b in range(K):
    gcd = math.gcd(a+1, b+1)
    for c in range(K):
      sum += math.gcd(gcd, c+1)
      
print(sum)
# Original solution
import math
from functools import reduce
from itertools import product

def gcd_(*numbers):
    return reduce(math.gcd, numbers)

K = int(eval(input()))

ans = 3*K*(K-1) + 1

for i,j,k in product(list(range(2,K+1)),list(range(2,K+1)),list(range(2,K+1))):
    ans += gcd_(i,j,k)

print(ans)

# Optimized solution
import math
from functools import reduce
from itertools import product,combinations_with_replacement

def gcd_(*numbers):
    return reduce(math.gcd, numbers)

K = int(eval(input()))

ans = 3*K*(K-1) + 1

# for i,j,k in product(range(2,K+1),repeat=3):
#     ans += gcd_(i,j,k)
for i,j,k in combinations_with_replacement(list(range(2,K+1)),3):
    if i==j and j==k:
        ans += i
    elif i==j or j==k or k==i:
        ans += gcd_(i,j,k) * 3
    else:
        ans += gcd_(i,j,k)*6
# for i in range(2,K+1):
#     for j in range(2,K+1):
#         for k in range(2,K+1):
#             ans += gcd_(i,j,k)

print(ans)
# Original solution
import math
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
def gcd_list(numbers):
    return reduce(math.gcd, numbers)

t = 0
k= int(eval(input('')))+1

cnt={}

for l in range(1,k):
    for m in range(l,k):
        for n in range(m,k):
#            print("{}/{}/{}".format(l,m,n))
            key = '-'.join(map(str,sorted([l,m,n])))
            if not key in list(cnt.keys()):
                cnt[key] = 0
            cnt[key]+=1
            if l==m and l==n:
                t+= gcd_list([l,m,n])
#                print("{}/{}/{} -> {} a".format(l,m,n,gcd(l,m,n)))
            elif l==m or l==n or m ==n:
                t+= gcd_list([l,m,n])*3
#                print("{}/{}/{} -> {} b".format(l,m,n,gcd(l,m,n)))
            elif l!=m and m!=n:
                t+= gcd_list([l,m,n])*6
#                print("{}/{}/{} -> {} c".format(l,m,n,gcd(l,m,n)))

#for a,b in cnt.items():
#    print("{} -> {}".format(a,b))
print(t)


# Optimized solution
import math
from functools import reduce
 
def gcd(*numbers):
    return reduce(math.gcd, numbers)
 
def gcd_list(numbers):
    return reduce(math.gcd, numbers)

t = 0
k= int(eval(input('')))+1

#cnt={}

for l in range(1,k):
    for m in range(l,k):
        for n in range(m,k):
#            print("{}/{}/{}".format(l,m,n))
#            key = '-'.join(map(str,sorted([l,m,n])))
#            if not key in cnt.keys():
#                cnt[key] = 0
#            cnt[key]+=1
            if l==m and l==n:
                t+= gcd_list([l,m,n])
#                print("{}/{}/{} -> {} a".format(l,m,n,gcd(l,m,n)))
            elif l==m or l==n or m ==n:
                t+= gcd_list([l,m,n])*3
#                print("{}/{}/{} -> {} b".format(l,m,n,gcd(l,m,n)))
            elif l!=m and m!=n:
                t+= gcd_list([l,m,n])*6
#                print("{}/{}/{} -> {} c".format(l,m,n,gcd(l,m,n)))

#for a,b in cnt.items():
#    print("{} -> {}".format(a,b))
print(t)
# Original solution
K=int(eval(input()))
def gcd(a,b,c):
    if a <b:
        a,b=b,a
    while a%b != 0:
        a,b= b,a%b
    if b < c:
        b,c=c,b
    while b % c != 0:
        b,c = c,b%c
    return c
x=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
            if (i==1) or (j==1) or (l==1):
                x+=1
            elif (j%i==i) or (l%j==j) or(l%i==i):
                x+=1
            else:
                x += gcd(i,j,l)
print(x)


# Optimized solution
K=int(eval(input()))
def gcd(a,b,c):
    if a <b:
        a,b=b,a
    while a%b != 0:
        a,b= b,a%b
    if b < c:
        b,c=c,b
    while b % c != 0:
        b,c = c,b%c
    return c
x=0
for i in range(1,K+1):
    for j in range(1,K+1):

            for l in range(1,K+1):
                
                    if (i==1) or (j==1) or (l==1):
                        x+=1
                    else:
                        x += gcd(i,j,l)
print(x)
# Original solution
import itertools
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(eval(input()))

la = []
l = list(range(1, k+1))
for a, b, c in itertools.product(l, l, l):
    la.append(gcd(a, b, c))

print((sum(la)))

# Optimized solution
k = int(eval(input()))

ans = [
1,
9,
30,
76,
141,
267,
400,
624,
885,
1249,
1590,
2208,
2689,
3411,
4248,
5248,
6081,
7485,
8530,
10248,
11889,
13687,
15228,
17988,
20053,
22569,
25242,
28588,
31053,
35463,
38284,
42540,
46581,
50893,
55362,
61824,
65857,
71247,
76884,
84388,
89349,
97881,
103342,
111528,
120141,
128047,
134580,
146316,
154177,
164817,
174438,
185836,
194157,
207927,
218812,
233268,
245277,
257857,
268182,
288216,
299257,
313635,
330204,
347836,
362973,
383709,
397042,
416448,
434025,
456967,
471948,
499740,
515581,
536073,
559758,
583960,
604833,
633651,
652216,
683712,
709065,
734233,
754734,
793188,
818917,
846603,
874512,
909496,
933081,
977145,
1006126,
1041504,
1073385,
1106467,
1138536,
1187112,
1215145,
1255101,
1295142,
1342852,
1373253,
1422195,
1453816,
1502376,
1553361,
1595437,
1629570,
1691292,
1726717,
1782111,
1827492,
1887772,
1925853,
1986837,
2033674,
2089776,
2145333,
2197483,
2246640,
2332104,
2379085,
2434833,
2490534,
2554600,
2609625,
2693919,
2742052,
2813988,
2875245,
2952085,
3003306,
3096024,
3157249,
3224511,
3306240,
3388576,
3444609,
3533637,
3591322,
3693924,
3767085,
3842623,
3912324,
4027884,
4102093,
4181949,
4270422,
4361548,
4427853,
4548003,
4616104,
4718640,
4812789,
4918561,
5003286,
5131848,
5205481,
5299011,
5392008,
5521384,
5610705,
5739009,
5818390,
5930196,
6052893,
6156139,
6239472,
6402720,
6493681,
6623853,
6741078,
6864016,
6953457,
7094451,
7215016,
7359936,
7475145,
7593865,
7689630,
7886244,
7984165,
8130747,
8253888,
8403448,
8523897,
8684853,
8802826,
8949612,
9105537,
9267595,
9376656,
9574704,
9686065,
9827097,
9997134,
10174780,
10290813,
10493367,
10611772,
10813692
]

print((ans[k-1]))
# Original solution
def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)


k = int(eval(input()))
sum = 0

for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            sum += gcd(gcd(i, j), l)

print(sum)

# Optimized solution
def gcd(a, b):
    if a % b == 0:
        return b
    return gcd(b, a % b)


k = int(eval(input()))

sum = 0

# gcd(a,a,a)
sum += int((1+k)*k/2)

# gcd(a,b,b)
if k >= 2:
    for i in range(1, k):
        for j in range(i+1, k+1):
            sum += gcd(i, j)*6

# gcd(a,b,c)
if k >= 3:
    for i in range(1, k-1):
        for j in range(i+1, k):
            for l in range(j+1, k+1):
                sum += gcd(gcd(i, j), l)*6

print(sum)
# Original solution
import math
from functools import reduce

K = int(eval(input()))

def gcd(*numbers):
    return reduce(math.gcd, numbers)

ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
            ans += gcd(i,j,l)
            
print(ans)

# Optimized solution
import math

K = int(eval(input()))



ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
            ans += math.gcd(math.gcd(i,j),l)
            
print(ans)
# Original solution
import math
k = int(eval(input()))

ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for s in range(1,k+1):
            ans += math.gcd(math.gcd(i,j),s)

print(ans)

# Optimized solution
k = int(eval(input()))

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

for i in range(1,k+1):
    for j in range(1,k+1):
        for s in range(1,k+1):
            ans += gcd(gcd(i,j),s)

print(ans)
# Original solution
k = int(eval(input()))

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

for i in range(1,k+1):
    for j in range(1,k+1):
        for s in range(1,k+1):
            ans += gcd(gcd(i,j),s)

print(ans)


# Optimized solution
import math
k = int(eval(input()))

ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for s in range(1,k+1):
            ans += math.gcd(math.gcd(i,j),s)

print(ans)
# Original solution
K = int(eval(input()))
import math

def gcd_three(a, b, c):
    r = math.gcd(a, b)
    if( r == 1 ):
        return 1
    else:
        return math.gcd(r, c)
sum = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            sum += gcd_three(a,b,c)
print(sum)

# Optimized solution
K = int(eval(input()))
import math
def gcd_three(a, b, c):
    r = math.gcd(a, b)
    if( r == 1 ):
        return 1
    else:
        return math.gcd(r, c)
sum = 0
if(K>2):
    for a in range(1, K-1):
        for b in range(a+1, K):
            for c in range(b+1, K+1):
                sum += gcd_three(a,b,c)*6
for a in range(1, K):
    for b in range(a+1, K+1):
        sum += math.gcd(a,b)*6
sum += K*(K+1)//2
print(sum)
# Original solution
import math
from functools import reduce
import itertools

K = int(eval(input()))
count = 0
for abc in list(itertools.product(list(range(1, K+1)), repeat=3)):
    count += reduce(math.gcd, abc)

print(count)

# Optimized solution
import math

count = 0

K = int(eval(input()))
for a in range(1, K+1):
    for b in range(1, K+1):
        temp = math.gcd(a, b)
        for c in range(1, K+1):
            count += math.gcd(temp, c)

print(count)
# Original solution
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(eval(input()))
ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            ans += gcd(i,j,l)
print(ans)

# Optimized solution
n = int(eval(input()))
import math
l = []
for i in range(1,n +1):
    for j in range(1,n+1):
        l.append(math.gcd(i,j))
ans = 0
for i in range(1, n+1):
    for j in l:
        ans += math.gcd(j,i)
print(ans)
# Original solution
import sys
input=sys.stdin.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
import math
#import collections
#from collections import deque
#from bisect import bisect_left
K = int(eval(input()))
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
s = 0
d = 0
e = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            d = math.gcd(c,b)
            e = math.gcd(a,d)
            s += e
print(s)

# Optimized solution
import sys
input=sys.stdin.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
import math
#import collections
#from collections import deque
#from bisect import bisect_left
K = int(eval(input()))
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
s = 0
d = 0
e = 0
for a in range(1,K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            d = math.gcd(c,b)
            e = math.gcd(a,d)
            if a == b and b == c:
                s += e
            elif a != b and b != c :
                s += 6 * e
            else:
                s += 3 * e
print(s)
# Original solution
import math
from functools import reduce

K=int(eval(input()))

def gcd(*numbers):
    return reduce(math.gcd, numbers)
sum = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            sum += gcd(a, b, c)

print(sum)

# Optimized solution
K=int(eval(input()))

cache = [1, 
9, 
30, 
76, 
141, 
267, 
400, 
624, 
885, 
1249, 
1590, 
2208, 
2689, 
3411, 
4248, 
5248, 
6081, 
7485, 
8530, 
10248, 
11889, 
13687, 
15228, 
17988, 
20053, 
22569, 
25242, 
28588, 
31053, 
35463, 
38284, 
42540, 
46581, 
50893, 
55362, 
61824, 
65857, 
71247, 
76884, 
84388, 
89349, 
97881, 
103342, 
111528, 
120141, 
128047, 
134580, 
146316, 
154177, 
164817, 
174438, 
185836, 
194157, 
207927, 
218812, 
233268, 
245277, 
257857, 
268182, 
288216, 
299257, 
313635, 
330204, 
347836, 
362973, 
383709, 
397042, 
416448, 
434025, 
456967, 
471948, 
499740, 
515581, 
536073, 
559758, 
583960, 
604833, 
633651, 
652216, 
683712, 
709065, 
734233, 
754734, 
793188, 
818917, 
846603, 
874512, 
909496, 
933081, 
977145, 
1006126, 
1041504, 
1073385, 
1106467, 
1138536, 
1187112, 
1215145, 
1255101, 
1295142, 
1342852, 
1373253, 
1422195, 
1453816, 
1502376, 
1553361, 
1595437, 
1629570, 
1691292, 
1726717, 
1782111, 
1827492, 
1887772, 
1925853, 
1986837, 
2033674, 
2089776, 
2145333, 
2197483, 
2246640, 
2332104, 
2379085, 
2434833, 
2490534, 
2554600, 
2609625, 
2693919, 
2742052, 
2813988, 
2875245, 
2952085, 
3003306, 
3096024, 
3157249, 
3224511, 
3306240, 
3388576, 
3444609, 
3533637, 
3591322, 
3693924, 
3767085, 
3842623, 
3912324, 
4027884, 
4102093, 
4181949, 
4270422, 
4361548, 
4427853, 
4548003, 
4616104, 
4718640, 
4812789, 
4918561, 
5003286, 
5131848, 
5205481, 
5299011, 
5392008, 
5521384, 
5610705, 
5739009, 
5818390, 
5930196, 
6052893, 
6156139, 
6239472, 
6402720, 
6493681, 
6623853, 
6741078, 
6864016, 
6953457, 
7094451, 
7215016, 
7359936, 
7475145, 
7593865, 
7689630, 
7886244, 
7984165, 
8130747, 
8253888, 
8403448, 
8523897, 
8684853, 
8802826, 
8949612, 
9105537, 
9267595, 
9376656, 
9574704, 
9686065, 
9827097, 
9997134, 
10174780, 
10290813, 
10493367, 
10611772, 
10813692, 
10962213]

print((cache[K-1]))
# Original solution
import math
from functools import reduce

k = int(eval(input()))
ans = 0

def gcd(*numbers):
    return reduce(math.gcd, numbers)

for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            ans += gcd(a,b,c)

print(ans)

# Optimized solution
import math

k = int(eval(input()))

box = [0]*(k+1)
ans = 0

for a in range(1,k+1):
    for b in range(1,k+1):
        box[math.gcd(a,b)] += 1

for a in range(1,k+1):
    for c in range(1,k+1):
        ans += math.gcd(a,c)*box[a]

print(ans)
# Original solution
MAX_K: int = 200

gt = [[0 for _ in range(MAX_K + 1)] for _ in range(MAX_K + 1)]

def gcd(x: int, y: int) -> int:
    ret: int = 0
    if x < y:
        ret = gcd(y, x)
    elif y == 0:
        ret = x
    elif y == 1:
        ret = 1
    else:
        ret = gcd(y, x % y)
    gt[x][y] = ret
    # print(f"gcd({x}, {y}) = {ret}")
    return ret

k = int(eval(input()))
print((sum([gcd(gcd(i + 1, j + 1), l + 1) for i in range(k) for j in range(k) for l in range(k)])))


# Optimized solution
MAX_K: int = 200

gt = [[0 for _ in range(MAX_K + 1)] for _ in range(MAX_K + 1)]

def gcd(x: int, y: int) -> int:
    if gt[x][y] > 0:
        return gt[x][y]
    ret: int = 0
    if x < y:
        ret = gcd(y, x)
    elif y == 0:
        ret = x
    elif y == 1:
        ret = 1
    else:
        ret = gcd(y, x % y)
    gt[x][y] = ret
    # print(f"gcd({x}, {y}) = {ret}")
    return ret

k = int(eval(input()))
print((sum([gcd(gcd(i + 1, j + 1), l + 1) for i in range(k) for j in range(k) for l in range(k)])))
# Original solution
import math
from functools import reduce

k = int(eval(input())) + 1


def gcd(*numbers):
    return reduce(math.gcd, numbers)


def counter(num):
    count = 0
    for a in range(1, num):
        for b in range(1, num):
            t = gcd(a,b)
            for c in range(1, num):
                    count += gcd(t, c)
    print(count)

counter(k)


# Optimized solution
from math import gcd 

num = int(eval(input())) + 1

count = 0
for a in range(1, num):
    for b in range(1, num):
        t = gcd(a,b)
        for c in range(1, num):
                count += gcd(t, c)
print(count)
# Original solution
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
k = int(eval(input()))
for i in range(1,k+1):
    for j in range(1,k+1):
        for p in range(1,k+1):
            ans += gcd(i,j,p)
print(ans)


# Optimized solution
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

ans = 0
k = int(eval(input()))
for i in range(1,k+1):
    for j in range(i,k+1):
        for p in range(j,k+1):
            if (i!=j) and (j!=p) and (i!=p):
                #print(i,j,p, 'pattern3')
                ans += gcd(i,j,p) * 6
            elif (i==j) and (j==p):
                #print(i,j,p, 'pattern1')
                ans += gcd(i,j,p)
            else:
                #elif (i==j) or (j==k) or (i==k):
                #print(i,j,p, 'pattern2')
                ans += gcd(i,j,p) * 3

print(ans)
# Original solution
class Gcd:

    def __init__(self, K):
        self.table = [[None] * K for i in range(K)]
        return

    def gcd(self, x, y):
        if self.table[x - 1][y - 1] is not None:
            return self.table[x - 1][y - 1]

        if x == y:
            self.table[x - 1][y - 1] = x
            return x
        elif x > y:
            z = x % y
            if z == 0:
                self.table[x - 1][y - 1] = y
                return y
            else:
                z = self.gcd(z, y)
                self.table[x - 1][y - 1] = z
                return z
        else:
            return self.gcd(y, x)

def quest_c():
    K = int(eval(input()))

    gcd = Gcd(K)
    sum = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            d = gcd.gcd(a, b)
            for c in range(1, K + 1):
                sum += gcd.gcd(d, c)
    print(sum)

if __name__ == '__main__':
    quest_c()

# Optimized solution
class Gcd:

    def __init__(self, K):
        self.table = [[None] * K for i in range(K)]
        return

    def gcd(self, x, y):
        if self.table[x - 1][y - 1] is not None:
            return self.table[x - 1][y - 1]

        if x == y:
            self.table[x - 1][y - 1] = x
            return x
        elif x > y:
            z = x % y
            if z == 0:
                self.table[x - 1][y - 1] = y
                return y
            else:
                z = self.gcd(z, y)
                self.table[x - 1][y - 1] = z
                return z
        else:
            return self.gcd(y, x)

def quest_c():
    K = int(eval(input()))

    gcd = Gcd(K)
    sum = 0
    for a in range(1, K + 1):
        for b in range(1, K + 1):
            d = gcd.gcd(a, b)
            if d == 1:
                sum += K
            else:
                for c in range(1, K + 1):
                    sum += gcd.gcd(d, c)
    print(sum)

if __name__ == '__main__':
    quest_c()
# Original solution
import math
K=int(eval(input()))
k=K+1
ans=0
for a in range(1,k):
    for b in range(1,k):
        for c in range(1,k):
            m=math.gcd(a,b)
            n=math.gcd(m,c)
            ans+=n

print(ans)

# Optimized solution
import math
K=int(eval(input()))
k=K+1
ans=0
for a in range(1,k):
    for b in range(a,k):
        for c in range(b,k):
            m=math.gcd(a,b)
            o=math.gcd(m,c)
            if a==b and b==c:
                n=o
            elif a==b or b==c:
                n=o*3
            else:
                n=o*6
                
            ans+=n

print(ans)
# Original solution
import math
from functools import reduce
k = int(eval(input()))

def gcd(*numbers):
    return reduce(math.gcd, numbers)

ans = 0

for i in range(k):
    i += 1
    for j in range(k):
        j += 1
        for l in range(k):
            l += 1
            ans += gcd(i, j, l)

print(ans)


# Optimized solution
from math import gcd
from functools import reduce
k = int(eval(input()))

ans = 0

for i in range(k):
    i += 1
    for j in range(k):
        j += 1
        m = gcd(i, j)
        for l in range(k):
            l += 1
            ans += gcd(l, m)

print(ans)
# Original solution
import math

k = int(eval(input()))
total = 0
for a in range(1,k+1):
  for b in range(1,k+1):
    for c in range(1,k+1):
      total+=math.gcd(math.gcd(a, b), c)
print(total)

# Optimized solution
def main():
  from math import gcd
  k = int(eval(input()))
  
  total = 0
  for a in range(1,k+1):
    for b in range(1,k+1):
      temp = gcd(a, b)
      for c in range(1,k+1):
        total+= gcd(temp, c)
  print(total)
main()
# Original solution
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

K = int(eval(input()))
num = 0

for a in range(K):
    for b in range(K):
        for c in range(K):
            num += gcd(a+1, b+1, c+1)

print(num)

# Optimized solution
import math

K = int(eval(input()))
num = 0

for a in range(K):
    for b in range(K):
        ab = math.gcd(a+1, b+1)
        for c in range(K):
            num += math.gcd(ab, c+1)

print(num)