#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, c;
  cin >> a >> c;
  vector<long long> aa;
  while (a) {
    aa.push_back(a % 3);
    a /= 3;
  }
  vector<long long> cc;
  while (c) {
    cc.push_back(c % 3);
    c /= 3;
  }
  long long i = 0, j = 0;
  vector<long long> res;
  while (i < aa.size() && j < cc.size()) {
    res.push_back((cc[j] - aa[i] + 3) % 3);
    i++;
    j++;
  }
  while (i < aa.size()) {
    res.push_back((3 - aa[i]) % 3);
    i++;
  }
  while (j < cc.size()) {
    res.push_back(cc[j] % 3);
    j++;
  }
  long long p = 1;
  long long ans = 0;
  for (int i = 0; i < res.size(); i++) {
    ans += (res[i] * (p));
    p *= 3;
  }
  cout << ans << endl;
  return 0;
}
