#include <bits/stdc++.h>
using namespace std;
int a[40], c[40], b[40], x, z, k, y;
void xtoa(int x, int *a, int k) {
  while (x) {
    a[++a[0]] = x % k;
    x /= k;
  }
}
void atox(int *a, int &x, int k) {
  int fac = 1;
  x = 0;
  for (int i = 1; i <= a[0]; i++) {
    x = x + a[i] * fac;
    fac *= k;
  }
}
void tor(int *a, int *b, int *c) {
  c[0] = max(a[0], b[0]);
  for (int i = 1; i <= c[0]; i++) c[i] = (a[i] + b[i]) % 3;
}
void writeln(int *a) {
  for (int i = 1; i <= a[0]; i++) printf("%d", a[i]);
  printf("\n");
}
int main() {
  scanf("%d %d", &x, &z);
  xtoa(x, a, 3);
  xtoa(z, c, 3);
  for (int i = max(a[0], c[0]); i >= 1; i--)
    for (k = 0; k < 3; k++)
      if ((k + a[i]) % 3 == c[i]) {
        b[i] = k;
        break;
      }
  b[0] = max(a[0], c[0]);
  while (b[b[0]] == 0 && b[0] > 1) b[0]--;
  atox(b, y, 3);
  printf("%d", y);
  return 0;
}
