/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 1010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int n, m, a[MAXN], b[MAXN], dp[MAXN][MAXN];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> m;
    FOR(i,1,n) cin >> a[i];
    FOR(i,1,m) cin >> b[i];
    dp[n+1][m+1] = 0;
    FOR(i,1,n) dp[i][m+1] = n-i+1;
    FOR(j,1,m) dp[n+1][j] = m-j+1;
    FORE(i,n,1)
        FORE(j,m,1) {
            dp[i][j] = dp[i+1][j+1] + (a[i] != b[j]);
            dp[i][j] = min(dp[i][j], dp[i+1][j] + 1);
            dp[i][j] = min(dp[i][j], dp[i][j+1] + 1);
        }
    int res = n + m;
    FOR(i,1,n)
        FOR(j,1,m) res = min(res, dp[i][j] + i-1 + j-1);
    cout << res;
    return 0;
}

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

#define ll long long
#define ld long double
#define ull unsigned long long
#define pi 3.141592653589793238
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define For(i,a,n) for(ll i=a;i<=n;i++)
const ll inf = 100000000000000000;
const ll mod = 1000000007;
const ll N=2e5+1;

typedef pair<int, int>     pii;
typedef pair<ll , ll>      pll;
typedef vector<int>        vi;
typedef vector<ll>         vl;
typedef vector<ull>        vll;
typedef vector<pii>        vpii;
typedef vector<pll>        vpl;
typedef vector<vi>         vvi;
typedef vector<vl>         vvl;

bool ispalin(string s){ ll len=s.length(); rep(i , 0 , len){ if(s[i] != s[len-i-1]) {return false;}} return true;}

ll add(ll a , ll b , ll m){ ll res = (a%m+b%m)%m; if(res<0)   {res+=m;} return res;}

ll mul(ll a , ll b , ll m){ ll res = (a%m*b%m)%m; if(res<0) {res+=m;} return res;}

ll power(ll a , ll b , ll m){ ll res = 1; while (b > 0) { if (b & 1) {res = mul(res,a,m);} a = mul(a , a , m); b >>= 1;} return res%m; }

ll binpow(ll a , ll b) { ll res = 1; while (b > 0) {  if (b & 1)  {res = (res*a);} a = (a*a); b >>= 1; } return res; }

ll binpow(ll a , ll b , ll m) { ll res = 1; while (b > 0) {  if (b & 1)  {res = (res*a)%m;} a = (a*a)%m; b >>= 1; } return res%m; }

bool isprime(ll n){ if (n <= 1) {return false;} if (n <= 3) {return true;} if (n%2 == 0 || n%3 == 0) {return false;} for (ll i=5; i*i<=n; i=i+6){ if (n%i == 0 || n%(i+2) == 0) {return false;}} return true; }

/// bool isprime(ll n){ int x=rand(); ll int y;  y=pow((x-1),n)-pow(x,n)+1; if(y%n==0) {return true;} else {return false;} }
///  for 1 to n prime numbers .....see down code
/// bool isprime[n+1]; for(i=0;i<=n;i++) {isprime[i]=true;} isprime[0]=false; isprime[1]=false; for(i=0;i<=n;i++) { if(isprime[i] == true) { count++; for(j=i*i;j<=n;j+=i) {isprime[j]=false;} } }

ll gcd(ll a , ll b) { if(b==0)   {return a;} else     {gcd(b,a%b);} }
ll lcm(ll a , ll b) { ll l = (a*b)/gcd(a,b); return l; }

ll CEIL(ll a, ll b) { return (a/b+(a%b!=0));}

ll inv (ll a , ll m){ return power(a,m-2,m);}

ll C(ll n, ll r) {
    if(n<r)     return 0;
    if(n==r)    return 1;
    if(r > n-r)   r = n-r; // because C(n, r) == C(n, n - r)
    ll ans = 1;
    for(ll i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }
    return ans;
}

bool isPowerOfTwo(int x){  return x && (!(x & (x - 1)));}
bool isPerfectSq(ll x){ if (x >= 0) { ll sr = sqrt(x); return (sr * sr == x); } return false; }
ll fact(ll n , ll m){ ll f = 1;  for(ll i=1;i<=n;i++)  f = (f * i)%m;  return f%m;}

/* combinatorics .. */
// for getting max power of K that divides n!
ll fact_pow (ll n, ll k) { ll res = 0; while (n) { n /= k; res += n;} return res; }

void solve()
{
    ll x , y;
    cin>>x>>y;
    if(x == y)  cout<<x;
    else
    {
        if(x == 0 && y == 1 || y == 0 && x == 1)    cout<<"2";
        else if(x == 0 && y == 2 || x == 2 && y == 0)    cout<<"1";
        else    cout<<"0";
    }
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    solve();
	return 0;
}