#include <bits/stdc++.h>
using namespace std;
int a, c, numA, numC, B;
int Ctri[100], Atri[100], b[100];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  scanf("%d %d", &a, &c);
  while (c >= 0) {
    numC++;
    Ctri[numC] = c % 3;
    if (c == 0) break;
    c /= 3;
  }
  while (a >= 0) {
    numA++;
    Atri[numA] = a % 3;
    if (a == 0) break;
    a /= 3;
  }
  if (numA < numC)
    for (int i = numA + 1; i <= numC; i++) Atri[i] = 0;
  else if (numC < numA)
    for (int i = numC + 1; i <= numA; i++) Ctri[i] = 0;
  for (int i = 1; i <= max(numA, numC); i++) {
    b[i] = Ctri[i] - Atri[i];
    if (b[i] < 0) b[i] += 3;
  }
  int tmp = 1;
  for (int i = 1; i <= max(numA, numC); i++) {
    B += b[i] * tmp;
    tmp *= 3;
  }
  printf("%d\n", B);
  return 0;
}
