#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
using ll = long long;
#define endl "\n";
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x);
#define pvector(x) for(auto i : x)cout << i << " ";cout << "\n";
#define ivector(x) for(auto &i : x)cin >> i;
const int MOD = 1e9 + 7;
int main() {
	fast;
	auto S = [](int n){
		string s = to_string(n);
		int sum=0;
		for(char i : s)sum += (i-'0');
		return sum;
	};
	int a,b; cin >> a >> b;
	cout << max(S(a),S(b)) << "\n";
}


/*
    solution and tut used...
    credits : @yash_daga
    author : Aryan Agarwal, IIT KGP
    created : 13-December-2020 19:44:17 IST
*/

#include <bits/stdc++.h>
using namespace std;
#define int long long

const int mxn = 1e5;
const long long INF = 2e18;
const int32_t M = 1000000007; /*more than (10)^9*/ /*7 + 1e9*/
// const int32_t M = 998244353; /*less than (10)^9*/   /*1 + 7*17*(2)^23*/
const long double pie = acos(-1);

#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)

#define dbg(a) cout<<"["<<#a<<" : "<<a<<"]\n"
#define dbg2(a,b) cout<<"["<<#a<<" : "<<a<<"], "<<"["<<#b<<" : "<<b<<"]\n"
#define dbg3(a,b,c) cout<<"["<<#a<<" : "<<a<<"], "<<"["<<#b<<" : "<<b<<"], "<<"["<<#c<<" : "<<c<<"]\n"

void solve()
{
    int n,m;
    cin>>n>>m;
    int a[n+1],b[m+1];

    F(i,1,n)cin>>a[i];
    F(i,1,m)cin>>b[i];

    int dp[n+1][m+1];
    dp[0][0]=0;
    F(i,1,n)
    {
        F(j,1,m)
        {
            dp[i][j]=n+m;
        }
    }
    F(i,1,n)dp[i][0]=i;
    F(i,1,m)dp[0][i]=i;
    F(i,1,n)
    {
        F(j,1,m)
        {
            dp[i][j]=min(dp[i-1][j]+1,dp[i][j]);
            dp[i][j]=min(dp[i][j-1]+1,dp[i][j]);
            dp[i][j]=min(dp[i-1][j-1]+(a[i]!=b[j]),dp[i][j]);
        }
    }

    cout<<dp[n][m];
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#ifdef ARYAN_SIEVE
    // defualt mxn_sieve = 1e5
    sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
    // default [L,R] = [1,1e5]
    segmented_sieve();
#endif
#ifdef ARYAN_FACT
    // default mxn_fact = 1e5
    fact_init();
#endif
    // cout<<fixed<<setprecision(10);
    int _t=1;
    // cin>>_t;
    for (int i=1;i<=_t;i++)
    {
        // cout<<"Case #"<<i<<": ";
        solve();
    }
    return 0;
}