#include <bits/stdc++.h>

using namespace std;

int main(){
  long long n;
  cin >> n;
  long long a = n;
  long long b = 0;
  long long c = 0;
  while(a!=0){
    a = a/1000;

    if(a == 0) break;
		
    b ++;
  }	    

  if(b == 1){
    c = n-1000;       
  }
  if(b == 2){
    c = n-1000000;      
  }
  if(b == 3){
    c = n-1000000000;  
  }
  if(b == 4){
    c = n-1000000000000;      
  }
	
  if(b == 0){
    cout << 0 << endl;  
  }
  if(b == 1){
    cout << c+1 << endl;		
  }
  if(b == 2){
    cout << 999999-1000+1 + 2*(c+1) << endl;		
  }
  if(b == 3){
    cout << 2*(999999999-1000000+1)+(999999-1000+1) + 3*(c+1) << endl;		
  }
  if(b == 4){
    cout << 3*(999999999999-1000000000+1)+ 2*(999999999-1000000+1)+(999999-1000+1) + 4*(c+1) << endl;		
  }
  if(b == 5){
    cout << 4*(999999999999999-1000000000000+1)+ 3*(999999999999-1000000000+1)+ 2*(999999999-1000000+1)+(999999-1000+1) + 5 << endl;			
  }
	
  return 0;
}

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

void __dbg() { cout << endl; }
template<typename Arg, typename... Args> void __dbg(Arg A, Args... B) { cout << " " << A; __dbg(B...); }
#define dbg(...)  cout << "(" << #__VA_ARGS__ << "):", __dbg(__VA_ARGS__)

#define rep(i,a,b) for(int i=a;i<b;i++)
#define lprin(n) printf("%lld\n", n)
#define lpri(n) printf("%lld ", n)
#define lscn(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define pri(n) printf("%d ", n)
#define scn(n) scanf("%d", &n)
#define vi vector<int>
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second

const int inf  = INT_MAX;
const int ninf = INT_MIN;
const int mod  = 1e9 + 7;
const int maxN = 1e6 + 2;

map<ll, ll> dp, vis; 
ll x, y, ans;

ll rec(ll val)
{
   if(vis[val]) return dp[val];

   vis[val] = 1;
   dp[val] = 1e18;

   ll& ret = dp[val];
   ret = abs(x - val);

   if(val == 1)
      return ret;

   if(val % 2 == 0) 
      ret = min(ret, 1 + rec(val / 2));
   else
      ret = min(ret, 1 + min(rec(val + 1), rec(val - 1)));

   return ret;
}

int main()
{  
   lscn(x); lscn(y);

   lprin(rec(y));
   
   return 0;
}