#include <bits/stdc++.h>
using namespace std;
long long a, b, q, answ;
vector<long long> ans;
int main() {
  cin >> a >> b;
  for (int i = 1; i <= 20; i++) {
    q = b % 3 - a % 3;
    q += 102;
    q %= 3;
    ans.push_back(q);
    b /= 3;
    a /= 3;
  }
  for (int i = ans.size(); i; --i) {
    answ = answ * 3 + ans[i - 1];
  }
  cout << answ << endl;
  cin.get();
  cin.get();
  return 0;
}
