#include <bits/stdc++.h>
using namespace std;
int t;
vector<int> a, b, c;
int main() {
  scanf("%d", &t);
  while (t > 0) {
    a.push_back(t % 3);
    t /= 3;
  }
  scanf("%d", &t);
  while (t > 0) {
    c.push_back(t % 3);
    t /= 3;
  }
  int as = a.size();
  int cs = c.size();
  int m = as;
  if (cs > m) m = cs;
  for (int i = as; i < cs; i++) a.push_back(0);
  for (int i = cs; i < as; i++) c.push_back(0);
  for (int i = 0; i < as || i < cs; i++) a[i] = (c[i] - a[i] + 3) % 3;
  int count = 0;
  for (int i = m - 1; i >= 0; i--) count = count * 3 + a[i];
  printf("%d", count);
  return 0;
}
