#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int a, b = 0, c, at, ct, bt;
  cin >> a >> c;
  string bs = "";
  while (1) {
    at = a % 3;
    ct = c % 3;
    if (ct < at) ct += 3;
    bt = ct - at;
    char ch = bt + '0';
    bs += ch;
    a /= 3;
    c /= 3;
    if (a == 0 and c == 0) break;
  }
  long long int n = bs.length();
  for (long long int i = 0; i < n; i++) b += pow(3, i) * (bs[i] - '0');
  cout << b;
  cerr << "time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
  return 0;
}
