#include <bits/stdc++.h>
using namespace std;
string base3(int x) {
  if (x == 0)
    return "";
  else {
    string s = base3(x / 3);
    s.push_back(x % 3 + '0');
    return s;
  }
}
int base10(char *c, int n) {
  int mult = 1;
  int risp = 0;
  for (int i = n - 1; i >= 0; i--) {
    risp += (c[i] - '0') * mult;
    mult *= 3;
  }
  return risp;
}
int main() {
  int a, c;
  cin >> a >> c;
  string sa = base3(a);
  string sc = base3(c);
  while (sa.length() < sc.length()) {
    sa = "0" + sa;
  }
  while (sc.length() < sa.length()) {
    sc = "0" + sc;
  }
  int n = sa.length();
  char sb[n + 1];
  sb[n] = '\0';
  for (int i = 0; i < n; i++) {
    if (sa[i] == '0' && sc[i] == '0')
      sb[i] = '0';
    else if (sa[i] == '0' && sc[i] == '1')
      sb[i] = '1';
    else if (sa[i] == '0' && sc[i] == '2')
      sb[i] = '2';
    else if (sa[i] == '1' && sc[i] == '0')
      sb[i] = '2';
    else if (sa[i] == '1' && sc[i] == '1')
      sb[i] = '0';
    else if (sa[i] == '1' && sc[i] == '2')
      sb[i] = '1';
    else if (sa[i] == '2' && sc[i] == '0')
      sb[i] = '1';
    else if (sa[i] == '2' && sc[i] == '1')
      sb[i] = '2';
    else if (sa[i] == '2' && sc[i] == '2')
      sb[i] = '0';
  }
  cout << base10(sb, n);
}
