#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; 
 
#define int long long int
#define double long double
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define pqi priority_queue<int>
#define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++)
#define inp(a,n,f) int a[n+f];for(int hh=f;hh<n+f;hh++)cin>>a[hh];
#define printdecimal(k) cout<<fixed<<setprecision(k);
#define mem(a,k) memset(a,k,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define mod (int)(1e9+7)
#define inf LLONG_MAX
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
#define N (int)(2e6+6)

struct segtree
{
	int size;
	vector<int> values;

	void ini(int n)
	{
		size=1;
		while(size<n)
			size*=2;
		values.assign(2*size,1);
	}

	void set(int i,int v,int cur,int lx,int rx)
	{
		if(rx-lx==1)
		{
			values[cur]=v;
			return;
		}
		int m=(lx+rx)/2;
		if(i<m) set(i,v,2*cur+1,lx,m);
		else set(i,v,2*cur+2,m,rx);
		values[cur]=max(values[2*cur+1],values[2*cur+2]);
	}

	void set(int i,int v)
	{
		set(i,v,0,0,size);
	}

	int cal(int v,int cur,int lx,int rx)
	{
		if(values[cur]<v) return -1;
		if(rx-lx==1) return lx;
		int m=(lx+rx)/2;
		int val=cal(v,2*cur+1,lx,m);
		if(val==-1) return cal(v,2*cur+2,m,rx);
		return val;
	}

	int cal(int v)
	{
		return cal(v,0,0,size);
	}
};


signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	for(int i=0;i<100;i++){}
	int n,m;
	cin>>n>>m;
	segtree st;
	st.ini(N);
	inp(a,n,0);
	int h[n+1]={0};
	for(int i=0;i<m-1;i++)
		st.set(a[i],0),h[a[i]]++;
	int ans=inf;
	for(int j=m-1,i=0;j<n;j++,i++)
	{
		if(h[a[j]]==0) st.set(a[j],0);
		h[a[j]]++;
		int idx=st.cal(1);
		ans=min(ans,idx);
		h[a[i]]--;
		if(h[a[i]]==0) st.set(a[i],1);
	}
  for(int i=0;i<100;i++){}
	cout<<ans;
	return 0;
}
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip> //setprecision
#include <map> // map
#include <unordered_map> //unordered_map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
#include <sstream>
#define endl "\n";
using namespace std;

using PLL = pair<long long,long long>;
typedef tuple<long long,long long,long long> TUP;
using P = pair<int,int>;
const long long INF = 4000000000000000001;
const int inf = 1001001001;
const long long MOD = 998244353;

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M, long long mod) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % mod, M / 2,mod);
    //When end-of-a bit is 1, times simple N.
    if (M & 1) res = (res * N) % mod;
    return res %= mod;
}
long long gcd(long long a, long long b) {
    if (b == 0) return a; else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}
long long get_mod(long long res){
    if(res < 0) res += MOD;
    return res % MOD;
}



int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    
    int T;T = 1;
    vector<long long> ans;
    for(int t = 0;t<T;t++){
        long long X,Y,A,B,C,D;cin >> X >> Y;
        if(X == Y){
            ans.push_back(0);
            break;
        }
        A = 1,B = 1,C = 1,D = 1;
        map<long long,long long> cost;
        cost[Y] = 0;
        cost[X] = INF;
        priority_queue<PLL,vector<PLL>,greater<PLL>> Q;
        Q.push(make_pair((long long)0,Y));
        while(!Q.empty()){
            long long now = Q.top().second , c = Q.top().first; Q.pop();
            if(cost.count(0) && c > cost[X]) break;
            //-1繰り返し
            if(abs(X-now)*D+c > 0) cost[X] = min(cost[X],abs(X-now)*D + c);
            //2倍
            long long c2 = now + 2-(now%2);
            if(cost.count(c2/2)) {
                if(abs(now-c2)*D + c + A > 0){
                    if(cost[c2/2] > abs(now-c2)*D + c + A) Q.push(make_pair(abs(now-c2)*D + c + A , c2/2)); 
                    cost[c2/2] = min(cost[c2/2], (abs(now-c2)*D) + c + A);
                }
            }
            else{
                if(abs(now-c2)*D + c + A > 0){
                    Q.push(make_pair(abs(now-c2)*D + c + A , c2/2)); 
                    cost[c2/2] = (abs(now-c2)*D) + c + A;
                }
            }
            c2 -= 2;
            if(cost.count(c2/2)) {
                if(abs(now-c2)*D + c + A > 0){
                    if(cost[c2/2] > abs(now-c2)*D + c + A) Q.push(make_pair(abs(now-c2)*D + c + A , c2/2));  
                    cost[c2/2] = min(cost[c2/2], (abs(now-c2)*D) + c + A);
                }
            }
            else{
                if(abs(now-c2)*D + c + A > 0){
                    Q.push(make_pair(abs(now-c2)*D + c + A , c2/2));  
                    cost[c2/2] = (abs(now-c2)*D) + c + A;
                }
            }
        }
        /*for(auto P : cost){
            cout << P.first << " " << P.second << endl;
        }*/
        ans.push_back(cost[X]);
    }
    for(long long OUTPUT : ans) cout << OUTPUT << endl;
}