#include <bits/stdc++.h>
int A[100], B[100];
int f(int x) {
  int i, j, k, l;
  i = 0;
  while (x >= 3) {
    A[++i] = x % 3;
    x /= 3;
  }
  A[++i] = x;
  return i;
}
int F(int x, int y) {
  int i, j, k, l;
  i = 0;
  while (x >= 3) {
    B[++i] = x % 3;
    x /= 3;
  }
  B[++i] = x;
  if (i < y) {
    for (j = i + 1; j <= y; j++) {
      B[j] = 0;
    }
  } else {
    for (j = y + 1; j <= i; j++) A[j] = 0;
  }
  return j - 1;
}
int main() {
  int a, b, i, j, k, l, tp1, tp2;
  while (scanf("%d %d", &a, &b) != EOF) {
    k = f(a);
    k = F(b, k);
    for (i = 1; i <= k; i++) {
      tp1 = B[i] - A[i];
      tp2 = B[i] + 3 - A[i];
      if (tp1 >= 0 && tp2 >= 0) {
        A[i] = tp1 < tp2 ? tp1 : tp2;
      } else {
        if (tp1 < 0)
          A[i] = tp2;
        else if (tp2 < 0)
          A[i] = tp1;
      }
    }
    l = 0;
    tp1 = 1;
    for (i = 1; i <= k; i++) {
      l += A[i] * tp1;
      tp1 *= 3;
    }
    printf("%d\n", l);
  }
  return 0;
}
