#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int main() {
  long long a, c;
  while (cin >> a >> c) {
    string sa, sc;
    while (a) {
      sa = (char)(a % 3 + '0') + sa;
      a /= 3;
    }
    while (c) {
      sc = (char)(c % 3 + '0') + sc;
      c /= 3;
    }
    int la = sa.size(), lc = sc.size();
    if (sa.size() < sc.size()) {
      for (int i = 0; i < lc - la; i++) sa = '0' + sa;
    } else if (sa.size() > sc.size()) {
      for (int i = 0; i < la - lc; i++) sc = '0' + sc;
    }
    string sb;
    for (int i = 0; i < sa.size(); i++) {
      sb += (char)((sc[i] - sa[i] + 3) % 3 + '0');
    }
    long long ans = 0, ba = 1;
    for (int i = sb.size() - 1; i >= 0; i--) {
      ans = ans + (sb[i] - '0') * ba;
      ba *= 3;
    }
    cout << ans << endl;
  }
  return 0;
}
