# Arup Guha
# 11/16/2024
# Solution to 2024 SER D2 Problem C: Traffic Lights

import math

# Returns the least common multiple of a and b.
def lcm(a,b):
    return a*b//math.gcd(a,b)

# Get number of equations.
n = int(input())

# Will store the mods for each traffic light and the remainders that are green.
mods = []
vals = []

# Go through each light.
for i in range(n):

    # Get numbers.
    tmp = [int(x) for x in input().split()]

    # Mod is the sum.
    mods.append(tmp[0]+tmp[1])

    # I make a set of the valid mods when the light is green.
    tmpset = set()
    for i in range(tmp[0], tmp[0]+tmp[1]):
        tmpset.add(i)
    vals.append(tmpset)

# Answer for one light.
curmod = mods[0]
curvals = vals[0]

# Add in rest.
for i in range(1, n):

    # Build the new set here, incorporating light i.
    newvals = set()

    # Our new cycle size.
    newmod = lcm(curmod, mods[i])

    # Go through each mod.
    for x in curvals:

        # Try each unique modulus mod the new mod. 
        y = x

        # This loop won't run too many times, maybe 20 at most.
        while y < newmod:

            # See if y also works with this new traffic light.
            if y%mods[i] in vals[i]:
                newvals.add(y)

            # Next consistent value equal to y%curmod.
            y += curmod

    # Our updated results.
    curmod = newmod
    curvals = newvals

# I suspect this never prints because each light becomes green at the CRT
# guarantees a solution.
if len(curvals) == 0:
    print(-1)

# Find the smallest valid answer.
else:
    res = 1000000000000000000000
    for x in curvals:
        res = min(res, x)
    print(res)

            
