#include <bits/stdc++.h>
using namespace std;
int a, b, c;
long long ans;
vector<int> wa, wb, wc;
void get(int num, vector<int>& w) {
  while (num > 0) {
    w.push_back(num % 3);
    num /= 3;
  }
}
int main() {
  scanf("%d %d", &a, &c);
  get(a, wa);
  get(c, wc);
  while (wa.size() < wc.size()) wa.push_back(0);
  while (wc.size() < wa.size()) wc.push_back(0);
  reverse(wa.begin(), wa.end());
  reverse(wc.begin(), wc.end());
  for (int i = 0; i < wa.size(); ++i) {
    if (wc[i] - wa[i] >= 0)
      wb.push_back(wc[i] - wa[i]);
    else
      wb.push_back(wc[i] + 3 - wa[i]);
  }
  long long k = 1;
  for (int i = wb.size() - 1; i >= 0; --i) {
    ans += wb[i] * k;
    k *= 3;
  }
  printf("%d", ans);
  return 0;
}
