#include <bits/stdc++.h>
using namespace std;
string tobase3(int x) {
  string s;
  while (x > 0) {
    s += (x % 3 + '0');
    x /= 3;
  }
  return s;
}
string sub(string x, string y) {
  string s;
  int i;
  while (x.size() < y.size()) x += '0';
  while (y.size() < x.size()) y += '0';
  for (i = 0; i < x.size(); i++) {
    while (y[i] < x[i]) y[i] += 3;
    s += y[i] - x[i] + '0';
  }
  return s;
}
int tobase10(string s) {
  int i, x = 0;
  for (i = s.size() - 1; i >= 0; i--) {
    x *= 3;
    x += (s[i] - '0');
  }
  return x;
}
int main() {
  int a, c;
  scanf("%d %d", &a, &c);
  printf("%d", tobase10(sub(tobase3(a), tobase3(c))));
}
