#include <bits/stdc++.h>
using namespace std;
long long N, a[100], K;
int main() {
  cin >> N;
  int i;
  while (N) {
    a[++K] = N % 3;
    N /= 3;
  }
  for (i = 1; i <= K; i++)
    if (a[i] != 0) break;
  long long ind = i, cnt = 1, res = 0;
  for (i = ind + 1; i <= K; i++) {
    res += cnt * a[i];
    cnt *= 3;
  }
  cout << res + 1 << "\n";
  return 0;
}
