#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * acos(0.0), EPS = 1e-9;
const int INF = 1e9, NINF = -1e9, MOD = 1e9 + 7;
int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void GO() { ios::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL); }
int main() {
  GO();
  int n, m, temp = 0, ans = 0;
  string second, s1, s2;
  cin >> n >> m;
  while (n) second = to_string(n % 3) + second, n /= 3;
  while (m) s1 = to_string(m % 3) + s1, m /= 3;
  while (((int)(second).size()) < ((int)(s1).size())) second = "0" + second;
  while (((int)(second).size()) > ((int)(s1).size())) s1 = "0" + s1;
  for (int i = second.size() - 1, j = s1.size() - 1; i >= 0 || j >= 0;
       i--, j--) {
    if (i < 0)
      temp = (s1[j] - '0');
    else if (j < 0)
      temp = (second[i] - '0');
    else
      temp = (s1[j] - '0') - (second[i] - '0');
    if (temp < 0) temp += 3;
    s2 = to_string(temp) + s2;
  }
  for (int i = s2.size() - 1, j = 0; i >= 0; i--, j++)
    ans += (s2[i] - '0') * (pow(3, j));
  cout << ans;
  return 0;
}
