#include <bits/stdc++.h>
using namespace std;
const int maxL = 33;
int a[maxL], b[maxL], c[maxL], aa, bb;
void trans(int n, int *bit) {
  for (int i = 0; n > 0; i++) {
    bit[i] = n % 3;
    n /= 3;
  }
}
int main() {
  cin >> aa >> bb;
  trans(aa, a);
  trans(bb, b);
  for (int i = 0; i < maxL; i++) {
    c[i] = b[i] - a[i];
    if (c[i] < 0) c[i] += 3;
  }
  int res = 0;
  for (int i = maxL - 1; i >= 0; i--) {
    res *= 3;
    res += c[i];
  }
  cout << res << endl;
}
