Positive Con Sequences

Fill the missing entry of each four-term sequence with a number from 1 to 10000 that makes it arithmetic or geometric, or output -1.

Easy3MathBrute forceInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

Your younger sister is preparing for a mathematics exam, and she keeps meeting problems that ask her to fill in the missing term of a number sequence. Almost every sequence of that style is either arithmetic or geometric. An arithmetic sequence adds a fixed integer to the previous term to form the next one, and a geometric sequence multiplies the previous term by a fixed integer.

Write a program that computes the missing term so she can check her answers.

Input

The input consists of one or more datasets.

Each dataset is one line holding 4 integers separated by spaces that describe a sequence. Exactly one of them is -1, which marks the position of the missing term. The other three are integers between 1 and 10,000 inclusive, and once the -1 position is ignored they are given in non-decreasing order.

The end of the input is a line containing four values of -1.

Output

Print one line for each dataset.

If an integer between 1 and 10,000 inclusive can be placed at the missing position so that the whole sequence becomes arithmetic or geometric, print that integer.

If no such integer exists, print -1.