#include <bits/stdc++.h>
using namespace std;
#define show(a) for(auto e:a)cout<<e<<" ";cout<<endl
#define size(a) static_cast<int>((a).size())

int main(){
  long long n, maxb, powb, ans = LLONG_MAX, a, c;
  cin >> n;
  maxb = log(n) / log(2);
  if(n == 1){
    cout << 1 << endl;
    return 0;
  }
  for(int b = 0; b < maxb; ++b){
    powb = static_cast<long long>(pow(2.0, b));
    a = n / powb;
    c = n - a * powb;
    ans = min(ans, a + b + c);
  }
  cout << ans << endl;
  return 0;
}
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll,ll> P;
typedef pair<double,ll> Pd;
typedef pair<double,double> PD;
typedef priority_queue<P,vector<P>,greater<P> > P_queue;
typedef priority_queue<Pd,vector<Pd>,greater<Pd> > Pd_queue;

const ll MOD=998244353;
const ll mod=1000000007;
const ll INF=1e15;
const double DEL=1e-7;

#define _GLIBCXX_DEBUG
#define REP(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(),a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

void Add(ll &a, ll b){
    a=(a+b)%mod;
    return;
}

void Mul(ll &a, ll b){
    a=(a*b)%mod;
    return;
}



bool nana(ll x, ll n){
    if(!x) return false;
    if(x%n==7) return true;
    return nana(x/n,n);
}

int main(){
    ll N; cin>>N;
    ll ans=0;
    REP(i,1,N+1) if(!nana(i,10) && !nana(i,8)) ans++;
    cout<<ans<<endl;
}