Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 0

def gcd(a,b):
    if a>b:
        p = a
        q = b
    else:
        p = b
        q = a
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


for i in range(1,n+1):
    for j in range(1,n+1):
        for k in range(1,n+1):
            if i==1 or j==1 or k==1:
                ans += 1
            elif abs(i-j)==1 or abs(j-k)==1 or abs(k-i)==1:
                ans += 1
            else:
                ans += gcd(gcd(i,j),k)

print(ans)


Fixed submission:
n = int(input())
ans = 0
 
def max(a,b):
    if a>b:
        return a
    else:
        return b
 
def min(a,b):
    if a<b:
        return a
    else:
        return b
 
def gcd(a,b):
    p = max(a,b)
    q = min(a,b)
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d
 
 
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)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 0

def gcd(a,b):
    if a>b:
        p = a
        q = b
    else:
        p = b
        q = a
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


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

print(ans)


Fixed submission:
n = int(input())
ans = 0
 
def max(a,b):
    if a>b:
        return a
    else:
        return b
 
def min(a,b):
    if a<b:
        return a
    else:
        return b
 
def gcd(a,b):
    p = max(a,b)
    q = min(a,b)
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d
 
 
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)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 0

def gcd(a,b):
    if a>b:
        p = a
        q = b
    else:
        p = b
        q = a
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


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)


Fixed submission:
n = int(input())
ans = 0
 
def max(a,b):
    if a>b:
        return a
    else:
        return b
 
def min(a,b):
    if a<b:
        return a
    else:
        return b
 
def gcd(a,b):
    p = max(a,b)
    q = min(a,b)
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d
 
 
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)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 0

def max(a,b):
    if a>b:
        return a
    else:
        return b

def min(a,b):
    if a<b:
        return a
    else:
        return b

def gcd(a,b):
    p = max(a,b)
    q = min(a,b)
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d


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)


Fixed submission:
n = int(input())
ans = 0
 
def max(a,b):
    if a>b:
        return a
    else:
        return b
 
def min(a,b):
    if a<b:
        return a
    else:
        return b
 
def gcd(a,b):
    p = max(a,b)
    q = min(a,b)
    d = q
    while p % q != 0:
        d = p % q
        p = q
        q = d
    return d
 
 
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)