#include <bits/stdc++.h>
using namespace std;
int ans[1000000];
int main() {
  int a, b;
  cin >> a >> b;
  int now = 3;
  int nn = 1;
  int pos = 0;
  int aa, bb;
  int t = max(a, b);
  while (nn <= t) {
    if (a >= 0)
      aa = a % now;
    else
      aa = 0;
    if (b >= 0)
      bb = b % now;
    else
      bb = 0;
    a -= aa;
    b -= bb;
    aa /= nn;
    bb /= nn;
    ans[pos] = (bb - aa + 3) % 3;
    pos++;
    now *= 3;
    nn *= 3;
  }
  long long out = 0;
  now = 1;
  for (int i = 0; i < pos; i++) {
    out += (long long)ans[i] * now;
    now *= 3;
  }
  cout << out << endl;
  return 0;
}
