#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, c, b = 0;
  cin >> a >> c;
  int ans[100];
  int k = 0;
  while (a || c) {
    ans[k] = ((c % 3) - (a % 3) + 3) % 3;
    a /= 3;
    c /= 3;
    k++;
  }
  for (int i = k - 1; i >= 0; i--) {
    b *= 3;
    b += ans[i];
  }
  cout << b;
  return 0;
}
