import math

def sum(a, b):
    g = math.gcd(a, b)
    total = 0
    i = 1
    while i * i <= g:
        if g % i == 0:
            total += i
            other = g // i
            if other != i:
                total += other
        i += 1
    return total