Kakao Money
Time limit5sMemory limit256 MB
Given a log of deposits and withdrawals with resulting balances, find a minimum charge unit M that is consistent with every withdrawal, or report that none exists.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Implementation, Brute force
- Solved
- No attempts yet
Problem
KakaoPay is a fintech service that lets you transfer money and make payments through KakaoTalk. KakaoPay offers a service called Kakao Money, which lets you charge and spend cash as much as you want. Muzi decided to start using Kakao Money today to spend cash conveniently. To use the service more easily, Muzi linked his own bank account, which has a balance of 10,100 won, to his Kakao Money account.
Initially, Muzi's Kakao Money balance is 0 won. When Muzi charges his balance from his bank account or receives a transfer from someone else, the Kakao Money balance increases, and this is called a deposit. When Muzi pays with Kakao Money or sends a transfer to someone else, the Kakao Money balance decreases, and this is called a withdrawal. In this problem, assume that there are no restrictions on deposits or withdrawals other than that the amount must be in units of 1 won. That is, ignore the real Kakao Money restrictions such as a balance limit of 2 million won and a daily transfer limit of 500,000 won.
When won is deposited, Muzi's Kakao Money balance increases by won. However, withdrawing won works differently. If the balance is at least won, subtract won from the balance. But if the balance is less than won, Kakao Money cannot cover the amount internally, so it must pull money from the linked bank account. To do this, Kakao sets a minimum charge unit : it keeps pulling won from the bank account until the balance is at least won, then subtracts won from the balance. is a positive integer.
For example, suppose , Muzi's balance is 1,500 won, and he wants to withdraw won. Since Muzi's balance cannot cover won, Kakao Money first pulls won from Muzi's bank account, making the balance 11,500 won. But 11,500 won still cannot cover won, so Kakao Money pulls another won from Muzi's bank account, making the balance 21,500 won. Now it can withdraw 17,000 won, so it subtracts won from the balance. In the end, Muzi's Kakao Money balance becomes won.
Apart from the transaction history kept by Kakao Money, Muzi has been writing his own deposit and withdrawal log since he started using Kakao Money. This log consists of pairs of integers , stored in chronological order. Muzi is careful, so he believes he has not missed any deposit or withdrawal in the log. The meaning of each pair is as follows.
- If , then won was deposited into Muzi's Kakao Money. As a result of the deposit, the balance was won.
- If , then won was withdrawn from Muzi's Kakao Money. As a result of the withdrawal, the final balance was won.
- There is no case where .
For the example above, the pair would have been added to Muzi's log.
However, Muzi worries that he might not be managing his log correctly, so he wants to simply check whether the log is consistent. His idea is to look only at the log and determine whether there exists a minimum charge unit that makes the log valid, meaning it does not create a contradiction, and if so, what its value is. Write a program to do this for Muzi.
Input
The first line gives the number of pairs in Muzi's log ().
The next lines give the log Muzi wrote. The -th line () contains two integers and (, , ) separated by a single space.
Output
If a valid minimum charge unit exists (), print on the first line. If there are multiple possible values, print any one of them that is at most .
If it does not exist, print -1.