#include <bits/stdc++.h>
using namespace std;
void rev(vector<unsigned>& arr, unsigned digit) {
  while (digit) {
    arr.push_back(digit % 3);
    digit /= 3;
  }
}
int main(int argv, char* argc[]) {
  unsigned a, c;
  cin >> a >> c;
  vector<unsigned> a_arr(0);
  rev(a_arr, a);
  vector<unsigned> c_arr(0);
  rev(c_arr, c);
  while (c_arr.size() > a_arr.size()) {
    a_arr.push_back(0);
  }
  while (a_arr.size() > c_arr.size()) {
    c_arr.push_back(0);
  }
  reverse(c_arr.begin(), c_arr.end());
  reverse(a_arr.begin(), a_arr.end());
  vector<unsigned> b_arr(0);
  for (unsigned i = 0; i < c_arr.size(); i++) {
    for (unsigned j = 0; j < 3; j++) {
      if ((a_arr[i] + j) % 3 == c_arr[i]) {
        b_arr.push_back(j);
      }
    }
  }
  reverse(b_arr.begin(), b_arr.end());
  unsigned digit = 0;
  for (unsigned i = 0; i < b_arr.size(); i++) {
    digit += b_arr[i] * pow(3.0, (double)i);
  }
  cout << digit << endl;
  return 0;
}
