#include <bits/stdc++.h>
using namespace std;
string str_a, str_c, str_b;
long long powerOf_3[31 + 5];
string convert_to_base_3(long long n) {
  string s = "";
  while (1) {
    char ch = char(n % 3 + '0');
    s += ch;
    n = n / 3;
    if (!n) break;
  }
  return s;
}
long long find_b() {
  string s = "";
  for (int i = 0; i < 31; i++) {
    int cur = 0, digitOf_a = str_a[i] - '0', digitOf_c = str_c[i] - '0';
    while ((digitOf_a + cur) % 3 != digitOf_c) {
      cur++;
    }
    s += char(cur + '0');
  }
  long long val = 0;
  for (int i = 0; i < 31; i++) {
    val += (s[i] - '0') * powerOf_3[i];
  }
  return val;
}
int main() {
  int T;
  T = 1;
  string zeros = "";
  powerOf_3[0] = 1;
  for (int i = 1; i <= 31; i++) {
    powerOf_3[i] = powerOf_3[i - 1] * 3;
  }
  for (int i = 0; i < 31; i++) zeros += '0';
  for (int tc = 1; tc <= T; tc++) {
    long long a, b, c;
    cin >> a >> c;
    str_a = convert_to_base_3(a) + zeros;
    str_c = convert_to_base_3(c) + zeros;
    b = find_b();
    cout << b << '\n';
  }
}
