#include <bits/stdc++.h>
using namespace std;
using namespace std;
void chekarre(int* arr, int n) {
  cout << "[";
  for (int i = 0; i < n; i++) cout << arr[i] << " ";
  cout << "]" << endl;
}
int n, m;
int solve() {
  vector<int> a, b;
  vector<int>::iterator it;
  int x;
  while (n > 0) {
    x = n % 3;
    n = (n - x) / 3;
    it = a.begin();
    a.insert(it, x);
    if (n == 1 || n == 2 || n == 0) {
      a.insert(a.begin(), n);
      break;
    }
  }
  while (m > 0) {
    x = m % 3;
    m = (m - x) / 3;
    b.insert(b.begin(), x);
    if (m == 1 || m == 2 || m == 0) {
      b.insert(b.begin(), m);
      break;
    }
  }
  while (a.size() != b.size()) {
    if (a.size() < b.size())
      a.insert(a.begin(), 0);
    else
      b.insert(b.begin(), 0);
  }
  int c[a.size()];
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == 0) {
      if (b[i] == 0)
        c[i] = 0;
      else if (b[i] == 1)
        c[i] = 1;
      else
        c[i] = 2;
    } else if (a[i] == 1) {
      if (b[i] == 0)
        c[i] = 2;
      else if (b[i] == 1)
        c[i] = 0;
      else
        c[i] = 1;
    } else {
      if (b[i] == 0)
        c[i] = 1;
      else if (b[i] == 1)
        c[i] = 2;
      else
        c[i] = 0;
    }
  }
  int u;
  long long z = 0, temp = 1;
  for (int i = a.size() - 1; i >= 0; i--) {
    if (i < a.size() - 1)
      temp *= 3;
    else
      temp = 1;
    z += (c[i] * temp);
  }
  cout << z;
  return 1;
}
bool input() {
  scanf("%d", &n);
  scanf("%d", &m);
  return true;
}
int main() {
  int T = 1;
  for (int testnum = 1; testnum <= T; testnum++) {
    if (!input()) break;
    solve();
  }
  return 0;
}
