Trade Agreements in Amber#596

Trade Agreements in Amber

1 second
64 megabytes
Very easy

In the country of Amber, trade and financial relationships are quite complex. If one merchant wants to exchange their goods for those of another merchant, they must go to the Amber Trade Guild. You might think that merchants would simply exchange goods and leave happily after a good deal? Not so! The laws in Amber require that the Trade Guild can profit from a transaction between two merchants only if the «normalizing coefficients» of both merchants are equal. Do you know what a «normalizing coefficient» is? That's not surprising...

Suppose a merchant is years old. In the country where they come from, the numeral system with base is used for monetary operations. The «normalizing coefficient» of this merchant is defined as the sum of the digits of in base . Write a program to determine how much the Trade Guild can earn after a deal between two merchants.

Input

The first line contains information about the first merchant: the numbers N_1 and P_1. The second line contains information about the second merchant: the numbers N_2 and P_2. Here, N_1 and N_2 are the ages of the merchants, 0 \leq N_1, N_2 \leq 2147483647. P_1 and P_2 are the bases of the numeral systems used in their respective countries, 2 \leq P_1, P_2 \leq 16.

Output

Output the amount that the Trade Guild will earn after the transaction between the merchants.

Examples

Example 1

Loading
Loading