#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, q;
  scanf("%d %d", &a, &c);
  b = 0;
  q = 1;
  while (a || c) {
    b += q * ((c % 3 - a % 3 + 3) % 3);
    a /= 3;
    c /= 3;
    q *= 3;
  }
  printf("%d\n", b);
  return 0;
}
