#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k, sum = 0, a[35], b[35], c[35], n, m;
  scanf("%d%d", &n, &m);
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(c, 0, sizeof(c));
  i = 0;
  j = 0;
  while (n) {
    a[i++] = n % 3;
    n /= 3;
  }
  while (m) {
    c[j++] = m % 3;
    m /= 3;
  }
  for (k = 0; k < (i > j ? i : j); k++) b[k] = (c[k] + 3 - a[k]) % 3;
  for (k = k - 1; k >= 0; k--) sum = sum * 3 + b[k];
  printf("%d", sum);
  return 0;
}
