#include <bits/stdc++.h>
using namespace std;
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  long long a, c, s = 0, i = 1, k;
  cin >> a >> c;
  while (a || c) {
    k = ((c % 3) - (a % 3) + 3) % 3;
    s += k * i;
    i *= 3;
    a /= 3;
    c /= 3;
  }
  cout << s;
}
