#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b;
  while (cin >> a >> b) {
    string s, st;
    while (a) {
      s += char('0' + a % 3);
      a /= 3;
    }
    while (b) {
      st += char('0' + b % 3);
      b /= 3;
    }
    while (s.size() < st.size()) s += "0";
    while (s.size() > st.size()) st += "0";
    string ss;
    for (int i = 0; i < s.size(); i++) {
      ss += char('0' + (st[i] - '0' + 12 - s[i] + '0') % 3);
    }
    long long cont = 1;
    long long res = 0;
    for (int i = 0; i < ss.size(); i++) {
      res += (ss[i] - '0') * cont;
      cont *= 3;
    }
    cout << res << endl;
  }
}
