#include <bits/stdc++.h>
using namespace std;
int sub_tern(int c, int a) {
  int temp_c;
  int temp_a;
  int diff;
  long sum = 0;
  int three_power = 1;
  while (c > 0 || a > 0) {
    temp_c = c % 3;
    c = c / 3;
    temp_a = a % 3;
    a = a / 3;
    diff = temp_c - temp_a;
    if (diff < 0) {
      diff += 3;
    }
    sum += (long)diff * (long)three_power;
    three_power *= 3;
  }
  return sum;
}
int main() {
  int a;
  int c;
  cin >> a >> c;
  if (a == c) {
    cout << "0\n";
    return 0;
  }
  cout << sub_tern(c, a) << "\n";
  return 0;
}
