#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int a, c;
  cin >> a >> c;
  int ta[20] = {0};
  int tb[20] = {0};
  int aptr = 0, cptr = 0;
  while (a != 0) {
    ta[aptr++] = a % 3;
    a /= 3;
  }
  while (c != 0) {
    tb[cptr++] = c % 3;
    c /= 3;
  }
  int res = 0;
  for (int i = max(aptr, cptr) - 1; i >= 0; --i) {
    res = res * 3 + (tb[i] - ta[i] + 3) % 3;
  }
  cout << res << "\n";
  return 0;
}
