#include <bits/stdc++.h>
int main() {
  int i, cnt1 = 0, l, diff, a, c, fck;
  scanf("%d%d", &a, &c);
  l = a;
  diff = 0;
  while (l > 0) {
    if (l % 3) diff += (3 - (l % 3)) * pow(3, cnt1);
    l /= 3;
    cnt1++;
  }
  cnt1 = 0;
  l = 0;
  while (c || diff) {
    fck = c + diff;
    l += fck % 3 * pow(3, cnt1);
    cnt1++;
    c /= 3;
    diff /= 3;
  }
  printf("%d", l);
}
