#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
int main() {
  long long a, c;
  cin >> a >> c;
  vector<long long> aa, cc;
  while (a) {
    aa.push_back(a % 3);
    a = a / 3;
  }
  while (c) {
    cc.push_back(c % 3);
    c /= 3;
  };
  ;
  long long sz = max(aa.size(), cc.size());
  aa.resize(sz, 0);
  cc.resize(sz, 0);
  vector<long long> bb(sz, 0);
  for (long long i = sz - 1; i >= 0; i--) {
    bb[i] = cc[i] - aa[i];
    if (bb[i] < 0) {
      bb[i] = bb[i] + 3;
    }
  }
  long long b = 0;
  for (long long i = sz - 1; i >= 0; i--) {
    b *= 3;
    b = b + bb[i];
  }
  cout << b << '\n';
}
