#include <bits/stdc++.h>
int solve(int a, int c) {
  if (a == 0 && c == 0) {
    return 0;
  }
  return solve(a / 3, c / 3) * 3 + (c % 3 - a % 3 + 3) % 3;
}
int main() {
  int a, c;
  while (scanf("%d%d", &a, &c) != EOF) {
    printf("%d\n", solve(a, c));
  }
}
