#include <bits/stdc++.h>
using namespace std;
vector<long long> adig;
vector<long long> cdig;
void getdig(long long a, long long c) {
  adig.clear(), cdig.clear();
  while (a) {
    adig.push_back(a % 3);
    a /= 3;
  }
  while (c) {
    cdig.push_back(c % 3);
    c /= 3;
  }
  int asize = adig.size(), csize = cdig.size();
  if (asize <= csize) {
    for (int i = 0; i < csize - asize; i++) adig.push_back(0);
  } else {
    for (int i = 0; i < asize - csize; i++) cdig.push_back(0);
  }
  reverse(adig.begin(), adig.end());
  reverse(cdig.begin(), cdig.end());
}
long long getmin(int a, int c) {
  if (a == 0) {
    if (c == 0) return 0;
    if (c == 1) return 1;
    if (c == 2) return 2;
  } else if (a == 1) {
    if (c == 0) return 2;
    if (c == 1) return 0;
    if (c == 2) return 1;
  } else if (a == 2) {
    if (c == 0) return 1;
    if (c == 1) return 2;
    if (c == 2) return 0;
  }
  return -1;
}
long long getval() {
  long long re = 0;
  vector<long long> bdig;
  for (int i = 0; i < (int)adig.size(); i++) {
    bdig.push_back(getmin(adig[i], cdig[i]));
  }
  for (int i = 0; i < (int)bdig.size(); i++) re = re * 3 + bdig[i];
  return re;
}
int main() {
  long long a, c;
  while (cin >> a >> c) {
    getdig(a, c);
    cout << getval() << endl;
  }
  return 0;
}
