#include <bits/stdc++.h>
using namespace std;
unsigned long long int build_num(unsigned long long int a,
                                 unsigned long long int c) {
  unsigned long long int b = 0, t1, t2;
  unsigned long long int tenstep = 1;
  int poryadoc = 0;
  while (a > 0 || c > 0) {
    t1 = a % 10;
    a /= 10;
    t2 = c % 10;
    c /= 10;
    if (t2 > t1) b += (t2 - t1) * tenstep;
    if (t2 < t1) b += (3 - (t1 - t2)) * tenstep;
    tenstep *= 10;
  }
  return b;
}
int main() {
  int aa, cc;
  cin >> aa >> cc;
  unsigned long long int a = 0, c = 0, b = 0;
  int numbers[20];
  int i = 0;
  while (aa != 0) {
    int mod;
    mod = aa % 3;
    aa /= 3;
    numbers[i] = mod;
    i++;
  }
  i--;
  while (i >= 0) {
    a *= 10;
    a += numbers[i];
    i--;
  }
  i = 0;
  while (cc != 0) {
    int mod;
    mod = cc % 3;
    cc /= 3;
    numbers[i] = mod;
    i++;
  }
  i--;
  while (i >= 0) {
    c *= 10;
    c += numbers[i];
    i--;
  }
  unsigned long long int num = build_num(a, c);
  int trstep = 1;
  for (int i = 0; num != 0; i++) {
    b += (num % 10) * trstep;
    num /= 10;
    trstep *= 3;
  }
  cout << b;
  return 0;
}
