Algorithm


B. Ternary Logic
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya very much likes computers. Recently he has received a new "Ternatron IV" as a gift from his mother. Unlike other modern computers, "Ternatron IV" operates with ternary and not binary logic. Petya immediately wondered how the xor operation is performed on this computer (and whether there is anything like it).

It turned out that the operation does exist (however, it is called tor) and it works like this. Suppose that we need to calculate the value of the expression a tor b. Both numbers a and b are written in the ternary notation one under the other one (b under a). If they have a different number of digits, then leading zeroes are added to the shorter number until the lengths are the same. Then the numbers are summed together digit by digit. The result of summing each two digits is calculated modulo 3. Note that there is no carry between digits (i. e. during this operation the digits aren't transferred). For example: 1410 tor 5010 = 01123 tor 12123 = 10213 = 3410.

Petya wrote numbers a and c on a piece of paper. Help him find such number b, that a tor b = c. If there are several such numbers, print the smallest one.

Input

The first line contains two integers a and c (0 ≤ a, c ≤ 109). Both numbers are written in decimal notation.

Output

Print the single integer b, such that a tor b = c. If there are several possible numbers b, print the smallest one. You should print the number in decimal notation.

Examples
input
Copy
14 34
output
Copy
50
input
Copy
50 34
output
Copy
14
input
Copy
387420489 225159023
output
Copy
1000000001
input
Copy
5 5
output
Copy
0



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#include <vector>

std::vector<int> decimalToTernary(long x){
    std::vector<int> output;
    while(x > 0){output.push_back(x % 3);x /= 3;}
    return output;
}

long ternaryToDecimal(std::vector<int> x){
    long output(0);
    for(int k = x.size() - 1; k >= 0; k--){output = 3 * output + x[k];}
    return output;
}

std::vector<int> inverseTorOperation(std::vector<int> x, std::vector<int> y){
    std::vector<int> output;
    for(int k = 0; k < x.size() || k < y.size(); k++){
        int temp(0);
        if(k < x.size()){temp += 3 + x[k];} 
        if(k < y.size()){temp += 3 - y[k];} 
        output.push_back(temp % 3); 
    }
    return output;
}

int main(){
    long a, c; scanf("%ld %ld", &a, &c);
    printf("%ld\n", ternaryToDecimal(inverseTorOperation(decimalToTernary(c), decimalToTernary(a))));
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
14 34

Output

x
+
cmd
50
Advertisements

Demonstration


Codeforces Solution-B. Ternary Logic-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+