Buggy python submission (verdict: Time Limit Exceeded):
K=int(input())

memo={}
def gcd(a,b):
    if a<b:
        a,b=b,a

    aa=a
    bb=b
    if (a,b) in memo.keys():
        return memo[a,b]
    while b != 0:
        a,b, = b, a%b
    memo[aa,bb]=a
    return a





memo3={}

def gcd3(a,b,c):
    if a>b:
        a,b = b,a
    if a>c:
        a,c = c,a
    if b>c:
        b,c = c,b
    if (a,b,c) in memo3.keys():
        return memo3[(a,b,c)]
    
    g = gcd(gcd(a,b),c)
    memo3[(a,b,c)]=g
    return g


sum=0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            
            if i==j & j==k:
                sum += gcd3(i,j,k)
                continue
            if i==j & j!=k:
                sum += gcd3(i,j,k)*3
                continue
            if i!=j & j==k:
                sum += gcd3(i,j,k)*3
                continue
            sum += gcd3(i,j,k)*6
print(sum)





Fixed submission:
K=int(input())

memo={}
def gcd(a,b):
    if a<b:
        a,b=b,a

    aa=a
    bb=b
    if (a,b) in memo.keys():
        return memo[a,b]
    while b != 0:
        a,b, = b, a%b
    memo[aa,bb]=a
    return a





memo3={}

def gcd3(a,b,c):
    if a>b:
        a,b = b,a
    if a>c:
        a,c = c,a
    if b>c:
        b,c = c,b
    if (a,b,c) in memo3.keys():
        return memo3[(a,b,c)]
    
    g = gcd(gcd(a,b),c)
    memo3[(a,b,c)]=g
    return g


sum=0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            
            if i==j & j==k:
                sum += gcd3(i,j,k)
                continue
            if i==j & j!=k:
                sum += gcd3(i,j,k)*3
                continue
            if i!=j & j==k:
                sum += gcd3(i,j,k)*3
                continue
            sum += gcd3(i,j,k)*6
print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
K=int(input())

memo={}
def gcd(a,b):
    if a<b:
        a,b=b,a

    aa=a
    bb=b
    if (a,b) in memo.keys():
        return memo[a,b]
    while b != 0:
        a,b, = b, a%b
    memo[aa,bb]=a
    return a





memo3={}

def gcd3(a,b,c):
    if a>b:
        a,b = b,a
    if a>c:
        a,c = c,a
    if b>c:
        b,c = c,b
    if (a,b,c) in memo3.keys():
        return memo3[(a,b,c)]
    
    g = gcd(gcd(a,b),c)
    memo3[(a,b,c)]=g
    return g


sum=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            sum += gcd3(i,j,k)
print(sum)





Fixed submission:
K=int(input())

memo={}
def gcd(a,b):
    if a<b:
        a,b=b,a

    aa=a
    bb=b
    if (a,b) in memo.keys():
        return memo[a,b]
    while b != 0:
        a,b, = b, a%b
    memo[aa,bb]=a
    return a





memo3={}

def gcd3(a,b,c):
    if a>b:
        a,b = b,a
    if a>c:
        a,c = c,a
    if b>c:
        b,c = c,b
    if (a,b,c) in memo3.keys():
        return memo3[(a,b,c)]
    
    g = gcd(gcd(a,b),c)
    memo3[(a,b,c)]=g
    return g


sum=0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            
            if i==j & j==k:
                sum += gcd3(i,j,k)
                continue
            if i==j & j!=k:
                sum += gcd3(i,j,k)*3
                continue
            if i!=j & j==k:
                sum += gcd3(i,j,k)*3
                continue
            sum += gcd3(i,j,k)*6
print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
K=int(input())

memo={}
def gcd(a,b):
    if a<b:
        a,b=b,a

    aa=a
    bb=b
    if (a,b) in memo.keys():
        return memo[a,b]
    while b != 0:
        a,b, = b, a%b
    memo[aa,bb]=a
    return a





memo3={}

def gcd3(a,b,c):
    if a>b:
        a,b = b,a
    if a>c:
        a,c = c,a
    if b>c:
        b,c = c,b
    if (a,b,c) in memo3.keys():
        return memo3[(a,b,c)]
    
    g = gcd(gcd(a,b),c)
    memo3[(a,b,c)]=g
    return g


sum=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            sum += gcd3(i,j,k)
print(sum)





Fixed submission:
K=int(input())

memo={}
def gcd(a,b):
    if a<b:
        a,b=b,a

    aa=a
    bb=b
    if (a,b) in memo.keys():
        return memo[a,b]
    while b != 0:
        a,b, = b, a%b
    memo[aa,bb]=a
    return a





memo3={}

def gcd3(a,b,c):
    if a>b:
        a,b = b,a
    if a>c:
        a,c = c,a
    if b>c:
        b,c = c,b
    if (a,b,c) in memo3.keys():
        return memo3[(a,b,c)]
    
    g = gcd(gcd(a,b),c)
    memo3[(a,b,c)]=g
    return g


sum=0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            
            if i==j & j==k:
                sum += gcd3(i,j,k)
                continue
            if i==j & j!=k:
                sum += gcd3(i,j,k)*3
                continue
            if i!=j & j==k:
                sum += gcd3(i,j,k)*3
                continue
            sum += gcd3(i,j,k)*6
print(sum)