#include <bits/stdc++.h>
using namespace std;
long long fix(long long cur, long long m) { return ((cur % m) + m) % m; }
long long fast_power(long long x, long long y, long long m) {
  if (y == 0) return 1;
  long long temp = fast_power(x, y / 2, m);
  temp = (temp * temp) % m;
  if (y % 2 != 0) temp = (temp * x) % m;
  return temp;
}
long long n, m;
string a, c;
string convToBinaryString(long long x) {
  string tmp = "";
  while (x) {
    tmp += ((x % 3) + '0');
    x /= 3;
  }
  reverse(tmp.begin(), tmp.end());
  return tmp;
}
long long ternaryToint(string x) {
  long long res = 0;
  for (int i = x.size() - 1, j = 0; i >= 0; i--, j++) {
    if (x[i] != '0') res += (pow(3, j) * (x[i] - '0'));
  }
  return res;
}
int main() {
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin >> n >> m;
  a = convToBinaryString(n);
  c = convToBinaryString(m);
  int sz = max(a.size(), c.size());
  while (a.size() != sz) a = '0' + a;
  while (c.size() != sz) c = '0' + c;
  string tmp = "";
  for (int i = 0; i < c.size(); i++) {
    long long x = c[i];
    long long y = a[i];
    long long res = x - y;
    res = fix(res, 3);
    tmp += (res + '0');
  }
  long long ans = ternaryToint(tmp);
  cout << ans << endl;
  return 0;
}
