#include <bits/stdc++.h>
using namespace std;
int a[30];
int b[30];
int c[30];
int main() {
  int x, y;
  cin >> x;
  cin >> y;
  for (int i = 0; i < 30; i++) {
    a[i] = x % 3;
    b[i] = y % 3;
    x /= 3;
    y /= 3;
    c[i] = b[i] - a[i];
    if (c[i] < 0) c[i] += 3;
    c[i] %= 3;
  }
  long long int ans = 0;
  for (int i = 29; i >= 0; i--) {
    ans *= 3;
    ans += c[i];
  }
  cout << ans << endl;
  return 0;
}
