#include <bits/stdc++.h>
using namespace std;
int conversion(string p) {
  int o;
  o = atoi(p.c_str());
  return o;
}
string toString(int h) {
  stringstream ss;
  ss << h;
  return ss.str();
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
long long toi(string p) {
  long long x;
  istringstream in(p);
  in >> x;
  return x;
}
string tern(int x) {
  string res = "";
  while (x / 3 > 0) {
    res += toString(x % 3);
    x /= 3;
  }
  res += toString(x);
  reverse(res.begin(), res.end());
  return res;
}
int main() {
  int a, c;
  cin >> a >> c;
  long long num = 0;
  string x = tern(a), y = tern(c), res = "";
  if (x.size() < y.size())
    x = string(y.size() - x.size(), '0') + x;
  else
    y = string(x.size() - y.size(), '0') + y;
  for (int i = x.size() - 1; i >= 0; i--) {
    for (int j = 0; j <= 2; j++) {
      if ((x[i] - '0' + j) % 3 == (y[i] - '0')) {
        res += toString(j);
        break;
      }
    }
  }
  reverse(res.begin(), res.end());
  for (int i = res.size() - 1; i >= 0; i--)
    num += ((long long)pow(3, res.size() - i - 1) * (long long)(res[i] - '0'));
  cout << num << endl;
}
