#include <bits/stdc++.h>
using namespace std;
long long arr[1001] = {}, arr2[1001] = {};
void omar() {
  ios::sync_with_stdio(0);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline long long power(int k, int rep) {
  if (rep == 0)
    return (1 * k);
  else if (k == 0)
    return 0;
  long long fact = 1;
  for (int i = 1; i <= rep; ++i) fact *= 3;
  return fact * k;
}
int main() {
  long long n, m, g = 1000, z = 1000, r, re;
  omar();
  cin >> n >> m;
  while (n != 0) {
    r = n % 3;
    arr[g] = r;
    n /= 3;
    g--;
  }
  while (m != 0) {
    re = m % 3;
    arr2[z] = re;
    m /= 3;
    z--;
  }
  long long t = 0, sum = 0;
  for (int i = 1000; i >= 0; --i) {
    for (int c = 0; c < 3; ++c)
      if ((arr[i] + c) % 3 == arr2[i]) {
        sum += power(c, t);
        break;
      }
    t++;
  }
  cout << sum << "\n";
  return 0;
}
