#include <bits/stdc++.h>
using namespace std;
long long count1, a, b;
void decToTernary(vector<int> &vt, int n) {
  int i = 0;
  while (n > 0) {
    vt.push_back(n % 3);
    n = n / 3;
    i++;
  }
}
int main() {
  int a, c;
  cin >> a >> c;
  vector<int> ternaryNum;
  vector<int> vt;
  decToTernary(ternaryNum, a);
  decToTernary(vt, c);
  int l1 = ternaryNum.size();
  int l2 = vt.size();
  if (l1 < l2) {
    for (int i = 1; i <= l2 - l1; i++) {
      ternaryNum.push_back(0);
    }
  } else if (l2 < l1) {
    for (int i = 1; i <= l1 - l2; i++) vt.push_back(0);
  }
  int l = vt.size();
  int total = 0;
  for (int i = l - 1; i >= 0; i--) {
    for (int j = 0; j < 3; j++) {
      if ((ternaryNum[i] + j) % 3 == vt[i]) {
        total += j * pow(3, i);
        break;
      }
    }
  }
  cout << total << endl;
}
