#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, c, ad[20], an = 0, cd[20], cn = 0;
  cin >> a >> c;
  memset(ad, 0, sizeof ad);
  memset(cd, 0, sizeof cd);
  while (a != 0) ad[an] = a % 3, a /= 3, an++;
  while (c != 0) cd[cn] = c % 3, c /= 3, cn++;
  int ans = 0, i = max(an, cn) - 1;
  for (; i >= 0; i--) ans = ans * 3 + (cd[i] + 3 - ad[i]) % 3;
  cout << ans;
  return 0;
}
