#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;

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long int
#define pb push_back
#define pi pair<int, int>
#define pii pair<pi, int>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 1001
#define mod 1000000007
#define eps 1e-9

int n, m;
vector<int> v;
vector<int> k;
int dp[MAXN][MAXN];

int solve(int i, int j)
{
  if (i == n)
  {
    return m - j;
  }
  if (j == m)
  {
    return n - i;
  }
  if (dp[i][j] != -1)
  {
    return dp[i][j];
  }
  int ret = LLONG_MAX;
  ret = (v[i] != k[j]) ? min(ret, 1 + solve(i + 1, j + 1)) : min(ret, 0 + solve(i + 1, j + 1));
  ret = min(ret, 1 + solve(i + 1, j));
  ret = min(ret, 1 + solve(i, j + 1));
  return dp[i][j] = ret;
}
signed main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  v.resize(n);
  k.resize(m);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  for (int i = 0; i < m; i++)
    cin >> k[i];
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
  return 0;
}
// 1 2 1 3
// 1 3 1

// 1 2 1
// 1 3 1

// 1 elemento removido
// apenas 1 par de indices que os caras sao diferentes
// ans = 2
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x) \
	for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T>
inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return 1;
	}
	return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return 1;
	}
	return 0;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << "(" << p.first << " " << p.second << ")";
	return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	rep(i, v.size()) {
		if (i) os << " ";
		os << v[i];
	}
	return os;
}
template <class T, class U>
istream& operator>>(istream& is, pair<T, U>& p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
	rep(i, v.size()) { is >> v[i]; }
	return is;
}
const long long INFLL = 1LL << 60;
const int INF         = 1 << 30;
const double PI       = acos(-1);
#ifdef LOCAL
#define dbg(x) cerr << #x << ": " << (x) << '\n'
#define say(x) cerr << (x) << '\n'
#else
#define dbg(x)
#define say(x)
#endif
string solve(bool a) { return ((a) ? "Yes" : "No"); }
int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	ll b, c, ans = 0LL;
	cin >> b >> c;
	P left, right;
  if(c==0LL){
  	cout<<1<<endl;
    return 0;
  }
  if(c==1LL){
  	if(b==0LL){
      cout<<1<<endl;
  }else{
    cout<<2<<endl;}
    return 0;
  }
  P cost;
	left.F  = -b - (c - 1) / 2;
	right.F = b - c / 2;
  cerr<<left.F<<" "<<right.F<<'\n';
  cost=mp(abs(-b-left.F)*2+1,abs(b-right.F)*2);
  cerr<<cost<<'\n';
  /*
	if (c % 2 == 0) {
		left.S  = -right.F + 1;
		right.S = -left.F;
	} else {
		left.S  = -right.F;
		right.S = -left.F - 1;
	}
    */
  if(c >cost.F){
  	right.S=-left.F;
  }else{
  	right.S=-left.F-1;
  }
  if(c > cost.S){
  	left.S=-right.F;
  }else
  {
    left.S=-right.F-1;
  }  
  	if(left.F >left.S){
    	swap(left.F,left.S);
    }
  if(right.F >right.S){
    swap(right.F,right.S);
  }
	 if (left.F <= right.S && right.F <= left.S) {
		ans = abs(min({left.F, left.S,right.F, right.S})-max({left.F, left.S,right.F, right.S}))+1;
       cerr<<"one"<<endl;
	} else {
		ans = abs(right.F - right.S) + abs(left.F - left.S)+2;
	}
  cerr<<left<<right<<endl;
	cout << ans << endl;
}
// https://atcoder.jp/contests/arc112/tasks/arc112_b
/*
[(r-1/2)-B   (r-1/2)+1-B -B     -(r/2)+1-B]   0        [-r/2+B    (r/2)-1+B B
(-(r-1/2)-1 )+B]
[-D-1          -D ...    -B   ... -E] ...  0 ... [      E-1       E ...    B ...
D] 1                      c(r2)  c-2(r2)     0     c(r2)
*/
/*
B-c/2 B
-B-(c-1)/2 -B
*/