#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, c;
  cin >> a >> c;
  vector<int> arr1;
  vector<int> arr2;
  int num;
  while (a > 0 || c > 0) {
    if (a > 0) {
      num = a % 3;
      arr1.push_back(num);
      a = a / 3;
    } else
      arr1.push_back(0);
    if (c > 0) {
      num = c % 3;
      arr2.push_back(num);
      c = c / 3;
    } else
      arr2.push_back(0);
  }
  vector<int> arr3;
  vector<int>::iterator i1;
  vector<int>::iterator i2 = arr2.begin();
  for (i1 = arr1.begin(); i1 != arr1.end(); i1++) {
    int n = 0;
    while ((*i1 + n) % 3 != *i2) n++;
    arr3.push_back(n);
    i2++;
  }
  int n = 0, k = 0;
  for (i1 = arr3.begin(); i1 != arr3.end(); i1++) {
    n = n + (*i1) * pow(3, k);
    k++;
  }
  cout << n;
  return 0;
}
