#include <bits/stdc++.h>
using namespace std;
int A[10000], C[10000], B[10000];
int na, nc;
void findA(int i, int n) {
  int num;
  num = 0;
  while (n) {
    num++;
    A[num] = n % i;
    n /= i;
  }
  na = num;
}
void findC(int i, int n) {
  int num;
  num = 0;
  while (n) {
    num++;
    C[num] = n % i;
    n /= i;
  }
  nc = num;
}
long long powmod(int a, int n) {
  long long BB = 1, AA = a;
  while (n) {
    if (n & 1) BB = BB * AA;
    AA = AA * AA;
    n /= 2;
  }
  return BB;
}
long long findB(int q) {
  int i, j, k;
  k = 0;
  for (i = 1, j = 1;; i++, j++) {
    k++;
    if (i > nc && j > na) break;
    if (C[i] >= A[j])
      B[k] = C[i] - A[j];
    else
      B[k] = C[i] + q - A[j];
  }
  for (i = k; i >= 1; i--) {
    if (B[i] > 0) break;
    if (B[i] <= 0) k--;
  }
  long long sum;
  sum = 0;
  for (i = k, j = 1; i >= 1; i--, j++) sum += (B[i] * powmod(q, k - j));
  return sum;
}
int main(int argc, char** argv) {
  int a, b, c, i;
  while (scanf("%d%d", &a, &c) != EOF) {
    if (a == c) {
      printf("0\n");
      continue;
    }
    memset(A, 0, sizeof(A));
    memset(C, 0, sizeof(C));
    long long ss;
    findA(3, a);
    findC(3, c);
    ss = findB(3);
    cout << ss << endl;
  }
  return 0;
}
