#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long a, c;
  cin >> a >> c;
  if (a + c == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<long long> aa, cc;
  long long tmp = a;
  while (tmp) aa.push_back(tmp % 3), tmp /= 3;
  tmp = c;
  while (tmp) cc.push_back(tmp % 3), tmp /= 3;
  int mn = abs((int)aa.size() - (int)cc.size());
  if (aa.size() > cc.size()) {
    for (int i = 0; i < mn; i++) cc.push_back(0LL);
  } else {
    for (int i = 0; i < mn; i++) aa.push_back(0LL);
  }
  vector<long long> bb;
  for (int i = 0; i < aa.size(); i++) {
    if (cc[i] < aa[i])
      bb.push_back(cc[i] + 3LL - aa[i]);
    else
      bb.push_back(cc[i] - aa[i]);
  }
  long long b = bb[0], p = 1;
  for (int i = 1; i < bb.size(); i++) p *= 3, b += (p * bb[i]);
  cout << b << endl;
  return 0;
}
