#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  string sa, sb, ans;
  unsigned long long g = 0;
  cin >> a >> b;
  while (a) {
    sa += a % 3 + '0';
    a /= 3;
  }
  while (b) {
    sb += b % 3 + '0';
    b /= 3;
  }
  while (sa.size() != sb.size()) {
    if (sa.size() < sb.size())
      sa += '0';
    else
      sb += '0';
  }
  for (int i = 0; i < sa.size(); i++) {
    ans += ((sb[i] - '0') - (sa[i] - '0') + 3) % 3 + '0';
  }
  for (unsigned long long mul = 1, i = 0; i < ans.size(); i++, mul *= 3) {
    g += (ans[i] - '0') * mul;
  }
  cout << g << endl;
  return 0;
}
