#include <bits/stdc++.h>
using namespace std;
int main(void) {
  unsigned long long in1, in2, in1m, in2m, out;
  int in1t[100], in2t[100];
  int i;
  memset(in1t, 0, sizeof(in1t));
  memset(in2t, 0, sizeof(in2t));
  cin >> in1 >> in2;
  for (i = 0; in1 != 0; i++) {
    in1t[i] = in1 % 3;
    in1 /= 3;
  }
  in1m = i;
  for (i = 0; in2 != 0; i++) {
    in2t[i] = in2 % 3;
    in2 /= 3;
  }
  in2m = i;
  out = 0;
  for (i = 99; i >= 0; i--) {
    out *= 3;
    if (in2t[i] - in1t[i] < 0) {
      in2t[i] += 3;
    }
    out += (in2t[i] - in1t[i]) % 3;
  }
  cout << out;
  return 0;
}
