#include <bits/stdc++.h>
using namespace std;
int a, c, b, k = 1, bs;
int main() {
  cin >> a >> c;
  while (!(a == 0 && c == 0)) {
    bs = ((c % 3) + 3 - (a % 3)) % 3;
    b += bs * k;
    k *= 3;
    c /= 3;
    a /= 3;
  }
  cout << b;
  return 0;
}
