#include <bits/stdc++.h>
using namespace std;
const int64_t I64 = int64_t(2e18) + 5;
int a, b;
string doi(int c) {
  string s = "";
  while (c) {
    int x = c % 3;
    s += char(x + '0');
    c /= 3;
  }
  reverse(s.begin(), s.end());
  return s;
}
int cov(string s) {
  int res = 0, n = s.size() - 1;
  for (char i : s) {
    int c = i - '0';
    res += (c * (pow(3, n)));
    n--;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> a >> b;
  string x = doi(a);
  string y = doi(b);
  int m = max(x.size(), y.size());
  while (x.size() < m) {
    x = '0' + x;
  }
  while (y.size() < m) {
    y = '0' + y;
  }
  string res = "";
  for (int i = 0; i < m; ++i) {
    int c = x[i] - '0';
    int d = y[i] - '0';
    if (d >= c)
      res += (d - c) + '0';
    else {
      int v = c - d;
      res += 3 - v + '0';
    }
  }
  cout << cov(res);
  return 0;
}
