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