#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 7;
const long long INF = 1e18 + 8;
int n, a, c;
int stk[MAXN], cnt;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> a >> c;
  int da, dc;
  while (a || c) {
    da = a % 3;
    dc = c % 3;
    stk[cnt++] = (dc + 3 - da) % 3;
    a /= 3;
    c /= 3;
  }
  int ans = 0, r = 1;
  for (int i = 0; i < cnt; i++) {
    ans += r * stk[i];
    r *= 3;
  }
  cout << ans << endl;
  return 0;
}
