#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, a, c;
  long long r = 0;
  cin >> a >> c;
  vector<int> va, vb, vc;
  while (a || c) {
    va.push_back(a % 3);
    a /= 3;
    vc.push_back(c % 3);
    c /= 3;
  }
  for (i = 0; i < va.size(); i++) {
    vb.push_back((vc.at(i) + 3 - va.at(i)) % 3);
  }
  for (i = vb.size() - 1; i >= 0; --i) {
    r = r * 3 + vb.at(i);
  }
  cout << r;
  return 0;
}
