def slove():

    import sys

    input = sys.stdin.readline

    n = int(input().rstrip('\n'))

    ab = [list(map(int, (str(i) + " " + input().rstrip('\n')).split())) for i in range(n)]

    ab.sort(reverse=True)

    cnt = 0

    for i, a, b in ab:

        if (a + cnt) % b != 0:

            cnt += (b - (a + cnt) % b)

    print(cnt)





if __name__ == '__main__':

    slove()

def slove():

    import sys

    input = sys.stdin.readline

    n = int(input().rstrip('\n'))

    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]

    t = 0

    for i in range(n-1, -1, -1):

        while True:

            if (ab[i][0] + t) % ab[i][1] == 0:

                break

            else:

                t += (ab[i][1] - (ab[i][0] + t) % ab[i][1])

    print(t)





if __name__ == '__main__':

    slove()

def slove():
    import sys

    input = sys.stdin.readline
    n = int(input().rstrip("\n"))
    ab = [
        list(map(int, (str(i) + " " + input().rstrip("\n")).split())) for i in range(n)
    ]
    ab.sort(reverse=True)
    cnt = 0
    for i, a, b in ab:
        if (a + cnt) % b != 0:
            cnt += b - (a + cnt) % b
    print(cnt)


if __name__ == "__main__":
    slove()

def slove():
    import sys

    input = sys.stdin.readline
    n = int(input().rstrip("\n"))
    ab = [list(map(int, input().rstrip("\n").split())) for _ in range(n)]
    t = 0
    for i in range(n - 1, -1, -1):
        while True:
            if (ab[i][0] + t) % ab[i][1] == 0:
                break
            else:
                t += ab[i][1] - (ab[i][0] + t) % ab[i][1]
    print(t)


if __name__ == "__main__":
    slove()

-    ab = [ -        list(map(int, (str(i) + " " + input().rstrip("\n")).split())) for i in range(n) -    ] -    ab.sort(reverse=True) -    cnt = 0 -    for i, a, b in ab: -        if (a + cnt) % b != 0: -            cnt += b - (a + cnt) % b -    print(cnt) +    ab = [list(map(int, input().rstrip("\n").split())) for _ in range(n)] +    t = 0 +    for i in range(n - 1, -1, -1): +        while True: +            if (ab[i][0] + t) % ab[i][1] == 0: +                break +            else: +                t += ab[i][1] - (ab[i][0] + t) % ab[i][1] +    print(t)