#include <bits/stdc++.h>
using namespace std;
int main() {
  int num1[20] = {0};
  int num2[20] = {0};
  int values[20];
  int ans[20] = {0};
  values[0] = 1;
  for (int i = 1; i < 20; i++) {
    values[i] = 3 * values[i - 1];
  }
  int x, y;
  cin >> x >> y;
  int temp;
  for (int i = 19; i >= 0; i--) {
    temp = x / values[i];
    if (temp > 2) temp = 2;
    x -= temp * values[i];
    num1[i] = temp;
    temp = y / values[i];
    if (temp > 2) temp = 2;
    y -= temp * values[i];
    num2[i] = temp;
  }
  for (int i = 0; i < 20; i++) {
    temp = num2[i] - num1[i];
    if (temp < 0) temp += 3;
    ans[i] = temp % 3;
  }
  int answer = 0;
  for (int i = 0; i < 20; i++) {
    answer += ans[i] * values[i];
  }
  cout << answer << endl;
  return 0;
}
