#include <bits/stdc++.h>
using namespace std;
int const NMAX = 2e6;
int moves[1 + NMAX];
int n, from;
queue<int> q;
bool isValid(int to) {
return 1 <= to && to <= NMAX;
}
void visit(int to) {
if(isValid(to) && moves[to] == 0) {
moves[to] = moves[from]+1;
q.push(to);
}
}
int main() {
cin >> n;
q.push(1);
while(true) {
from = q.front();
if(from == n) {
cout << moves[from];
return 0;
}
q.pop();
visit(from-1);
visit(from+1);
visit(3*from);
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGNvbnN0IE5NQVggPSAyZTY7CmludCBtb3Zlc1sxICsgTk1BWF07CmludCBuLCBmcm9tOwpxdWV1ZTxpbnQ+IHE7Cgpib29sIGlzVmFsaWQoaW50IHRvKSB7CiAgICByZXR1cm4gMSA8PSB0byAmJiB0byA8PSBOTUFYOwp9Cgp2b2lkIHZpc2l0KGludCB0bykgewogICAgaWYoaXNWYWxpZCh0bykgJiYgbW92ZXNbdG9dID09IDApIHsKICAgICAgICBtb3Zlc1t0b10gPSBtb3Zlc1tmcm9tXSsxOwogICAgICAgIHEucHVzaCh0byk7CiAgICB9Cn0KCmludCBtYWluKCkgewogICAgY2luID4+IG47CiAgICBxLnB1c2goMSk7CiAgICB3aGlsZSh0cnVlKSB7CiAgICAgICAgZnJvbSA9IHEuZnJvbnQoKTsKICAgICAgICBpZihmcm9tID09IG4pIHsKICAgICAgICAJY291dCA8PCBtb3Zlc1tmcm9tXTsKICAgICAgICAJcmV0dXJuIDA7CiAgICAgICAgfQogICAgICAgIHEucG9wKCk7CiAgICAgICAgdmlzaXQoZnJvbS0xKTsKICAgICAgICB2aXNpdChmcm9tKzEpOwogICAgICAgIHZpc2l0KDMqZnJvbSk7CiAgICB9Cn0=