#include <bits/stdc++.h>
using namespace std;

#define int long long
const int MAX = 1e15;

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);

    long long n;
    cin >> n; n++;

    long long speed = 1;
    long long sum = 0;
    long long tm = 0;
    while (1) {
		if (n - (sum*2 + speed) < 0) {
			break;
		} else {
			sum += speed;
			speed *= 3;
			tm++;
		}
    }
    speed /= 3;
	sum -= speed;

	n -= sum*2 + speed;

	//cout << tm << ' ' << n << '\n';
	int ite = tm - 1;
	tm = 2*tm - 2;
// 9

    while (ite--) {
        tm += n % 3;
        n /= 3;
    }
    cout << tm + n << '\n';

    return 0;
}

