#include <bits/stdc++.h>
using namespace std;
long int a, c, b, u;
int main() {
  cin >> a >> c;
  for (b = 0, u = 1; a || c; a /= 3, c /= 3, u *= 3) {
    b += (c % 3 - a % 3 + 3) % 3 * u;
  }
  cout << b;
  return 0;
}
