def sum(a, b):
    limit = min(abs(a), abs(b))
    total = 0

    if limit == 0:
        return 0

    for divisor in range(1, limit + 1):
        if a % divisor == 0 and b % divisor == 0:
            total += divisor

    return total