#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, c;
  cin >> a >> c;
  long long i = 0, sum = 0;
  while (a != 0 || c != 0) {
    int aa = a % 3, cc = c % 3;
    sum += pow(3, i) * ((cc + 3 - aa) % 3);
    a = a / 3;
    c = c / 3;
    i++;
  }
  cout << sum << endl;
}
