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

int main() {
  ll N; cin>>N;
  ll ans=LLONG_MAX;
  ll b=0;
  ll pow2=1;
  while(pow2<=N) {
    ans=min(ans,N/pow2+N%pow2+b);
    pow2*=2;
    b++;
  }
  cout<<ans<<endl;
}

#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
	cin>>s;
	for(int i=s.size()-1;i>=0;i--){
		if(s[i]=='6')cout<<9;
		else if(s[i]=='9')cout<<6;
		else cout<<s[i];
	}
} 