#include <bits/stdc++.h>
using namespace std;
void to_t(int a, int k, vector<int>& ret) {
  while (a > 0) {
    ret.push_back(a % k);
    a /= k;
  }
}
int to_d(int k, vector<int>& input) {
  int ret = 0;
  int mul = 1;
  for (size_t i = 0; i < input.size(); i++) {
    ret += input[i] * mul;
    mul *= k;
  }
  return ret;
}
int main() {
  int a, b;
  while (cin >> a >> b) {
    vector<int> at;
    vector<int> bt;
    to_t(a, 3, at);
    to_t(b, 3, bt);
    int n = max(at.size(), bt.size());
    at.resize(n);
    bt.resize(n);
    for (int i = 0; i < n; i++) {
      at[i] = (bt[i] + 3 - at[i]) % 3;
    }
    cout << to_d(3, at) << endl;
  }
  return 0;
}
