#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}
struct greaterr {
  template <class T>
  bool operator()(T const &a, T const &b) const {
    return a > b;
  }
};
int main() {
  std::ios::sync_with_stdio(false);
  int a, b;
  cin >> a >> b;
  vector<int> x, y, z;
  while (a || b) {
    x.push_back(a % 3), a /= 3;
    y.push_back(b % 3), b /= 3;
  }
  reverse(x.begin(), x.end());
  reverse(y.begin(), y.end());
  for (int i = 0; i < x.size(); i++) {
    for (int j = 0; j <= 2; j++) {
      if ((x[i] + j) % 3 == y[i]) {
        z.push_back(j);
        break;
      }
    }
  }
  int ans = 0;
  for (__typeof__(z.begin()) it = z.begin(); it != z.end(); it++)
    ans = ans * 3 + *it;
  cout << ans;
  return 0;
}
