#include <bits/stdc++.h>
using namespace std;
const int m = 3;
int trans(int a, int b) {
  int c, pa, pb, t = 1;
  c = 0;
  while (a || b) {
    pa = a % 3;
    a /= 3;
    pb = b % 3;
    b /= 3;
    c += (((pb - pa) % m + m) % m) * t;
    t *= 3;
  }
  return c;
}
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b) + 1) printf("%d\n", trans(a, b));
}
