#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N;
	int a = 0, b = 0;
	vector<int> T;
	vector<int> dp;
	
	cin>>N;
	
	T.resize(N);
	dp.resize(110000, INF);
	dp[0] = 0;
	
	for(int i = 0; i < N; i++){
		cin>>T[i];
	}
	
	for(int i = 0; i < N; i++){
		vector<int> ndp(110000, INF);
		
		for(int j = (int)dp.size()-1; j >= 0; j--){
			if(j+T[i] < dp.size()) {
				ndp[j+T[i]] = min(ndp[j+T[i]], dp[j]);
			}
			
			ndp[j] = min(dp[j] + T[i], ndp[j]);
		}
		
		swap(ndp, dp);
		// cout<<endl;
		// cout<<"====== "<<i<<" T = "<<T[i]<<endl;
		// for(int i = 0; i <= 20; i++){
			// cout<<i<<" <><> "<<dp[i]<<" "<<endl; 
		// }
		// cout<<endl;
	}
	
	int ans = INF;
	for(int i = 0; i < dp.size(); i++){
		ans = min(ans, max(dp[i], i));
	}
	
	cout<<ans<<endl;
	
	// sort(T.begin(), T.end(), greater<int>());
	
	// for(int i = 0; i < N; i++){
		// if(a < b) {
			// a += T[i];
		// } else {
			// b += T[i];
		// }
	// }
	
	// cout<<max(a, b)<<endl;;
	
	return 0;
}
#include<bits/stdc++.h>
#include<cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll> 
#define vd vector<double>
#define vld vector<long double>
#define ll long long int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for(ll i=0; i<n; i++) 
#define forr1(i, n) for(ll i=1; i<=n; i++)
using namespace std;
const ld PI =3.1415926535897923846;
const ll MOD = 1000000007;
const ll N=998244353;
ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;}
ll modinverse(ll a){return power(a, MOD-2);}
ll n, m;
vl a, b;
int dp[1001][1001];
ll ans(ll ind1, ll ind2)
{
	if(ind1==n||ind2==m) 
		return max(n-ind1, m-ind2);
	if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];
	if(a[ind1]==b[ind2])
	{
		// either take these 
		return dp[ind1][ind2]=min({
			ans(ind1+1, ind2+1),
			1+ans(ind1+1, ind2),
			1+ans(ind1, ind2+1),
		});
	}
	else
	{
		return dp[ind1][ind2]=min({
			1+ans(ind1+1, ind2+1),
			1+ans(ind1+1, ind2),
			1+ans(ind1, ind2+1),
		});
	}
}
void solve()
{
    cin>>n>>m;
    a.resize(n);
    b.resize(m);
    forr(i, n)
    {
    	cin>>a[i];
    }
    forr(i, m)
    {
    	cin>>b[i];
    }
    forr(i, n+1) forr(j, m+1) dp[i][j]=-1;
    cout<<ans(0, 0)<<endl;
}
int main()
{
	#ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}