#include <bits/stdc++.h>
using namespace std;
int n, i, j, ans, arra[25], arrb[25], arrc[25], a, c, temp, cnt;
int main() {
  cin >> a >> c;
  temp = a;
  for (i = 19; i >= 0; i--)
    if (temp / pow(3, i)) {
      cnt = pow(3, i);
      arra[i] += temp / cnt, temp %= cnt;
    }
  temp = c;
  for (i = 19; i >= 0; i--)
    if (temp / pow(3, i)) cnt = pow(3, i), arrc[i] += temp / cnt, temp %= cnt;
  for (i = 0; i < 20; i++) {
    switch (arra[i]) {
      case 2:
        if (arrc[i] == 0)
          arrb[i] = 1;
        else if (arrc[i] == 1)
          arrb[i] = 2;
        else
          arrb[i] = 0;
        break;
      case 1:
        if (arrc[i] == 0)
          arrb[i] = 2;
        else if (arrc[i] == 1)
          arrb[i] = 0;
        else
          arrb[i] = 1;
        break;
      case 0:
        if (arrc[i] == 0)
          arrb[i] = 0;
        else if (arrc[i] == 1)
          arrb[i] = 1;
        else
          arrb[i] = 2;
        break;
    }
  }
  for (i = 0; i < 20; i++) cnt = pow(3, i), ans += arrb[i] * cnt;
  cout << ans << endl;
}
