#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, i, j, sum, p[3][3];
  stack<int> s;
  queue<int> q1, q2;
  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++) p[i][j] = (j + 3 - i) % 3;
  while (s.size()) s.pop();
  while (q1.size()) q1.pop();
  while (q2.size()) q2.pop();
  scanf("%d%d", &a, &b);
  while (a) {
    q1.push(a % 3);
    a /= 3;
  }
  while (b) {
    q2.push(b % 3);
    b /= 3;
  }
  if (q1.size() > q2.size())
    for (i = q2.size(); i < q1.size(); i++) q2.push(0);
  else {
    if (q2.size() > q1.size())
      for (i = q1.size(); i < q2.size(); i++) q1.push(0);
  }
  while (q1.size()) {
    a = q1.front();
    q1.pop();
    b = q2.front();
    q2.pop();
    s.push(p[a][b]);
  }
  sum = 0;
  while (s.size()) {
    c = s.top();
    s.pop();
    sum = sum * 3 + c;
  }
  printf("%d\n", sum);
  return 0;
}
