#include <bits/stdc++.h>
using namespace std;
long long bp(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
int main() {
  long long a, c;
  cin >> a >> c;
  vector<long long> A, C;
  long long aa = a, cc = c;
  while (aa) {
    A.push_back(aa % 3);
    aa /= 3;
  }
  while (cc) {
    C.push_back(cc % 3);
    cc /= 3;
  }
  long long la = A.size(), lc = C.size();
  if (la < lc) {
    la = lc - la;
    for (long long i = 0; i < la; i++) A.push_back(0);
    la = A.size();
  } else if (lc < la) {
    lc = la - lc;
    for (long long i = 0; i < lc; i++) C.push_back(0);
    lc = C.size();
  }
  vector<long long> B;
  for (long long i = 0; i < la; i++) {
    if (A[i] == 0)
      B.push_back(C[i]);
    else if (A[i] == 1) {
      if (C[i] == 0)
        B.push_back(2);
      else if (C[i] == 1)
        B.push_back(0);
      else
        B.push_back(1);
    } else {
      if (C[i] == 0)
        B.push_back(1);
      else if (C[i] == 1)
        B.push_back(2);
      else
        B.push_back(0);
    }
  }
  long long ans = 0;
  for (long long i = 0; i < B.size(); i++) {
    long long x = bp(3, i);
    ans += x * B[i];
  }
  cout << ans << '\n';
  return 0;
}
