#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int bit[1000];
  while (scanf("%d%d", &n, &m) == 2) {
    int i = 0, j = 0;
    memset(bit, 0, sizeof(bit));
    while (m != 0) {
      bit[j] = m % 3;
      m /= 3;
      j++;
    }
    while (n != 0) {
      bit[i] -= n % 3;
      if (bit[i] < 0) bit[i] += 3;
      n /= 3;
      i++;
    }
    i = max(i, j);
    int ans = 0;
    for (int k = i - 1; k >= 0; k--) {
      ans *= 3;
      ans += bit[k];
    }
    printf("%d\n", ans);
  }
}
