#include <bits/stdc++.h>
using namespace std;
int a, b, c;
int main() {
  cin >> a >> c;
  for (int p = 1; p < 1e9; p *= 3) {
    b += (((c / p) % 3 - (a / p) % 3 + 3) % 3) * p;
  }
  cout << b << endl;
}
