#include <bits/stdc++.h>
using namespace std;
int A, C;
int main() {
  cin >> A >> C;
  int vA, vC;
  int res = 0;
  int cnt = 0;
  while (A != 0 || C != 0) {
    vA = A % 3;
    vC = C % 3;
    A /= 3;
    C /= 3;
    res *= 3;
    res += (3 + vC - vA) % 3;
    cnt++;
  }
  int rr = 0;
  while (cnt) {
    rr *= 3;
    rr += res % 3;
    res /= 3;
    --cnt;
  }
  cout << rr << "\n";
  return 0;
}
