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

typedef long long              ll;
typedef long double            ld;
typedef pair<int, int>         pi;
typedef vector<int>            vi;
typedef vector<long long>      vll;
typedef vector<pair<int, int>> vpi;

#define pb            push_back
#define pf            push_front
#define all(x)        begin(x), end(x)
#define sz(x)         (int)(x).size()
#define ff            first
#define ss            second
#define mp            make_pair
#define lb            lower_bound
#define ub            upper_bound
#define Tcase()       int tcc; cin >> tcc; while(tcc--)
#define FOR(i, a, b)  for(int i = (a); i < (b); i++)
#define F0R(i, a)     FOR(i, 0, a)

const ll  MOD   = 998244353;
const ll  INF   = 9e18;
const int MX    = 2e5 + 5;
const ld  PI    = acos((ld) -1);
const int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

void setIO(string name = "") {
    ios_base::sync_with_stdio(0); cin.tie(0);
    if(sz(name)){
        freopen((name+".in").c_str(), "r", stdin);
        freopen((name+".out").c_str(), "w", stdout);
    }
}

int Dp[5005][5005];

int main(){

    setIO();

    int n, m;
    cin >> n >> m;

    int a[n], b[m];
    F0R(i, n) cin >> a[i];
    F0R(i, m) cin >> b[i];

    F0R(i, n + 1){
        F0R(j, m + 1){
            Dp[i][j] = MOD;
            if(!i) Dp[i][j] = j;
            if(!j) Dp[i][j] = i;
        }
    }

    FOR(i, 1, n + 1){
        FOR(j, 1, m + 1){

            if(a[i - 1] == b[j - 1]) Dp[i][j] = Dp[i - 1][j - 1];
            else Dp[i][j] = min({Dp[i - 1][j], Dp[i][j - 1], Dp[i - 1][j - 1]}) + 1;
        }
    }

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

//OM TAT SAT
#include<bits/stdc++.h>
#define N 100010
using namespace std;
int a[N],b[N];
int main()
{
    int n;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];

    }//cout<<aa<<bb<<endl;
    int ans=10000000,p;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            //ans=min(ans,i==j? a[i]+b[j] :max(a[i],b[j]));
            if(i==j)
            p=a[i]+b[j];
           else
            p=max(a[i],b[j]);

           ans=min(ans,p);

        }
    }
    cout<<ans<<endl;

    return 0;
}
