//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")

const int maxn = 1e5 + 5;

ll x, ans = 1e18 + 5;
map<ll, int> ca;

void calc(ll y)
{
	queue<pair<ll, int> > q;
	q.push({y, 0});
	while(q.size())
	{
		auto b = q.front();
		q.pop();
		int t = b.S;
		if(ca.count(b.F)&& ca[b.F] < t)
			continue;
		ca[b.F] = t;
		y = b.F;
		ans = min(ans, abs(y - x) + t);
		if(y % 2 == 0)
			q.push({y / 2, t + 1});
		else
		{
			q.push({y / 2, t + 2});
			q.push({y / 2 + 1, t + 2});
		}
	}
	/*if(ca.count(y))
		return;
	ca.insert(y);
	ans = min(ans, abs(y - x) + t);
	if(y == 0)
		return;
	if(y % 2 == 0)
		calc(y / 2, t + 1);
	else
	{
		calc(y / 2, t + 2);
		calc(y / 2 + 1, t + 2);
	}*/
}

int main()
{
	//ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll y;
	cin >> x >> y;
	if(x >= y)
	{
		cout << x - y << '\n';
		return 0;
	}
	else
	{
		calc(y);
		cout << ans << '\n';
	}
}

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scanf("%c",&a)
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 1e3 + 10;
int n, m;
int a[N], b[N];
int f[N][N];

int main() {
    n = qr(), m = qr();
    repi(i, 1, n)a[i] = qr();
    repi(i, 1, m)b[i] = qr();
    memset(f, 0x3f, sizeof(f));
    f[0][0] = 0;
    int ans = INF;
    repi(i, 0, m)f[0][i] = i;
    repi(i, 0, n)f[i][0] = i;
    repi(i, 1, n)
        repi(j, 1, m) {
            if (a[i] != b[j]) {
                f[i][j] = min({f[i][j], f[i - 1][j - 1] + 1, f[i][j - 1] + 1, f[i - 1][j] + 1});
            } else {
                f[i][j] = min({f[i][j], f[i - 1][j - 1], f[i][j - 1] + 1, f[i - 1][j] + 1});
            }
        }
    repi(i, 1, n)repi(j, 1, m)ans = min(ans, f[i][j] + n - i + m - j);
    pi(ans);
    return 0;
}