#include <bits/stdc++.h>
using namespace std;
const int N = 0;
vector<int> convert(int x) {
  vector<int> res;
  do {
    res.push_back(x % 3);
    x /= 3;
  } while (x > 0);
  return res;
}
int main() {
  int a, b, c;
  scanf("%d%d", &a, &c);
  vector<int> va, vb, vc;
  va = convert(a);
  vc = convert(c);
  while (int(va.size()) != int(vc.size())) {
    if (int(va.size()) > int(vc.size()))
      vc.push_back(0);
    else
      va.push_back(0);
  }
  for (int i = 0; i < int(va.size()); ++i) {
    vb.push_back(0);
    for (vb[i] = 0; vb[i] < 3; ++vb[i])
      if ((vb[i] + va[i]) % 3 == vc[i]) break;
  }
  b = 0;
  for (int i = int(vb.size()) - 1; i >= 0; i--) b = b * 3 + vb[i];
  printf("%d\n", b);
  return 0;
}
