SCUSA Region ICPC Masthead ACM Balloon Logo
2015 ACM ICPC South Central USA Regional Programming Contest
Positive Con Sequences

E - Positive Con Sequences

Your younger sister is studying for an upcoming standardized test in mathematics. She needs practice with the common style of problem in which the student is asked to fill in the missing value in a sequence of numbers.

The vast majority of these problems feature either arithmetic sequences (where each number in the sequence is formed by adding an integer constant to the prior number) or geometric sequences (where each number in the sequence is formed by multiplying the prior number by an integer constant).

Write a program that will help your sister practice on this style of problem by allowing her to check her answers on sample problems.


Input will consist of one or more datasets.

Each dataset will be a single line containing 4 integers defining a sequence. One of these will be -1, denoting the missing value. The remainder will be positive integers in the range 1..10,000, inclusive. Other than the -1 placeholder value, the values will be in non-decreasing order.

End of input will be signaled by a line containing four -1 values.


For each dataset, print one line of output.

If an integer in the range 1..10,000 inclusive exists that can be filled in to the missing value position to create an arithmetic or geometric sequence, print that missing value.

If there is no such positive integer, print -1.

Sample Input

1 2 -1 4
2 4 8 -1
7 8 -1 21
5 -1 11 14
-1 2 4 6
-1 -1 -1 -1

Sample Output