#include <bits/stdc++.h>
using namespace std;
long long pangkat(int p, int q) {
  long long r = 1;
  if (p == 0)
    return 0;
  else {
    for (int j = 0; j < q; j++) r *= 3;
    return r * p;
  }
}
string tiga(int p) {
  int q;
  string sp;
  do {
    q = p % 3;
    switch (q) {
      case 0:
        sp = '0' + sp;
        break;
      case 1:
        sp = '1' + sp;
        break;
      case 2:
        sp = '2' + sp;
        break;
    }
    p = p / 3;
  } while (p > 0);
  return sp;
}
int a, c;
int main() {
  scanf("%lld %lld", &a, &c);
  string sa = tiga(a);
  string sb;
  string sc = tiga(c);
  int x = sa.size();
  int y = sc.size();
  while (x < y) {
    sa = '0' + sa;
    x++;
  }
  while (x > y) {
    sc = '0' + sc;
    y++;
  }
  string sbi;
  int saii, scii, sbii;
  for (int i = 0; i < x; i++) {
    switch (sa[i]) {
      case '0':
        saii = 0;
        break;
      case '1':
        saii = 1;
        break;
      case '2':
        saii = 2;
        break;
    }
    switch (sc[i]) {
      case '0':
        scii = 0;
        break;
      case '1':
        scii = 1;
        break;
      case '2':
        scii = 2;
        break;
    }
    sbii = scii - saii;
    if (sbii < 0) sbii += 3;
    switch (sbii) {
      case 0:
        sb = sb + '0';
        break;
      case 1:
        sb = sb + '1';
        break;
      case 2:
        sb = sb + '2';
        break;
    }
  }
  long long hasil = 0;
  for (int i = 0; i < x; i++) {
    if (sb[i] == '0')
      hasil += pangkat(0, x - i - 1);
    else if (sb[i] == '1')
      hasil += pangkat(1, x - i - 1);
    else if (sb[i] == '2')
      hasil += pangkat(2, x - i - 1);
  }
  printf("%lld\n", hasil);
}
