#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int a, c;
  cin >> a >> c;
  vector<int> trc;
  vector<int> tra;
  stack<int> tr1;
  stack<int> tr2;
  while ((c >= 3) || (a >= 3)) {
    int r = c % 3;
    int h = a % 3;
    tr1.push(r);
    tr2.push(h);
    c = (c - r) / 3;
    a = (a - h) / 3;
  }
  tr1.push(c);
  tr2.push(a);
  while (!tr1.empty()) {
    trc.push_back(tr1.top());
    tr1.pop();
    tra.push_back(tr2.top());
    tr2.pop();
  }
  int ans = 0;
  int three = 1;
  for (int i = (int)trc.size() - 1; i >= 0; i--) {
    int y;
    if (tra[i] > trc[i]) {
      if (tra[i] == 1) y = 2;
      if (tra[i] == 2) y = 2 - (trc[i] != 1);
    } else {
      if (tra[i] <= trc[i]) y = (trc[i] - tra[i]) % 3;
    }
    ans += (three * y);
    three *= 3;
  }
  cout << ans;
}
