#include <bits/stdc++.h>
using namespace std;
void makeEqual(string& s, int n) {
  string temp;
  for (int i = 0; i < n; i++) {
    temp += '0';
  }
  s += temp;
}
string reverse(string a, string c) {
  string temp = "";
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < 3; j++) {
      if ((a[i] - '0' + j) % 3 == c[i] - '0') temp += j + '0';
    }
  }
  temp[a.size()] = '\0';
  return temp;
}
int TertoDec(string s) {
  int num = 0, fact = 1;
  for (int i = 0; i < s.size(); i++) {
    num += (s[i] - '0') * fact;
    fact *= 3;
  }
  return num;
}
string DecToTer(int i) {
  string temp;
  int num = 0;
  char c;
  while (i) {
    num = i % 3;
    i /= 3;
    c = num + '0';
    temp += c;
  }
  return temp;
}
int main() {
  int a, c;
  cin >> a >> c;
  string as = DecToTer(a);
  string cs = DecToTer(c);
  if (cs.size() > as.size()) {
    makeEqual(as, cs.size() - as.size());
  } else if (as.size() > cs.size()) {
    makeEqual(cs, as.size() - cs.size());
  }
  string ans = reverse(as, cs);
  int answer = TertoDec(ans);
  cout << answer << endl;
  return 0;
}
