#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 50;

ll p3[N];
ll n;
ll solve(int k) {
    ll rem = n - 2 * p3[k] + 2;

    ll ans = 2 * k;
    if (rem < 0) return n;

    ans +=  rem / p3[k];
    rem %= p3[k];
    while (rem) {
        ans += rem % 3;
        rem /= 3;
    }
    return ans;
}
int main() {
    p3[0] = 1;
    for(int i = 1; i < N; i++) p3[i] = p3[i - 1] * 3;
    cin >> n;
    
    ll ans = n;
    for(int i = 1; p3[i] <= n; i++) 
        ans = min(ans, solve(i));
    cout << ans << endl;
}

