#include <bits/stdc++.h>
int getTernary(int a, int radix, int ta[]) {
  int j = 0;
  while (a > 0) {
    ta[j] = a % radix;
    a = a / radix;
    ++j;
  }
  return --j;
}
int main() {
  int a, b, c;
  int ina, inc, index;
  int i;
  int ta[20];
  int tb[20];
  int tc[20];
  scanf("%d%d", &a, &c);
  memset(ta, 0, 20 * sizeof(int));
  memset(tb, 0, 20 * sizeof(int));
  memset(tc, 0, 20 * sizeof(int));
  ina = getTernary(a, 3, ta);
  inc = getTernary(c, 3, tc);
  if (ina >= inc) {
    index = ina;
  } else {
    index = inc;
  }
  for (i = 0; i <= index; ++i) {
    if (tc[i] >= ta[i])
      tb[i] = tc[i] - ta[i];
    else
      tb[i] = tc[i] + 3 - ta[i];
  }
  b = 0;
  for (i = 0; i <= index; ++i) {
    b = b + tb[i] * pow((double)3, i);
  }
  printf("%d\n", b);
  return 0;
}
