#include <bits/stdc++.h>
using namespace std;
long long a, c;
int arr[100], brr[100], crr[100];
int n1, n2;
int i, j;
int main() {
  cin >> a >> c;
  while (a != 0) {
    n1++;
    arr[n1] = a % 3;
    a /= 3;
  }
  while (c != 0) {
    n2++;
    crr[n2] = c % 3;
    c /= 3;
  }
  n2 = max(n2, n1);
  for (i = n2; i >= 1; i--) {
    if (crr[i] >= arr[i])
      brr[i] = crr[i] - arr[i];
    else
      brr[i] = (crr[i] + 3 - arr[i]);
  }
  long long temp = 1, sum = 0;
  for (a = 1; a <= n2; a++) {
    sum += brr[a] * temp;
    temp *= 3;
  }
  cout << sum << endl;
}
