#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, -1, 1};
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int a, c;
  vector<int> x, y;
  cin >> a >> c;
  while (a) {
    x.push_back(a % 3), a /= 3;
  }
  while (c) {
    y.push_back(c % 3), c /= 3;
  }
  a = max(x.size(), y.size());
  if (x.size() < a) {
    for (int i = x.size(); i < a; ++i) x.push_back(0);
  }
  if (y.size() < a) {
    for (int i = y.size(); i < a; ++i) y.push_back(0);
  }
  for (int i = 0; i < a; ++i) {
    c = y[i] - x[i];
    if (c < 0)
      y[i] = c + 3;
    else
      y[i] = c;
  }
  long long ans = 0, z = 0;
  for (int i = 0; i < a; ++i) {
    z = pow(3, i);
    ans += (z * y[i]);
  }
  cout << ans;
  return 0;
}
