#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, c;
  scanf("%d %d", &a, &c);
  vector<int> A;
  vector<int> C;
  while (a) {
    A.push_back(a % 3);
    a /= 3;
  }
  while (c) {
    C.push_back(c % 3);
    c /= 3;
  }
  while (A.size() < C.size()) A.push_back(0);
  while (C.size() < A.size()) C.push_back(0);
  int ans = 0;
  int k = 1;
  for (int i = 0; i < A.size(); i++) {
    int d = C[i] - A[i];
    if (d < 0) d += 3;
    ans += (d * k);
    k *= 3;
  }
  printf("%d\n", ans);
  return 0;
}
