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