#include <bits/stdc++.h>
using namespace std;
vector<int> get(int a) {
  vector<int> res;
  while (a > 0) {
    res.push_back(a % 3);
    a /= 3;
  }
  reverse(res.begin(), res.end());
  return res;
}
void print_(vector<int> a) {
  for (int i = 0; i < a.size(); ++i) cout << a[i] << " ";
  cout << endl;
}
int main() {
  long long a = 2376234, c = 0;
  cin >> a;
  cin >> c;
  vector<int> a_ = get(a);
  vector<int> c_ = get(c);
  int i = a_.size() - 1;
  int k = c_.size() - 1;
  vector<int> z(max(i, k) + 1, 0);
  while (i >= 0 && k >= 0) {
    z[max(i, k)] = c_[k] - a_[i];
    --i;
    --k;
  }
  if (k == -1) {
    for (int j = i; j >= 0; --j) {
      z[j] = -a_[j];
    }
  }
  if (i == -1) {
    for (int j = k; j >= 0; --j) {
      z[j] = c_[j];
    }
  }
  int size = z.size() - 1;
  long long st = 1;
  while (size >= 0) {
    int f = z[size];
    while (f < 0) f += 3;
    z[size] = f % 3;
    --size;
    st *= 3;
  }
  st /= 3;
  long long res = 0;
  for (int b = 0; b < z.size(); ++b) {
    res += st * z[b];
    st /= 3;
  }
  cout << res;
  return EXIT_SUCCESS;
}
