#include <bits/stdc++.h>
using namespace std;
string t(int a) {
  int i, tmp = 1;
  for (i = 1; i <= a; i *= 3) {
  }
  string s;
  for (; i >= 1; i /= 3) {
    if (a / i == 2)
      s += '2', a %= i;
    else if (a / i == 1)
      s += '1', a %= i;
    else
      s += '0';
  }
  return s;
}
int main() {
  int i, a, b = 0, c;
  cin >> a >> c;
  string s1, s2, s3;
  s1 = t(a), s2 = t(c);
  int t = max(((int)s1.size()), ((int)s2.size()));
  for (i = 0; i < t; i++) {
    if (((int)s1.size()) - t + i >= 0)
      a = s1[((int)s1.size()) - t + i] - '0';
    else
      a = 0;
    if (((int)s2.size()) - t + i >= 0)
      c = s2[((int)s2.size()) - t + i] - '0';
    else
      c = 0;
    s3 += ((c - a + 3) % 3 + '0');
  }
  reverse(s3.begin(), s3.end());
  for (i = 0; i < ((int)s3.size()); i++) {
    t = 1;
    for (int j = 0; j < i; j++) t *= 3;
    b += (s3[i] - '0') * t;
  }
  cout << b << endl;
}
