#include <bits/stdc++.h>
using namespace std;
int main() {
  char ga[110], gc[110];
  char gb[110];
  for (int i = 0; i < 110; i++) {
    ga[i] = '0';
    gc[i] = '0';
  }
  int a, c;
  scanf("%d%d", &a, &c);
  int cnt = 0, cnt1 = 0;
  while (a > 0) {
    ga[cnt++] = a % 3 + '0';
    a = a / 3;
  }
  while (c > 0) {
    gc[cnt1++] = c % 3 + '0';
    c = c / 3;
  }
  int mxcnt = max(cnt1, cnt);
  for (int i = 0; i < mxcnt; i++) {
    for (int j = 0; j < 3; j++)
      if ((ga[i] - '0' + j) % 3 == gc[i] - '0') {
        gb[i] = j + '0';
      }
  }
  int ans = 0;
  for (int i = mxcnt - 1; i >= 0; i--) {
    ans = ans * 3 + gb[i] - '0';
  }
  printf("%d", ans);
  return 0;
}
