#include <bits/stdc++.h>
int main(void) {
  int number1, number2;
  scanf("%d %d", &number1, &number2);
  int array1[1000], array2[1000];
  int pow1 = 0, pow2 = 0;
  while (number1 != 0) {
    array1[pow1] = number1 % 3;
    number1 /= 3;
    pow1++;
  }
  while (number2 != 0) {
    array2[pow2] = number2 % 3;
    number2 /= 3;
    pow2++;
  }
  int pow, i, array[1000];
  if (pow1 > pow2) {
    pow = pow1;
    for (i = pow2; i <= pow1; i++) array2[i] = 0;
  } else if (pow2 > pow1) {
    pow = pow2;
    for (i = pow1; i <= pow2; i++) array1[i] = 0;
  } else
    pow = pow1;
  for (i = 0; i < pow; i++) {
    array[i] = array2[i] - array1[i];
    if (array[i] < 0) array[i] += 3;
  }
  int n = 1, sum = 0;
  for (i = 0; i < pow; i++) {
    sum += n * array[i];
    n *= 3;
  }
  printf("%d", sum);
  return 0;
}
