Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.
Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

-----Constraints-----
 - 1 \leq K \leq 200
 - K is an integer.

-----Input-----
Input is given from Standard Input in the following format:
K

-----Output-----
Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

-----Sample Input-----
2

-----Sample Output-----
9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)=1+1+1+1+1+1+1+2=9
Thus, the answer is 9.

Difficulty: introductory

Starter code:

Solutions:
import math
K = int(input())
rst = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)

from math import gcd
K = int(input())
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            ans += gcd(gcd(a,b),c)
print(ans)

from math import gcd
n = int(input())
ans = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    for k in range(1,n+1):
      ans += gcd(gcd(i,j),k)
print(ans)

def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b,a%b)
k = int(input())
ans = 0
for a in range(1,k+1):
  for b in range(a,k+1):
    for c in range(b,k+1):
      d = gcd(a, b)
      if len({a,b,c}) == 1:
        ans += gcd(c, d)
      elif len({a,b,c}) == 2:
        ans += 3*gcd(c, d)
      else:
        ans += 6*gcd(c, d)
print(ans)

K = int(input())
ans = 0

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

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)

import itertools as itt
import math

k = int(input())

ans = 0
for i in itt.combinations_with_replacement(range(1, k+1), 3):
    if i[0] == i[1] and i[0] != i[2]:
        ans += 3 * math.gcd(math.gcd(i[0], i[1]), i[2])
    elif i[1] == i[2] and i[1] != i[0]:
        ans += 3 * math.gcd(math.gcd(i[0], i[1]), i[2])
    elif i[0] == i[1] and i[0] == i[2]:
        ans += math.gcd(math.gcd(i[0], i[1]), i[2])
    else:
        ans += 6 * math.gcd(math.gcd(i[0], i[1]), i[2])

print(ans)

from math import gcd
def main():
    ans = 0
    k = int(input())
    for i in range(1,1+k):
        for j in range(1,1+k):
            for l in range(1,1+k):
                ans += gcd(i,gcd(j,l))
    print(ans)


def __starting_point():
    main()
__starting_point()

from math import gcd
K=int(input());print(sum(gcd(a+1,gcd(b+1,c+1))for a in range(K)for b in range(K)for c in range(K)))

import math 

k = int(input())

ans = 0

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

print(ans)

import math
K = int(input())
ans = 0

for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            s = math.gcd(a, b)
            t = math.gcd(s, c)
            if a == c:
                ans += t
            elif (a == b or b == c) and a != c:
                ans += 3*t
            else:
                ans += 6*t
print(ans)

import sys
from math import ceil as C, floor as F, sqrt, gcd as G
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as HQ

class Heap:
  def __init__(self, data, reverse=False):
    self.reverse = -1 if reverse else 1
    self.data = [self.reverse * d for d in data]
    HQ.heapify(self.data)
  def push(self, x): return HQ.heappush(self.data, self.reverse * x)
  def pop(self): return self.reverse * HQ.heappop(self.data) 

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()

ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
           ans += G(i, G(j, k))

print(ans)
            


from math import gcd

k = int(input())


ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += gcd(gcd(x,y),z)

print(ans)


from math import gcd


def main():
    K = int(input())
    ans = 0

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


def __starting_point():
    main()
__starting_point()

def main():
    import math
    k = int(input())
    cand = [int(v) for v in range(1, k + 1)]
    ans = 0
    for i in range(1, k + 1):
        for j in range(1, k + 1):
            for l in range(1, k + 1):
                temp = math.gcd(i, j)
                res = math.gcd(temp, l)
                ans += res
    return ans


def __starting_point():
    print((main()))

__starting_point()

from math import *

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


import math

k = int(input())
total = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        tmp = math.gcd(i, j)
        for k in range(1,k+1):
            total += math.gcd(tmp,k)
print(total)

from math import gcd 
k = int(input())
cnt = 0
for i in range(1, k+1):
  for j in range(1, k+1):
    for l in range(1, k+1):
      cnt += gcd(gcd(i, j), l)
print(cnt)

import math
K = int(input())
rst = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)

K = int(input())

ans = 0

def gcd(x, y):
    if x == 1 or y == 1:
        return 1
    else:
        while True:
            if x >= y:
                x %= y
            else:
                y %= x
            if x == 0 or y == 0:
                break
        return x + y

for i in range(1, K + 1):
    for j in range(i, K + 1):
        for k in range(j, K + 1):
            if i == j == k:
                ans += gcd(i, gcd(j, k))
            elif i == j or j == k or k == i:
                ans += gcd(i, gcd(j, k)) * 3
            else:
                ans += gcd(i, gcd(j, k)) * 6

print(ans)

from math import gcd

k = int(input())

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

import math

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

import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
gcd = math.gcd

def main():
    K = I()
    ans = 0
    for i in range(1,K+1):
        for j in range(i, K+1):
            temp = gcd(i, j)
            for k in range(j, K+1):
                n = set([i,j,k])
                n = len(n)
                ans += gcd(temp,k) * (n+1)*(n)//2
    print(ans)
def __starting_point():
    main()
__starting_point()

# -*- coding: utf-8 -*-
import sys


def main():
    N = int( sys.stdin.readline() )


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

    num_cnt_dic = {}
    num_gcd = [ [0]*(N+1) for _ in range(N+1) ]

    for i in range(1, N+1):
        for j in range(1, N+1):
            g = gcd(i, j)

            num_cnt_dic[g] = num_cnt_dic.get(g, 0) + 1
            num_gcd[i][j] = g
    

    ans = 0

    for i in list(num_cnt_dic.keys()):
        for j in range(1, N+1):
            ans += (num_gcd[i][j] * num_cnt_dic[i])
    
    
    print(ans)


def __starting_point():
    main()

__starting_point()

import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
    k = int(input())

    k2 = np.arange(1, k+1)
    k2gcd = np.gcd.outer(k2, np.gcd.outer(k2, k2))
    print(k2gcd.sum())

def __starting_point():
    main()
__starting_point()

#!/usr/env python3
#encoding:utf8

import math
from itertools import combinations_with_replacement as comb
from functools import reduce

def main():
    K = int(input())

    ans = 0
    for abc in comb(range(1, K+1), 3):
        gcd = reduce(math.gcd, abc)
        s = len(set(abc))
        if s == 1:
            k = 1
        elif s == 2:
            k = 3
        else:
            k = 6
        ans += gcd * k
        #print(f"abc={abc} gcd={gcd} k={k}")

    print(ans)

def __starting_point():
    main()
__starting_point()

import math

k=int(input())
ans=0

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

print(ans)

import math
k=int(input())
ans=0
for a in range(1,k+1):
    for b in range(1,k+1):
        if math.gcd(a,b)==1:
            ans+=k
        else :
            for c in range(1,k+1):
                ans+=math.gcd(math.gcd(a,b),c)
print(ans)


from collections import defaultdict
from math import gcd


def answer(k: int) -> int:
    dd = defaultdict(int)
    for a in range(1, k + 1):
        for b in range(1, k + 1):
            dd[gcd(a, b)] += 1

    gcd_sum = 0
    for c in range(1, k + 1):
        for gcd_ab in dd.keys():
            gcd_sum += gcd(gcd_ab, c) * dd[gcd_ab]

    return gcd_sum


def main():
    k = int(input())
    print(answer(k))


def __starting_point():
    main()
__starting_point()

import collections
def gcd(a,b):
  if b==0:
    return a
  else:
    return gcd(b,a%b)
K=int(input())
cnt=collections.defaultdict(int)
for a in range(1,K+1):
  for b in range(1,K+1):
    cnt[gcd(a,b)]+=1
ans=0
for c in range(1,K+1):
  for gcd_of_ab in cnt.keys():
  	ans+=gcd(gcd_of_ab,c)*cnt[gcd_of_ab]
print(ans)

k = int(input())
ans = 0
def gcd(a,b):
    if a % b == 0:
        return b
    c = a % b
    return gcd(b,c)

for l in range(1,k+1):
    for m in range(l,k+1):
        for n in range(m,k+1):
            tmp1 = gcd(l,n)
            tmp2= gcd(tmp1,m)
            if (l==m==n):
                ans+=tmp2
            elif(l==m or m==n):
                ans+= 3*tmp2
            else:
                ans += 6*tmp2


print(ans)

import math

N=int(input())
ans=0
for i in range(1,N+1):
    for j in range(i,N+1):
        for k in range(j,N+1):
            if i==j and j==k:
                ans+=i
            elif i<j and j<k:
                ans+=6*math.gcd(i,math.gcd(j,k))
            else:
                ans+=3*math.gcd(i,math.gcd(j,k))
print(ans)


import math

K = int(input())
ans = 0

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

print(ans)

from math import gcd

k = int(input())

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

print(ans)


k = int(input())
def gcd1 (a, b):
    while True:
        if (a < b):
            a, b = b, a
        c = a%b
        if (c == 0):
            return (b)
        else:
            a = b
            b = c

count = 0
for i in range(k):
    for j in range(k):
        tmp = gcd1(i + 1, j + 1)
        if (tmp == 1):
            count = count + k
        else:
            for l in range(k):
                tmp2 = gcd1(tmp, l + 1)
                count = count + tmp2

print(count)


from math import gcd


def main():
    k = int(input())

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

    print(answer)
    return


def __starting_point():
    main()

__starting_point()

import math
from functools import reduce

k = int(input())

ans = 0

gcd_sum = [0] * 201

for i in range(1, k + 1):
    for j in range(1, k + 1):
        gcd_sum[i] += math.gcd(i, j)

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

print(ans)


from math import gcd
k=int(input())

cnt=0

for i in range(1,k+1):
    for j in range(1,k+1):
        a=gcd(i,j)
        for k in range(1,k+1):
            cnt+=gcd(a,k)
print(cnt)


from math import gcd

k = int(input())

ans = 0
for x in range(1,k+1):
    for y in range(1,k+1):
        for z in range(1,k+1):
            ans += gcd(gcd(x,y),z)

print(ans)


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
gcd = math.gcd

def main():
    K = I()
    ans = 0
    for i in range(1,K+1):
        for j in range(1, K+1):
            temp = gcd(i, j)
            for k in range(1, K+1):
                ans += gcd(temp,k)
    print(ans)
def __starting_point():
    main()
__starting_point()

import math

k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(a+1, k+1):
        ans += math.gcd(a, b)
        for c in range(b+1, k+1):
            ans += math.gcd(math.gcd(a, b), c)
ans *= 6
ans += (k+1)*k/2
print((int(ans)))


from math import gcd

K = int(input())


ans = 0
for i in range(1, K+1):
    for j in range(1, K+1):
        tmp = gcd(i, j)
        if tmp == 1:
            ans += tmp*K
        else:
            for k in range(1, K+1):
                ans += gcd(tmp, k)
            

print(ans)

import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
gcd = math.gcd

def main():
    K = I()
    ans = 0
    D = Init(K+1, K+1, 0)
    for i in range(1, K+1):
        for j in range(i, K+1):
            if i == 1:
                D[i][j] = 1
            if i == j:
                D[i][j] = i
            else:
                D[i][j] = gcd(i, j)

    for i in range(1,K+1):
        for j in range(i, K+1):
            temp = D[i][j]
            for k in range(j, K+1):
                n = set([i,j,k])
                n = len(n)
                ans += D[temp][k] * (n+1)*(n)//2
    print(ans)
def __starting_point():
    main()
__starting_point()

from math import gcd
K=int(input())
result=0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            result+=gcd(gcd(a,b),c)
print(result)

import math
k = int(input())

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

import math
k = int(input())
ans = 0


for a in range(1, k+1):
  for b in range(a, k+1):
    for c in range(b, k+1):
      gcd = math.gcd(math.gcd(a,b),c)
      if a == b == c:
        ans += gcd
      elif a == b or b == c:
        ans += gcd *3
      else:
        ans += gcd *6
      
print(ans)

import math
k = int(input())

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

print(ans)


from sys import stdin,stdout
import math

def main():
    k = int(stdin.readline().rstrip())
    ans = 0
    for a in range(1,k+1):
        for b in range(1,k+1):
            ab = math.gcd(a,b)
            for c in range(1,k+1):
                ans = ans + math.gcd(ab,c)

    stdout.write(str(ans))
    stdout.write('\n')

main()


# C - Sum of gcd of Tuples (Easy)

import math

k = int(input())
s = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        gcdab = math.gcd(a,b)
        if  gcdab== 1:
            s += k
        else:
            for c in range(1,k+1):
                s += math.gcd(gcdab,c)

print(s)




import math

K = int(input())
sum = 0

for i in range(1, K + 1):
  for j in range(1, K + 1):
    tmp = math.gcd(i, j)
    for k in range(1, K + 1):
      sum += math.gcd(tmp, k)
      
print(sum)


K = int(input())

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

result = 0

for a in range(1, K+1):
    result += a
    #print('{} add'.format(a))
    b = a + 1
    while b <= K:
        result += 6 * gcd(a, b)
        #print('{} and {} add'.format(a, b))
        l = gcd(a, b)
        c = b + 1
        while c <= K:
            result += 6 * gcd(l, c)
            #print('{} and {} and {} add'.format(a, b, c))
            c+= 1
        b += 1
print(result)

import math
k = int(input())
sum = 0

for a in range(1,k+1):
    for b in range(1,k+1): 
        x = math.gcd(a, b)
        for c in range(1,k+1):
            sum += math.gcd(x, c)

print(sum)

import math

K = int(input())

sum = 0
for i in range(1,K+1):
  for j in range(1,K+1):
    temp = math.gcd(i,j)
    for k in range(1,K+1):
      sum+=math.gcd(temp,k)
print (sum)

import math
K = int(input())
ans = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        a = math.gcd(i,j)
        for k in range(1,K+1):
            ans += math.gcd(a,k)
print(ans)

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

import math
n=int(input())

ans=0

for i in range(1,n+1):
  for j in range(1,n+1):
    r=math.gcd(i,j)
    for k in range(1,n+1):
      ans+=math.gcd(r,k)
print(ans)

import math
k = int(input())


total = 0

for x in range(1,k+1):
    for y in range(x,k+1):
        for z in range(y,k+1):
            if x == y == z:
                total += x
            elif x == y or y == z:
                total += 3 * math.gcd(math.gcd(x,y),z)
            else:
                total += 6 * math.gcd(math.gcd(x,y),z)

print(total)


import math
from functools import reduce

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

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
k=int(input())
p=0
ans=0
if k>2:
  for i in range(1,k-1):
    for j in range(i+1,k):
        for m in range(j+1,k+1):
            ans+=gcd(i,j,m)
p+=6*ans
b=0
for i in range(1,k):
    for j in range(i+1,k+1):
        b+=math.gcd(i,j)
p+=6*b+k*(k+1)//2
print(p)

import math

k = int(input())
wa = 0
aa = 0
for i in range(1,k+1):
  for j in range(1,k+1):
    aa =math.gcd(i,j)
    for l in range(1,k+1):
      wa +=math.gcd(aa,l)
print(wa)

k=int(input())
import math
s=0
result=0
for a in range(1,k+1,1):
    for b in range(a,k+1,1):
        for c in range(b,k+1,1):
            gcd=math.gcd(math.gcd(a,b),math.gcd(b,c))
            if a==b==c:
                pass
            elif (a==b and b!=c) or (b==c and b!=a) or (a==c and a!=c):
                gcd=3*gcd
            else:
                gcd=6*gcd
            
            result+=gcd
print(result)

import numpy as np

K = int(input())
x = np.arange(1, K + 1)

print(np.sum(np.gcd.outer(np.gcd.outer(x, x), x)))

import math
K=int(input())

list1=[]
s=1
while s<=K:
    list1.append(s)
    s=s+1

list3=[]
t=1
while t<=K*K:
    list3.append(t)
    t=t+1

list2=[]
k=1
for i in list1:
    for j in list1:
        X=math.gcd(i,j)
        list2.append(X)

Y=0
for k in list1:
    for l in list3:
        Y=Y+math.gcd(k,list2[l-1])

print(Y)


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp

INF = inf
MOD = 1000000007

k = int(input())

tmp = [[0 for i in range(k + 1)] for j in range(k + 1)]
res = 0

for i in range(1, k + 1):
    for j in range(1, k + 1):
        tmp[i][j] = gcd(i, j)

for a in range(1, k + 1):
    for b in range(k + 1):
        for c in range(k + 1):
            res += tmp[tmp[a][b]][c]

print(res)


import math
n =int(input())
c=0
for i in range(1,n+1):
    for j in range(1,n+1):
        d= math.gcd(i,j)
        for k in range(1,n+1):
            c+=math.gcd(d,k)
print(c)

import math
n=int(input());ans=0
for i in range(1,n+1):
  for j in range(1,n+1):
    r=math.gcd(i,j)
    for k in range(1,n+1):
      ans+=math.gcd(r,k)
print(ans)

from math import gcd

k=int(input())
ans=0

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

print(ans)


k = int(input())
def gcd1 (a, b):
    while True:
        if (a < b):
            a, b = b, a
        c = a%b
        if (c == 0):
            return (b)
        else:
            a = b
            b = c

def gcd2 (a, b, c):
    tmp = gcd1(a, b)
    ans = gcd1(tmp, c)
    return (ans)

count = 0
for i in range(k):
    for j in range(i, k):
        for l in range(j, k):
            tmp = gcd2(i + 1, j + 1, l + 1)
            if (i == j == l):
                count = count + tmp
            elif (i == j or j == l):
                count = count + tmp*3
            else:
                count = count + tmp*6
print(count)


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

import math
K = int(input())
rst = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)


from math import gcd
K=int(input())
g=0
for a in range(1,K-1):
  for b in range(a+1,K):
    for c in range(b+1,K+1):
      g+=gcd(gcd(a,b),c)*6
for  d in range(1,K):
  for e in range(d+1,K+1):
    g+=gcd(d,e)*6
g+=K*(K+1)//2
print(g)

import math
K = int(input())
rst = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)

import math

k = int(input())
ans = 0

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

print(ans)

from math import gcd

n = int(input())

ans = 0


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

print(ans)


import math

K = int(input())
ans = sum(math.gcd(i, math.gcd(j, k)) for i in range(1, K+1) for j in range(1, K+1) for k in range(1, K+1))
print(ans)

from math import gcd


def answer(k: int) -> int:
    result = 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):
                result += gcd(temp, c)

    return result


def main():
    k = int(input())
    print((answer(k)))


def __starting_point():
    main()

__starting_point()

from math import gcd
k = int(input())
ans = 0
for i in range(1, k + 1):
  for j in range(1, k + 1):
    g = gcd(i, j)
    for k in range(1, k + 1):
      ans += gcd(g, k)
print(ans)



from math import gcd as g


k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        temp = g(a, b)
        for c in range(1, k+1):
            ans += g(temp, c)


print(ans)


import math
k=int(input())

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


from math import gcd
k = int(input())
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(gcd(a, b), c)

print(sum)

n = int(input())
memo = [[0] * (n+1) for x in range(n+1)]

def gcd(x,y):
  if y == 0:
    return x
  if not memo[x][y] == 0:
    return memo[x][y]
  memo[x][y] = gcd(y,x % y)
  return gcd(y,x % y)

res = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    q = gcd(i,j)
    if q == 1:
      res += n
    else:
      for k in range(1,n+1):
        p = gcd(q,k)
        res += p
print(res)

import math
K = int(input())
rst = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)

import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
gcd = math.gcd

def main():
    K = I()
    ans = 0
    D = Init(K+1, K+1, 0)
    for i in range(1, K+1):
        for j in range(i, K+1):
            D[i][j] = gcd(i, j)

    for i in range(1,K+1):
        for j in range(i, K+1):
            temp = D[i][j]
            for k in range(j, K+1):
                n = set([i,j,k])
                n = len(n)
                ans += D[temp][k] * (n+1)*(n)//2
    print(ans)
def __starting_point():
    main()
__starting_point()

def gcd_r(a, b):
  if memo[a][b] != 0:
    return memo[a][b]

  if a < b:
    a, b = b, a

  memo[a][b] = gcd(a, b)
  memo[b][a] = memo[a][b]
  return memo[a][b]

def gcd(a, b):
  r = a % b
  if r == 0:
    return b

  return gcd(b, r)

k = int(input())
memo = [[0]*(k+1) for _ in range(k+1)]
total = 0

for i in range(1, k+1, 1):
  for c in range(1, k+1, 1):
    total += gcd_r(gcd_r(i, i), c)

for a in range(1, k+1, 1):
  for b in range(a+1, k+1, 1):
    for c in range(1, k+1, 1):
      total += gcd_r(gcd_r(a, b), c) * 2

print(total)


from math import gcd
def main():
    ans = 0
    k = int(input())
    for i in range(k):
        for j in range(k):
            for l in range(k):
                ans += gcd(i+1,gcd(j+1,l+1))
    print(ans)


def __starting_point():
    main()
__starting_point()

import math
k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(a, k+1):
        for c in range(b, k+1):
            if a == b == c:
                ans += math.gcd(math.gcd(a,b),c)
            elif a == b or b == c or c == a:
                ans += 3 * math.gcd(math.gcd(a, b), c)
            else:
                ans += 6 * math.gcd(math.gcd(a, b), c)
print(ans)

from math import gcd
K = int(input())
ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        r = gcd(a,b)
        for c in range(1,K+1):
            ans += gcd(r,c)
print(ans)

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

import math
K=int(input())
ans=0
for a in range(1,K+1):
  for b in range(1,K+1):
    g=math.gcd(a,b)
    for c in range(1,K+1):
      ans+=math.gcd(g,c)
print(ans)      

from math import gcd
K=int(input())
k=[1,1,1]
g=0
for a in range(1,K+1):
  for b in range(1,K+1):
    for c in range(1,K+1):
      g+=gcd(gcd(a,b),c)
      
      
print(g)

from math import gcd

def main():
    k = int(input())
    ans = 0
    for i in range(1, k+1):
        for j in range(1, k+1):
            for k in range(1, k+1):
                ans += gcd(gcd(i, j), k)
    print(ans)
def __starting_point():
    main()
__starting_point()

import math
K =  int(input())
ans = []
bc = [math.gcd(b, c) for b in range(1, K+1) for c in range(1, K+1)]
for a in range(1, K+1):
    for i in bc:
        x = math.gcd(a, i)
        ans.append(x)
print(sum(ans))

import math
def resolve():
    k = int(input())
    ans = 0
    for a in range(1,k+1):
        for b in range(1, k+1):
            for c in range(1, k+1):
                ans += math.gcd(math.gcd(a,b),c)
    print(ans)
resolve()

import math
n=int(input())
ans=0
for i in range(1,n+1):
    for j in range(1,n+1):
        r=math.gcd(i,j)
        for k in range(1,n+1):
            ans+=math.gcd(r,k)
print(ans)



import math
rst = 0
K = int(input())
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(k, tmp)
print(rst)


import math
K = int(input())
rst = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        tmp = math.gcd(i, j)
        for k in range(1, K + 1):
            rst += math.gcd(tmp, k)
print(rst)

from math import gcd
n = int(input())
ans = 0

for i in range(1,n+1):
  for j in range(1,n+1):
    x = gcd(i,j)
    for k in range(1,n+1):
        ans += gcd(x,k)
print(ans)


from math import gcd

k = int(input())
result = 0

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

print(result)

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


k = int(input())
ans = 0
for a in range(1, k + 1):
    for b in range(a, k + 1):
        for c in range(b, k + 1):
            d = gcd(a, b)
            if len({a, b, c}) == 1:
                ans += gcd(c, d)
            elif len({a, b, c}) == 2:
                ans += 3 * gcd(c, d)
            else:
                ans += 6 * gcd(c, d)
print(ans)


from math import gcd
k = int(input())
sum = 0

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

print(sum)

K = int(input())
ans = 0
def gcd(x,y):
    if(y == 0):
        return x
    if(x >= y):
        return gcd(y,x%y)
    if(x < y):
        return gcd(x,y%x)
for i in range(1,K+1):
    ans += i
for i in range(1,K+1):
    for j in range(i+1,K+1):
        ans += gcd(i,j)*6
for i in range(1,K+1):
    for j in range(i+1,K+1):
        for k in range(j+1,K+1):
            ans += gcd(gcd(i,j),k)*6  
print(ans)