#include <bits/stdc++.h>
using namespace std;
inline string get(int& n) {
  if (n == 2) return "2";
  if (n == 1) return "1";
  return "0";
}
inline string ToBase3(int n) {
  string ret = "";
  while (n != 0) {
    int t = n % 3;
    ret = get(t) + ret;
    n /= 3;
  }
  return ret;
}
inline int ToBase10(vector<int> v) {
  int ret = 0;
  int t = 1;
  for (int i = 0; i < (v).size(); ++i) {
    ret = ret + (t * v[i]);
    t *= 3;
  }
  return ret;
}
map<pair<int, int>, int> sol;
inline void init() {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      int k = 0;
      while ((i + k) % 3 != j) ++k;
      sol[make_pair(i, j)] = k;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(0);
  init();
  int x, z;
  cin >> x >> z;
  string a = ToBase3(x);
  string c = ToBase3(z);
  while ((a).size() < (c).size()) a = "0" + a;
  while ((c).size() < (a).size()) c = "0" + c;
  vector<int> ans((a).size());
  for (int i = 0; i < (a).size(); ++i) {
    ans[i] = sol[make_pair(a[i] - '0', c[i] - '0')];
  }
  reverse((ans).begin(), (ans).end());
  { std::cout << ToBase10(ans) << '\n'; };
  return 0;
}
