#include <iostream>
#include <algorithm>
using namespace std;
int dp[1000001] = { 0, };
int main(void)
{
int n;
cin >> n;
for (int i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + 1;
if (i % 2 == 0) dp[i] = min(dp[i], dp[i / 2] + 1);
if (i % 3 == 0) dp[i] = min(dp[i], dp[i / 3] + 1);
}
cout << dp[n];
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGRwWzEwMDAwMDFdID0geyAwLCB9OwppbnQgbWFpbih2b2lkKQp7CglpbnQgbjsKCWNpbiA+PiBuOwoKCWZvciAoaW50IGkgPSAyOyBpIDw9IG47IGkrKykKCXsKCQlkcFtpXSA9IGRwW2kgLSAxXSArIDE7CgkJaWYgKGkgJSAyID09IDApIGRwW2ldID0gbWluKGRwW2ldLCBkcFtpIC8gMl0gKyAxKTsKCQlpZiAoaSAlIDMgPT0gMCkgZHBbaV0gPSBtaW4oZHBbaV0sIGRwW2kgLyAzXSArIDEpOwoJfQoJY291dCA8PCBkcFtuXTsKfQ==