Buggy python submission (verdict: Time Limit Exceeded):
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import math
import fractions
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

k = I()

result = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        frc = fractions.gcd(i,j)
        for l in range(1, k+1):
            result += fractions.gcd(frc, l)

print(result)


Fixed submission:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import math
import fractions
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

k = I()

result = 0

gcdList = {}

frc = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        t = (i,j)
        if t in gcdList:
            frc = gcdList[t]
        else:
            frc = fractions.gcd(i,j)
            gcdList[t] = frc
            
        for l in range(1, k+1):
            t2 = (gcdList[(i,j)],l)
            
            if t2 in gcdList:
                frc = gcdList[t2]
            else:
                frc = fractions.gcd(gcdList[(i,j)],l)
                gcdList[t2] = frc
            
            result += frc

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import math
import fractions
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

k = I()

result = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        frc = fractions.gcd(i,j)
        for l in range(1, k+1):
            result += fractions.gcd(frc, l)

print(result)


Fixed submission:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import math
import fractions
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

k = I()

result = 0

gcdList = {}

frc = 0
for i in range(1, k+1):
    for j in range(1, k+1):
        t = (i,j)
        if t in gcdList:
            frc = gcdList[t]
        else:
            frc = fractions.gcd(i,j)
            gcdList[t] = frc
            
        for l in range(1, k+1):
            t2 = (gcdList[(i,j)],l)
            
            if t2 in gcdList:
                frc = gcdList[t2]
            else:
                frc = fractions.gcd(gcdList[(i,j)],l)
                gcdList[t2] = frc
            
            result += frc

print(result)