#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long inf = 1e18;
long long A, B;
vector<long long> a, b;
vector<long long> cnvrt(long long x) {
  vector<long long> q;
  while (x) {
    q.push_back(x % 3);
    x /= 3;
  }
  return q;
}
void solve() {
  cin >> A >> B;
  a = cnvrt(A);
  b = cnvrt(B);
  while ((long long)a.size() < (long long)b.size()) a.push_back(0);
  while ((long long)b.size() < (long long)a.size()) b.push_back(0);
  long long ans = 0;
  long long p = 1;
  for (long long i = (0); i < (a.size()); i++) {
    long long num;
    if (a[i] <= b[i])
      num = b[i] - a[i];
    else
      num = b[i] + 3 - a[i];
    ans += num * p;
    p *= 3;
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
