#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x) { return (x > 1e-8) - (x < -1e-8); }
int a, b;
int x[100002];
int y[100002];
void change1(int *aa, int n) {
  aa[0] = 0;
  while (n) {
    int p = n % 3;
    aa[++aa[0]] = p;
    n /= 3;
  }
}
int z[100002];
int main() {
  while (cin >> a >> b) {
    change1(x, a);
    change1(y, b);
    memset(z, 0, sizeof(z));
    int m = max(x[0], y[0]);
    for (int i = 1; i <= m; i++) {
      z[i] = y[i] - x[i];
      if (z[i] < 0) z[i] += 3;
    }
    long long sum = 0;
    int j = 1;
    for (int i = 1; i <= m; i++) {
      sum += z[i] * j;
      j *= 3;
    }
    cout << sum << endl;
  }
}
