#include <bits/stdc++.h>
using namespace std;
int N = 500, status[5001];
int main() {
  int a, c, p, s = 0;
  cin >> a >> c;
  p = 1;
  while (!(!a && !c)) {
    s += ((c % 3) - (a % 3) + 3) % 3 * p;
    a /= 3;
    c /= 3;
    p *= 3;
  }
  cout << s << endl;
  return 0;
}
