#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;

const int N = 1e5 + 10;
int n, ans;
struct node { int k, a; }e[N];

bool cmp(node x, node y) {
	return x.k < y.k;
}

int main() {
	cin >> n;
	rep(i, 1, n) scanf("%d%d", &e[i].k, &e[i].a);
	sort(e + 1, e + n + 1, cmp);
	rep(i, 1, n - 1) {
		int tmp = e[i + 1].k - e[i].k;
		if (tmp >= 15) continue;
		int num = 1;
		rep(j, 1, tmp) num *= 4;
		if (num * e[i + 1].a < e[i].a) {
			e[i].a -= num * e[i + 1].a;
			e[i + 1].a += e[i].a / num + (e[i].a % num > 0);
		}
	}
	/* ans = e[n].k + 1;
	int num = 4;
	rep(i, 1, 14) {
		if (num >= e[n].a) return printf("%d\n", ans), 0;
		ans++, num *= 4;
	}*/

    ans = e[n].k;
    double t = (double)e[n].a - 0.1;
    if (e[n].a == 1) { printf("%d\n", ans + 1); return 0; }
    while (t >= 1) {
    	ans++, t /= 4;
    } printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;

const int N = 1e5 + 10;
typedef long long ll;
ll n, ans;
struct node { ll k, a; }e[N];

bool cmp(node x, node y) {
	return x.k < y.k;
}

int main() {
	cin >> n;
	rep(i, 1, n) scanf("%lld%lld", &e[i].k, &e[i].a);
	sort(e + 1, e + n + 1, cmp);
	rep(i, 1, n - 1) {
		ll tmp = e[i + 1].k - e[i].k;
		if (tmp >= 15) continue;
		ll num = 1;
		rep(j, 1, tmp) num *= 4;
		if (num * e[i + 1].a < e[i].a) {
			e[i].a -= num * e[i + 1].a;
			e[i + 1].a += e[i].a / num + (e[i].a % num > 0);
		}
	}
	/* ans = e[n].k + 1;
	int num = 4;
	rep(i, 1, 14) {
		if (num >= e[n].a) return printf("%d\n", ans), 0;
		ans++, num *= 4;
	}*/

    ans = e[n].k;
    double t = (double)e[n].a - 0.1;
    if (e[n].a == 1) { printf("%lld\n", ans + 1); return 0; }
    while (t >= 1) {
    	ans++, t /= 4;
    } printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;

const int N = 1e5 + 10;
int n, ans;
struct node { int k, a; }e[N];

bool cmp(node x, node y) {
	return x.k < y.k;
}

int main() {
	cin >> n;
	rep(i, 1, n) scanf("%d%d", &e[i].k, &e[i].a);
	sort(e + 1, e + n + 1, cmp);
	rep(i, 1, n - 1) {
		int tmp = e[i + 1].k - e[i].k;
		if (tmp >= 15) continue;
		int num = 1;
		rep(j, 1, tmp) num *= 4;
		if (num * e[i + 1].a < e[i].a) {
			e[i + 1].a += e[i].a - num * e[i + 1].a;
		}
	}
	ans = e[n].k + 1;
	int num = 4;
	rep(i, 1, 14) {
		if (num >= e[n].a) return printf("%d\n", ans), 0;
		ans++, num *= 4;
	}
	return 0;
}


#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;

const int N = 1e5 + 10;
int n, ans;
struct node { int k, a; }e[N];

bool cmp(node x, node y) {
	return x.k < y.k;
}

int main() {
	cin >> n;
	rep(i, 1, n) scanf("%d%d", &e[i].k, &e[i].a);
	sort(e + 1, e + n + 1, cmp);
	rep(i, 1, n - 1) {
		int tmp = e[i + 1].k - e[i].k;
		if (tmp >= 15) continue;
		int num = 1;
		rep(j, 1, tmp) num *= 4;
		if (num * e[i + 1].a < e[i].a) {
			e[i].a -= num * e[i + 1].a;
			e[i + 1].a += e[i].a / num + (e[i].a % num > 0);
		}
	}
	ans = e[n].k + 1;
	int num = 4;
	rep(i, 1, 14) {
		if (num >= e[n].a) return printf("%d\n", ans), 0;
		ans++, num *= 4;
	}
	return 0;
}


// ==========================================================================
//
//                   Bismillahir-Rahmanir-Rahim
//
// ==========================================================================
#include <bits/stdc++.h>
#define        ll                              long long
#define        f(x,y,z)                        for(int x=y;x<z;x++)
#define        take1(a);                       scanf("%d",&a);
#define        take2(a,b);                     scanf("%d%d",&a,&b);
#define        take3(a,b,c);                   scanf("%d%d%d",&a,&b,&c);
#define        take4(a,b,c,d);                 scanf("%d%d%d%d",&a,&b,&c,&d);
#define        pii                             pair<int,int>
#define        mem(a,x)                        memset(a,x,sizeof(a))
#define        N                               100010
#define        M                               1000000007
#define        pi                              acos(-1.0)
#define        ff                              first
#define        ss                              second
#define        pb                              push_back
using namespace std;
pii ar[N];ll dp[N];
ll go(ll b){
       if(b==1) return 1;
      ll x=1;ll ans=0;
     while(x<b){
         x*=4;
         ans++;
      }
   return ans;
}
int main()
{
    int n;take1(n);
    f(i,0,n) cin>>ar[i].ff>>ar[i].ss;
      sort(ar,ar+n);
      f(i,0,n){
         // cout<<go(1)<<endl;
          dp[i+1]=max(dp[i],dp[i+1]+go(ar[i].ss)+ar[i].ff);
      }
      cout<<dp[n]<<endl;
      return 0;
}


// ==========================================================================
//
//                   Bismillahir-Rahmanir-Rahim
//
// ==========================================================================
#include <bits/stdc++.h>
#define        ll                              long long
#define        f(x,y,z)                        for(int x=y;x<z;x++)
#define        take1(a);                       scanf("%d",&a);
#define        take2(a,b);                     scanf("%d%d",&a,&b);
#define        take3(a,b,c);                   scanf("%d%d%d",&a,&b,&c);
#define        take4(a,b,c,d);                 scanf("%d%d%d%d",&a,&b,&c,&d);
#define        pii                             pair<int,int>
#define        mem(a,x)                        memset(a,x,sizeof(a))
#define        N                               100010
#define        M                               1000000007
#define        pi                              acos(-1.0)
#define        ff                              first
#define        ss                              second
#define        pb                              push_back
using namespace std;
pii ar[N];ll dp[N];
ll go(ll b){
      ll x=1;ll ans=0;
     while(x<b){
         x*=4;
         ans++;
      }
   return ans;
}
int main()
{
    int n;take1(n);
    f(i,0,n) cin>>ar[i].ff>>ar[i].ss;
      sort(ar,ar+n);
      f(i,0,n){
          dp[i+1]=max(dp[i],dp[i+1]+go(ar[i].ss)+ar[i].ff);
      }
      cout<<dp[n]<<endl;
      return 0;
}


#include <bits/stdc++.h>

#define left nadnlassad
#define right asdaslknd
#define y1 kjdajasjdsas

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define ll long long
#define ld long double
#define ull unsigned ll

#define _hash pair<int, ull>
#define pii pair<int, int>
#define prr pair<pii, pii>

#define sqr(x) ((x) * 1LL * (x))

#define vec vector<int>
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define upper(v, x) (upper_bound(all(v), x) - v.begin())
#define lower(v, x) (lower_bound(all(v), x) - v.begin())
#define iter set<int>::iterator
#define _bits(x) __builtin_popcountll(x)

using namespace std;

const int nx[8] = {2, -2, -2, 2, 1, 1, -1, -1};
const int ny[8] = {1, 1, -1, -1, 2, -2, -2, 2};

const ll LINF = (ll) 2e18;
const int INF = 1e9 + 7;

const int PF[5] = {77, 31, 98, 45, 67};
const int MD[6] = {INF, 1000019677, 1000019393, 1000007521, 1000024073, 1000009667};

const int N = 2e5 + 100; // change this !!!
const int MAXN = 5e7 + 50;
const double EPS = 1e-9, PI = 2 * acos(0.0);
int n;
pii a[N];

bool ok(int x) {
   ll c = 1;
   for(int i = 1; i <= x; i++) {
      if(c >= a[n].s) return true;
      c *= 4;
   }
   if(c >= a[n].s) return true;
   return false;
}

int main() {
   #define fname "f"
   #ifdef sony
      freopen("input.txt", "r", stdin);
      //freopen("output1.txt", "w", stdout);
   #else
      //freopen(fname".in", "r", stdin);
      //freopen(fname".out", "w", stdout);
   #endif //sony
   srand(time(NULL));
   cin >> n;
   for(int i = 1; i <= n; i++)
      cin >> a[i].f >> a[i].s;
   sort(a + 1, a + n + 1);
   for(int i = 1; i < n; i++) {
      int c = 1, d = a[i + 1].f - a[i].f;
      while(d > 0 && c < a[i].s)
         c *= 4, d--;
      int K = a[i + 1].s / c;
      if(d > 0 || K * c < a[i].s)
         a[i + 1].s++;
   }
   int lo = 0, hi = INF;
   while(hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if(ok(mid)) hi = mid;
      else lo = mid;
   }
   if(ok(lo)) hi = lo;
   cout << a[n].f + hi << endl;
   return 0;
}


#include <bits/stdc++.h>

#define left nadnlassad
#define right asdaslknd
#define y1 kjdajasjdsas

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define ll long long
#define ld long double
#define ull unsigned ll

#define _hash pair<int, ull>
#define pii pair<int, int>
#define prr pair<pii, pii>

#define sqr(x) ((x) * 1LL * (x))

#define vec vector<int>
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define upper(v, x) (upper_bound(all(v), x) - v.begin())
#define lower(v, x) (lower_bound(all(v), x) - v.begin())
#define iter set<int>::iterator
#define _bits(x) __builtin_popcountll(x)

using namespace std;

const int nx[8] = {2, -2, -2, 2, 1, 1, -1, -1};
const int ny[8] = {1, 1, -1, -1, 2, -2, -2, 2};

const ll LINF = (ll) 2e18;
const int INF = 1e9 + 7;

const int PF[5] = {77, 31, 98, 45, 67};
const int MD[6] = {INF, 1000019677, 1000019393, 1000007521, 1000024073, 1000009667};

const int N = 2e5 + 100; // change this !!!
const int MAXN = 5e7 + 50;
const double EPS = 1e-9, PI = 2 * acos(0.0);
int n;
pii a[N];

bool ok(int x) {
   ll c = 1;
   for(int i = 1; i <= x; i++) {
      if(c >= a[n].s) return true;
      c *= 4;
   }
   if(c >= a[n].s) return true;
   return false;
}

int main() {
   #define fname "f"
   #ifdef sony
      freopen("input.txt", "r", stdin);
      //freopen("output1.txt", "w", stdout);
   #else
      //freopen(fname".in", "r", stdin);
      //freopen(fname".out", "w", stdout);
   #endif //sony
   srand(time(NULL));
   cin >> n;
   for(int i = 1; i <= n; i++)
      cin >> a[i].f >> a[i].s;
   sort(a + 1, a + n + 1);
   for(int i = 1; i < n; i++) {
      int c = 1, d = a[i + 1].f - a[i].f;
      while(d > 0) {
         c *= 4;
         if(c > a[i].s)
            break;
         d--;
      }
      if(d) {
         a[i + 1].s++;
      } else {
         int can_give = a[i].s / c;
         can_give = min(can_give, a[i + 1].s);
         a[i].s -= can_give * c;
         if(a[i].s)
            a[i + 1].s++;
      }
   }
   cerr << a[n].s << endl;
   int lo = 1, hi = INF;
   while(hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if(ok(mid)) hi = mid;
      else lo = mid;
   }
   if(ok(lo)) hi = lo;
   cout << a[n].f + hi << endl;
   return 0;
}


#include <bits/stdc++.h>

#define left nadnlassad
#define right asdaslknd
#define y1 kjdajasjdsas

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define ll long long
#define ld long double
#define ull unsigned ll

#define _hash pair<int, ull>
#define pii pair<int, int>
#define prr pair<pii, pii>

#define sqr(x) ((x) * 1LL * (x))

#define vec vector<int>
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define upper(v, x) (upper_bound(all(v), x) - v.begin())
#define lower(v, x) (lower_bound(all(v), x) - v.begin())
#define iter set<int>::iterator
#define _bits(x) __builtin_popcountll(x)

using namespace std;

const int nx[8] = {2, -2, -2, 2, 1, 1, -1, -1};
const int ny[8] = {1, 1, -1, -1, 2, -2, -2, 2};

const ll LINF = (ll) 2e18;
const int INF = 1e9 + 7;

const int PF[5] = {77, 31, 98, 45, 67};
const int MD[6] = {INF, 1000019677, 1000019393, 1000007521, 1000024073, 1000009667};

const int N = 2e5 + 100; // change this !!!
const int MAXN = 5e7 + 50;
const double EPS = 1e-9, PI = 2 * acos(0.0);
int n;
pii a[N];

int main() {
   #define fname "f"
   #ifdef sony
      freopen("input.txt", "r", stdin);
      //freopen("output1.txt", "w", stdout);
   #else
      //freopen(fname".in", "r", stdin);
      //freopen(fname".out", "w", stdout);
   #endif //sony
   srand(time(NULL));
   cin >> n;
   for(int i = 1; i <= n; i++)
      cin >> a[i].f >> a[i].s;
   sort(a + 1, a + n + 1);
   for(int i = 1; i < n; i++) {
      int c = 1, d = a[i + 1].f - a[i].f;
      while(d > 0 && c < a[i].s)
         c *= 4, d--;
      int K = a[i + 1].s / c;
      if(d > 0 || K * c < a[i].s)
         a[i + 1].s++;
   }
   while(a[n].s > 0) {
      a[n].f++;
      a[n].s = (a[n].s - 1) /4;
   }
   cout << a[n].f << endl;
   return 0;
}


#include <bits/stdc++.h>

#define left nadnlassad
#define right asdaslknd
#define y1 kjdajasjdsas

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define ll long long
#define ld long double
#define ull unsigned ll

#define _hash pair<int, ull>
#define pii pair<int, int>
#define prr pair<pii, pii>

#define sqr(x) ((x) * 1LL * (x))

#define vec vector<int>
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define upper(v, x) (upper_bound(all(v), x) - v.begin())
#define lower(v, x) (lower_bound(all(v), x) - v.begin())
#define iter set<int>::iterator
#define _bits(x) __builtin_popcountll(x)

using namespace std;

const int nx[8] = {2, -2, -2, 2, 1, 1, -1, -1};
const int ny[8] = {1, 1, -1, -1, 2, -2, -2, 2};

const ll LINF = (ll) 2e18;
const int INF = 1e9 + 7;

const int PF[5] = {77, 31, 98, 45, 67};
const int MD[6] = {INF, 1000019677, 1000019393, 1000007521, 1000024073, 1000009667};

const int N = 2e5 + 100; // change this !!!
const int MAXN = 5e7 + 50;
const double EPS = 1e-9, PI = 2 * acos(0.0);
int n;
pii a[N];

bool ok(int x) {
   ll c = 1;
   for(int i = 1; i <= x; i++) {
      if(c >= a[n].s) return true;
      c *= 4;
   }
   if(c >= a[n].s) return true;
   return false;
}

int main() {
   #define fname "f"
   #ifdef sony
      freopen("input.txt", "r", stdin);
      //freopen("output1.txt", "w", stdout);
   #else
      //freopen(fname".in", "r", stdin);
      //freopen(fname".out", "w", stdout);
   #endif //sony
   srand(time(NULL));
   cin >> n;
   for(int i = 1; i <= n; i++)
      cin >> a[i].f >> a[i].s;
   sort(a + 1, a + n + 1);
  // for(int i = 1; i <= n; i++)
  //    cout << a[i].f << ' ' << a[i].s << "\n"; cout << "---------\n";
   for(int i = 1; i < n; i++) {
      int c = 1, d = a[i + 1].f - a[i].f;
      while(d > 0) {
         c *= 4;
         if(c > a[i].s)
            break;
         d--;
      }
     // cout << i << " ";
      if(d) {
     //    cout << "can't reach";
         a[i + 1].s++;
      } else {
         int can_give = a[i].s / c;
         can_give = min(can_give, a[i + 1].s);
   //      cout << can_give;
         a[i].s -= can_give * c;
         if(a[i].s)
            a[i + 1].s += (a[i].s + c - 1) / c;
      }
  //    cout << endl;
   }
   cerr << a[n].s << endl;
   int lo = 1, hi = INF;
   while(hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if(ok(mid)) hi = mid;
      else lo = mid;
   }
   if(ok(lo)) hi = lo;
   cout << a[n].f + hi << endl;
   return 0;
}


#include <bits/stdc++.h>

#define left nadnlassad
#define right asdaslknd
#define y1 kjdajasjdsas

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define ll long long
#define ld long double
#define ull unsigned ll

#define _hash pair<int, ull>
#define pii pair<int, int>
#define prr pair<pii, pii>

#define sqr(x) ((x) * 1LL * (x))

#define vec vector<int>
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define upper(v, x) (upper_bound(all(v), x) - v.begin())
#define lower(v, x) (lower_bound(all(v), x) - v.begin())
#define iter set<int>::iterator
#define _bits(x) __builtin_popcountll(x)

using namespace std;

const int nx[8] = {2, -2, -2, 2, 1, 1, -1, -1};
const int ny[8] = {1, 1, -1, -1, 2, -2, -2, 2};

const ll LINF = (ll) 2e18;
const int INF = 1e9 + 7;

const int PF[5] = {77, 31, 98, 45, 67};
const int MD[6] = {INF, 1000019677, 1000019393, 1000007521, 1000024073, 1000009667};

const int N = 2e5 + 100; // change this !!!
const int MAXN = 5e7 + 50;
const double EPS = 1e-9, PI = 2 * acos(0.0);
int n;
pii a[N];

int get(int id) {
   int k = a[id].f;
   while(a[id].s > 4) {
      k++;
      a[id].s = (a[id].s + 3) / 4;
   }
   return k;
}

int main() {
   #define fname "f"
   #ifdef sony
      freopen("input.txt", "r", stdin);
      //freopen("output1.txt", "w", stdout);
   #else
      //freopen(fname".in", "r", stdin);
      //freopen(fname".out", "w", stdout);
   #endif //sony
   srand(time(NULL));
   cin >> n;
   int ans = 0;
   for(int i = 1; i <= n; i++) {
      cin >> a[i].f >> a[i].s;
      ans = max(ans, get(i));
   }
   cout << ans + 1 << endl;
   return 0;
}


#include <bits/stdc++.h>

#define left nadnlassad
#define right asdaslknd
#define y1 kjdajasjdsas

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define ll long long
#define ld long double
#define ull unsigned ll

#define _hash pair<int, ull>
#define pii pair<int, int>
#define prr pair<pii, pii>

#define sqr(x) ((x) * 1LL * (x))

#define vec vector<int>
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define upper(v, x) (upper_bound(all(v), x) - v.begin())
#define lower(v, x) (lower_bound(all(v), x) - v.begin())
#define iter set<int>::iterator
#define _bits(x) __builtin_popcountll(x)

using namespace std;

const int nx[8] = {2, -2, -2, 2, 1, 1, -1, -1};
const int ny[8] = {1, 1, -1, -1, 2, -2, -2, 2};

const ll LINF = (ll) 2e18;
const int INF = 1e9 + 7;

const int PF[5] = {77, 31, 98, 45, 67};
const int MD[6] = {INF, 1000019677, 1000019393, 1000007521, 1000024073, 1000009667};

const int N = 2e5 + 100; // change this !!!
const int MAXN = 5e7 + 50;
const double EPS = 1e-9, PI = 2 * acos(0.0);
int n;
pii a[N];

bool ok(int x) {
   ll c = 1;
   for(int i = 1; i <= x; i++) {
      if(c >= a[n].s) return true;
      c *= 4;
   }
   if(c >= a[n].s) return true;
   return false;
}

int main() {
   #define fname "f"
   #ifdef sony
      freopen("input.txt", "r", stdin);
      //freopen("output1.txt", "w", stdout);
   #else
      //freopen(fname".in", "r", stdin);
      //freopen(fname".out", "w", stdout);
   #endif //sony
   srand(time(NULL));
   cin >> n;
   for(int i = 1; i <= n; i++)
      cin >> a[i].f >> a[i].s;
   sort(a + 1, a + n + 1);
   for(int i = 1; i < n; i++) {
      int c = 1, d = a[i + 1].f - a[i].f;
      while(d > 0 && c < a[i].s)
         c *= 4, d--;
      int K = a[i + 1].s / c;
      if(d > 0 || K * c < a[i].s)
         a[i + 1].s++;
   }
   int lo = 1, hi = INF;
   while(hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if(ok(mid)) hi = mid;
      else lo = mid;
   }
   if(ok(lo)) hi = lo;
   cout << a[n].f + hi << endl;
   return 0;
}


//by david942j
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <deque>
#include <cassert>
#include <queue>
#include <stack>
#include <cstdlib>
#define openfile(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define mpr std::make_pair
#define lg(x) (31-__builtin_clz(x))
#define lgll(x) (63-__builtin_clzll(x))
#define __count __builtin_popcount
#define __countll __builtin_popcountll
#define X first
#define Y second
#define mst(x) memset(x,0,sizeof(x))
#define mst1(x) memset(x,-1,sizeof(x))
#define ALL(c) (c).begin(),(c).end()
#define FOR(i,n) for(int i=0;i<n;i++)
#define FOR1(i,n) for(int i=1;i<=n;i++)
#define FORit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define pb push_back
#define RI(x) scanf("%d",&x)
#define RID(x) int x;RI(x)
using namespace std;
typedef long long LL;
typedef double LD;
typedef vector<int> VI;
typedef std::pair<int,int> PII;
template<class T>inline void maz(T &a,T b){if(a<b)a=b;}
template<class T>inline void miz(T &a,T b){if(a>b)a=b;}
template<class T>inline T abs(T a){return a>0?a:-a;}
const int N=200000, INF=1e9;
const LD EPS=1e-6, MAGIC=381401241.01201;
int n,m,k;
int A[N];
char s[N],t[N];
LL prv[N];
const LL mod=1e9+7;
bool nd[N];
int main() {
    RI(n);
    int ans=0;
    FOR(i,n) {
        RID(a);RID(b);
        int tmp=sqrt(b);
        if(tmp*tmp<b)tmp++;
        a+=lg(tmp);
        if((1<<lg(tmp))!=tmp) a++;
        if(b==1)a++;
        maz(ans,a);
    }
    printf("%d\n",ans);
}
/*

 */


//by david942j
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <deque>
#include <cassert>
#include <queue>
#include <stack>
#include <cstdlib>
#define openfile(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define mpr std::make_pair
#define lg(x) (31-__builtin_clz(x))
#define lgll(x) (63-__builtin_clzll(x))
#define __count __builtin_popcount
#define __countll __builtin_popcountll
#define X first
#define Y second
#define mst(x) memset(x,0,sizeof(x))
#define mst1(x) memset(x,-1,sizeof(x))
#define ALL(c) (c).begin(),(c).end()
#define FOR(i,n) for(int i=0;i<n;i++)
#define FOR1(i,n) for(int i=1;i<=n;i++)
#define FORit(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define pb push_back
#define RI(x) scanf("%d",&x)
#define RID(x) int x;RI(x)
using namespace std;
typedef long long LL;
typedef double LD;
typedef vector<int> VI;
typedef std::pair<int,int> PII;
template<class T>inline void maz(T &a,T b){if(a<b)a=b;}
template<class T>inline void miz(T &a,T b){if(a>b)a=b;}
template<class T>inline T abs(T a){return a>0?a:-a;}
const int N=200000, INF=1e9;
const LD EPS=1e-6, MAGIC=381401241.01201;
int n,m,k;
int A[N];
char s[N],t[N];
LL prv[N];
const LL mod=1e9+7;
bool nd[N];
int main() {
    RI(n);
    int ans=0;
    FOR(i,n) {
        RID(a);RID(b);
        int tmp=sqrt(b);
        if(tmp*tmp<b)tmp++;
        a+=lg(tmp);
        if((1<<lg(tmp))!=tmp) a++;
        maz(ans,a);
    }
    printf("%d\n",ans);
}
/*

 */


//In The Name of Beauty
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef vector<ll> vll;
#define IB std::ios::sync_with_stdio(0);
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define pll pair<ll,ll>
#define F first
#define S second
ll const MAXN = 1e5 + 8;
ll const INF  = 1e12 + 8;
ll const delta = 1000000007;

int main()
{
    IB;
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin >> n;
    ll a[n] , b[n];
    ll ans = 0;
    for(ll i = 0;i < n;i++)
    {
        cin >> a[i] >> b[i];
        ll lg = 0;
        ll t = b[i];
        ll p = 1;
        while(b[i])
        {
            lg++;
            b[i] /= 4;
            p *= 4;
        }
        if(p / 4 == t)lg--;
        ans = max(ans,a[i] + lg);
    }
    cout << ans;
    return 0;
}
//Written by M_H_H_7


//In The Name of Beauty
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef vector<ll> vll;
#define IB std::ios::sync_with_stdio(0);
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define pll pair<ll,ll>
#define F first
#define S second
ll const MAXN = 1e5 + 8;
ll const INF  = 1e12 + 8;
ll const delta = 1000000007;

map<ll,ll> cnt;

int main()
{
    IB;
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin >> n;
    ll a[n] , b[n];
    for(ll i = 0;i < n;i++)
    {
        cin >> a[i] >> b[i];
        cnt[a[i]] += b[i];
        if(cnt[a[i]] % 4 == 0)
        {
            cnt[a[i] + 1] += ((cnt[a[i]] - 4) / 4);
            cnt[a[i]] = 4;
        }
        else
        {
            cnt[a[i] + 1] += (cnt[a[i]] / 4);
            cnt[a[i]] %= 4;
        }
    }
    ll mmax = 0;
    for(map<ll,ll>::iterator i = cnt.begin();i != cnt.end();i++)
    {
        if(!(i->S))continue;
        mmax = max(mmax,i->F + 1);
    }
    cout << mmax;
    return 0;
}
//Written by M_H_H_7


//In The Name of Beauty
#include <bits/stdc++.h>

using namespace std;

typedef long long   ll;
typedef vector<ll> vll;
#define IB std::ios::sync_with_stdio(0);
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define pll pair<ll,ll>
#define F first
#define S second
ll const MAXN = 1e5 + 8;
ll const INF  = 1e12 + 8;
ll const delta = 1000000007;

int main()
{
    IB;
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin >> n;
    ll a[n] , b[n];
    ll ans = 0;
    for(ll i = 0;i < n;i++)
    {
        cin >> a[i] >> b[i];
        ll lg = 0;
        ll t = b[i];
        ll p = 1;
        while(b[i])
        {
            lg++;
            b[i] /= 4;
            p *= 4;
        }
        if(p / 4 == t)lg--;
        ans = max(ans,a[i] + lg);
    }
    cout << ans;
    return 0;
}
//Written by M_H_H_7


//In The Name of Beauty
#include <bits/stdc++.h>

using namespace std;

typedef long long   ll;
typedef vector<ll> vll;
#define IB std::ios::sync_with_stdio(0);
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define pll pair<ll,ll>
#define F first
#define S second
ll const MAXN = 1e5 + 8;
ll const INF  = 1e12 + 8;
ll const delta = 1000000007;

int main()
{
    IB;
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin >> n;
    ll a[n] , b[n];
    ll ans = 0;
    ll mmax = 0;
    for(ll i = 0;i < n;i++)
    {
        cin >> a[i] >> b[i];
        mmax = max(mmax,a[i]);
        ll lg = 0;
        ll t = b[i];
        ll p = 1;
        while(b[i])
        {
            lg++;
            b[i] /= 4;
            p *= 4;
        }
        if(p / 4 == t)lg--;
        ans = max(ans,a[i] + lg);
    }
    if(ans == mmax)ans++;
    cout << ans;
    return 0;
}
//Written by M_H_H_7


# include <bits/stdc++.h>

# define ll long long
# define pb push_back
# define ppb pop_back

using namespace std;

const ll N = 1e5 + 10;
const ll inf = -1e18;
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double PI = acos(-1);

ll n, p, c, ans, k[N], a[N];

int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) {
        cin >> k[i] >> a[i];
    }
    for(int i = 1;i <= n;i++) {
        p = 1, c = 0;
        while(p < a[i]) {
            p = p * 4;
            c++;
        }
        ans = max(ans, c + k[i] + (a[i] == 1));
    }
    cout << ans;
}


# include <bits/stdc++.h>

# define ll long long
# define pb push_back
# define ppb pop_back

using namespace std;

const ll N = 1e5 + 10;
const ll inf = -1e18;
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double PI = acos(-1);

ll n, p, c, ans, k[N], a[N];

int main() {
    cin >> n;
    for(int i = 1;i <= n;i++) {
        cin >> k[i] >> a[i];
    }
    for(int i = 1;i <= n;i++) {
        p = 1, c = 0;
        while(p < a[i]) {
            p = p * 4;
            c++;
        }
        ans = max(ans, c + k[i]);
    }
    cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
//1->0
//4->1
//16->2
//64->3
int main(void){
    long long k,a;
    long long ans=0;
    int n=0;
    cin>>n;
    while(n--){
        cin>>k>>a;
        long long temp=k;
        long long t=0;
        long long i=1;
        while(i<a){t++;i*=4;}
        if(t==0)    t=1;
        temp+=t;
        ans=max(ans,temp);
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
//1->0
//4->1
//16->2
//64->3
int main(void){
    long long k,a,ans=0,n=0;
    cin>>n;
    while(n--){
        cin>>k>>a;
        ans=max(ans,1+k);
        a--;
        while(a>0){a/=4;k++;}
        ans=max(ans,k);
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
//1->0
//4->1
//16->2
//64->3
int main(void){
    long long k,a;
    long long ans=0;
    int n=0;
    cin>>n;
    while(n--){
        cin>>k>>a;
        long long temp=k;
        long long t=0;
        long long i=1;
        while(i<a){t++;i*=4;}
        temp+=t;
        ans=max(ans,temp);
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <cmath>
using namespace std;
int main(){
	int n,a,b,minl=0;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> b >> a;
		a--;
		while(a>0){
			a = a/4;
			b++;
		}
		minl = max(minl,b);
	}
	cout << minl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
int main(){
	int n,a,b,minl=0;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> b >> a;
		if(a!=1){
			a--;
			while(a>0){
				a = a/4;
				b++;
			}
			minl = max(minl,b);
		}
		else{
			minl = max(minl,b);
		}
	}
	cout << minl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
int main(){
	int n,a,b,minl=0;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> b >> a;
		if(a!=1){
			a--;
			while(a>0){
				a = a/4;
				b++;
			}
			minl = max(minl,b);
		}
		else{
			minl = max(minl,a);
		}
	}
	cout << minl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
int main(){
	int n,a,b,minl=0;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> b >> a;
		while(a>0){
			if(a==4)
				b--;
			a = a/4;
			b++;
		}
		minl = max(minl,b);
	}
	cout << minl;
	return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
int main(){
	long long n,a,b,minl=0,x;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> b >> a;
		x = b;
		while((long long)pow((double)4,(double)(x-b))<a){
			x++;
		}
		if(x==b)
			x++;
		minl = max(minl,x);
	}
	cout << minl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		k=v[i].first;
		a=v[i].second;
		while(a>v[i+1].second&&k!=v[i+1].first)
		{
			if(a%4==0)
			a/=4;
			else
			a=a/4+1;
			k++;
		}
		if(k==v[i+1].first)
		v[i+1].second=max(v[i+1].second,a);
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>4)
	{
		if(a%4==0)
		a/=4;
		else
		a=a/4+1;
		k++;
	}
	cout<<k+1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		k=v[i].first;
		a=v[i].second;
		while(k!=v[i+1].first)
		{
			if(a%4==0)
			a/=4;
			else
			a=a/4+1;
			k++;
		}
		v[i+1].second=max(v[i+1].second,a);
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>4)
	{
		if(a%4==0)
		a/=4;
		else
		a=a/4+1;
		k++;
	}
	cout<<k+1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		v[i].second=v[i].second-min(v[i].second,4*v[i+1].second);
		(v[i+1].second)+=(v[i].second)/4;
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>1)
	{
		if(a%4==0)
		a=a/4;
		else
		a=a/4+1;
		k++;
	}
	cout<<k<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		v[i].second=v[i].second-min(v[i].second,4*v[i+1].second);
		(v[i+1].second)+=(v[i].second)/4;
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>4)
	{
		a/=4;
		k++;
	}
	cout<<k+1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		k=v[i].first;
		a=v[i].second;
		while(a>4&&k!=v[i+1].first)
		{
			if(a%4==0)
			a/=4;
			else
			a=a/4+1;
			k++;
		}
		if(k==v[i+1].first)
		{
			v[i+1].second+=a;
		}
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>4)
	{
		if(a%4==0)
		a/=4;
		else
		a=a/4+1;
		k++;
	}
	cout<<k+1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		v[i].second=v[i].second-min(v[i].second,4*v[i+1].second);
		(v[i+1].second)+=(v[i].second)/4;
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>4)
	{
		if(a%4==0)
		a/=4;
		else
		a=a/4+1;
		k++;
	}
	cout<<k+1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		v[i].second=v[i].second-min(v[i].second,4*v[i+1].second);
		(v[i+1].second)+=(v[i].second)/4;
		if(v[i].second%4!=0)
		(v[i+1].second)++;
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>4)
	{
		if(a%4==0)
		a/=4;
		else
		a=a/4+1;
		k++;
	}
	cout<<k+1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		v[i].second=v[i].second-min(v[i].second,4*v[i+1].second);
		(v[i+1].second)+=(v[i].second)/4;
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>4)
	{
		a=a/4;
		k++;
	}
	cout<<k+1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define MOD 1000000007

using namespace std;

int main()
{
	vector<pair<ll,ll> > v;
	ll n,k,a;
	cin>>n;
	for(ll i=0;i<n;++i)
	{
		cin>>k>>a;
		v.pb(pair<ll,ll> (k,a));
	}
	sort(v.begin(),v.end());
	for(ll i=0;i<v.size()-1;++i)
	{
		(v[i+1].second)+=(v[i].second)/4;
		v[i].second=(v[i].second)%4;
	}
	k=v[n-1].first;
	a=v[n-1].second;
	while(a>=4)
	{
		a=a/4;
		k++;
	}
	cout<<k<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,start,lim) for(lld i=start;i<lim;i++)
#define repd(i,start,lim) for(lld i=start;i>=lim;i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sz(a) lld((a).size())
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define MOD         1000000007
#define PI          3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
lld ctl(char x,char an='a') {return (lld)(x-an);}
char ltc(lld x,char an='a') {return (char)(x+an);}
#define bit(x,j) ((x>>j)&1)
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mxm(a,b) a = max(a,b)
#define mnm(a,b) a = min(a,b)
#define N 1000005

int main()
{
	lld n;
	cin >> n;
	lld min_length = 0;
	rep(i, 0, n)
	{
		lld square, count_sim;
		cin >> square >> count_sim;
		lld sq = sqrt(count_sim);
		lld length_one = powm(2, square);
		lld length_cur = sq * length_one;
		if (sq * sq != count_sim)
			length_cur += length_one;
		if (length_cur > min_length)
		{
			min_length = length_cur;
			if (count_sim == 1)
				min_length++;
		}
	}

	cout << ceil(log2(min_length)) << endl;
}


#include <bits/stdc++.h>
#define rep(i,start,lim) for(lld i=start;i<lim;i++)
#define repd(i,start,lim) for(lld i=start;i>=lim;i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sz(a) lld((a).size())
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define MOD         1000000007
#define PI          3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
lld ctl(char x,char an='a') {return (lld)(x-an);}
char ltc(lld x,char an='a') {return (char)(x+an);}
#define bit(x,j) ((x>>j)&1)
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mxm(a,b) a = max(a,b)
#define mnm(a,b) a = min(a,b)
#define N 1000005

int main()
{
	lld n;
	cin >> n;
	lld min_length = 0;
	rep(i, 0, n)
	{
		lld square, count_sim;
		cin >> square >> count_sim;
		lld sq = sqrt(count_sim);
		lld length_one = powm(2, square);
		lld length_cur = sq * length_one;
		if (sq * sq != count_sim)
			length_cur += length_one;
		if (length_cur > min_length)
		{
			min_length = length_cur;
	//		if (count_sim == 1)
	//			min_length++;
		}
	}

	cout << ceil(log2(min_length)) << endl;
}


#include <bits/stdc++.h>
#define rep(i,start,lim) for(lld i=start;i<lim;i++)
#define repd(i,start,lim) for(lld i=start;i>=lim;i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sz(a) lld((a).size())
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define MOD         1000000007
#define PI          3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
lld ctl(char x,char an='a') {return (lld)(x-an);}
char ltc(lld x,char an='a') {return (char)(x+an);}
#define bit(x,j) ((x>>j)&1)
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mxm(a,b) a = max(a,b)
#define mnm(a,b) a = min(a,b)
#define N 1000005

int main()
{
	lld n;
	cin >> n;
	lld min_length = 0;
	rep(i, 0, n)
	{
		lld square, count_sim;
		cin >> square >> count_sim;
		lld sq = ceil(sqrt(count_sim));
		lld length_cur = square + ceil(log2(sq));
		if (length_cur >= min_length)
		{
			min_length = length_cur;
			if (count_sim == 1)
				min_length++;
		}
	}

	cout << min_length << endl;
}


#include <bits/stdc++.h>
#define rep(i,start,lim) for(lld i=start;i<lim;i++)
#define repd(i,start,lim) for(lld i=start;i>=lim;i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sz(a) lld((a).size())
#define YES printf("YES\n")
#define NO printf("NO\n")
#define all(c) (c).begin(),(c).end()
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define MOD         1000000007
#define PI          3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
lld powm(lld base,lld exp,lld mod=MOD) {lld ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
lld ctl(char x,char an='a') {return (lld)(x-an);}
char ltc(lld x,char an='a') {return (char)(x+an);}
#define bit(x,j) ((x>>j)&1)
typedef vector<lld> vlld;
typedef pair<lld,lld> plld;
typedef map<lld,lld> mlld;
typedef set<lld> slld;
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mxm(a,b) a = max(a,b)
#define mnm(a,b) a = min(a,b)
#define N 1000005

int main()
{
	lld n;
	cin >> n;
	lld min_length = 0;
	rep(i, 0, n)
	{
		lld square, count_sim;
		cin >> square >> count_sim;
		lld sq = ceil(sqrt(count_sim));
		lld length_cur = square + ceil(log2(sq));
		if (length_cur > min_length)
		{
			min_length = length_cur;
			if (count_sim == 1)
				min_length++;
		}
	}

	cout << min_length << endl;
}


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

int main ()
{
    int n;
    cin >> n;
    int x, y;
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        cin >> x >> y;
        if(y == 1) x++;
        while (y > 1)
        {
            y = ceil(y / 4.0);
            x++;
        }
        ans = max(x, ans);
    }
    cout << ans << endl;
}


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

int main ()
{
    int n;
    cin >> n;
    int x, y;
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        cin >> x >> y;
        while (y > 1)
        {
            y = ceil(y / 4.0);
            x++;
        }
        ans = max(x, ans);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

int n,i,j;
pair <int, int> a[100005];

int main(){
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	scanf("%d%d", &a[i].first, &a[i].second);

	sort(a, a + n);

	long long res = 0;

	for(i = 0; i < n; i++){
		long long lv = (long long)ceil(log(a[i].second) / log(4));
		res = max(res, lv + a[i].first);
	}

	printf("%lld\n", res);
}


#include <bits/stdc++.h>

using namespace std;

int n,i,j;
pair <int, int> a[100005];

int main(){
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	scanf("%d%d", &a[i].first, &a[i].second);

	sort(a, a + n);

	long long res = 0;

	for(i = 0; i < n; i++){
		long long lv = max(1LL, (long long)ceil(log(a[i].second) / log(4)));
		res = max(res, lv + a[i].first);
	}

	printf("%lld\n", res);
}


#include <string>
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define vi vector < int >
#define mod 1000000007
#define matrix vector < vector < ll > >
typedef long long ll ;
using namespace std;
typedef long long ll ;
using namespace std;
int main() {
	int n ;
	ll ans = 0 , k , a;
	cin >> n  ;
	for(int i = 0 ; i < n ; i++)
	{
		cin >> k >> a ;
		ll dec = 4 ;
		int aa = a ;
		while(a > 0)
		{
			a= aa - dec ;
			dec *= 4 ;
			k++ ;
		}
		ans = max(ans , k) ;
	}
	cout << ans ;

}


#include <string>
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define vi vector < int >
#define mod 1000000007
#define matrix vector < vector < ll > >
typedef long long ll ;
using namespace std;
typedef long long ll ;
using namespace std;
int main() {
	int n ;
	ll ans = 0 , k , a;
	cin >> n  ;
	for(int i = 0 ; i < n ; i++)
	{
		cin >> k >> a ;
		ll dec = 4 ;
		k ;
		while(a > 0)
		{
			a-=dec ;
			dec *= 4 ;
			k++ ;
		}
		ans = max(ans , k) ;
	}
	cout << ans ;

}


#include <iostream>
#include <algorithm>
using namespace std;

struct Box
{
	int size;
	int n;
};

bool comp(Box a, Box b)
{
	return a.size > b.size;
}

int main()
{
	int N;
	Box b[100000];

	cin >> N;
	for(int i = 0; i < N; ++i)
	{
		cin >> b[i].size >> b[i].n;
	}

	sort(b, b + N, comp);

	int ns = b[0].size;
	long long x = 1, mb = 1e9, max = 0;
	for(int i = 0; i < N && x <= mb; ++i)
	{
		while(ns > b[i].size && x <= mb)
		{
			x *= 4;
			--ns;
		}

		if(x <= mb)
		{
			int t = b[i].n / x + (b[i].n % x == 0 ? 0 : 1);
			if(t > max)
			{
				max = t;
			}
		}
	}

	int ans = b[0].size;
	while(max > 4)
	{
		max = max / 4 + (max % 4 == 0 ? 0 : 1);
		++ans;
	}

	cout << ans + 1 << endl;

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

struct Box
{
	int size;
	int n;
};

bool comp(Box a, Box b)
{
	return a.size > b.size;
}

int main()
{
	int N;
	Box b[100000];

	cin >> N;
	for(int i = 0; i < N; ++i)
	{
		cin >> b[i].size >> b[i].n;
	}

	sort(b, b + N, comp);

	int ns = b[0].size;
	long long x = 1, mb = 1e9, max = 0;
	for(int i = 0; i < N && x <= mb; ++i)
	{
		while(ns > b[i].size && x <= mb)
		{
			x *= 4;
			--ns;
		}

		if(x <= mb)
		{
			int t = b[i].n / x + (b[i].n % x == 0 ? 0 : 1);
			if(t > max)
			{
				max = t;
			}
		}
	}

	int ans = b[0].size;
	while(max != 1)
	{
		max = max / 4 + (max % 4 == 0 ? 0 : 1);
		++ans;
	}

	cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
	cin>>n;
	int p=0,m=0;
	for(int i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		m=max(m,a);
		int t=1,k=0;
		while(t<b)
		{
			t*=4;
			k++;
		}
		p=max(p,a+k);
	}
	//cout<<p<<" "<<m<<endl;
	if(p==m) p++;
	cout<<p<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
	cin>>n;
	int p=0,m=0;
	for(int i=1;i<=n;i++)
	{
		int a,b;
		m=max(m,a);
		cin>>a>>b;
		int t=1,k=0;
		while(t<b)
		{
			t*=4;
			k++;
		}
		p=max(p,a+k);
	}
	if(p==m) p++;
	cout<<p<<endl;
}


#include<bits/stdc++.h>
using namespace std;
long long ans,n;
long long madeans(long long x){
	long long a=0;
	x=sqrt(x-1)+1;
	while(pow(2,a)<x)a++;
	return max(a,1ll);
}
int main(){
	cin>>n;
	while(n--){
		long long x,y;
		cin>>x>>y;
		ans=max(ans,x+madeans(y));
	}
	cout<<ans;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#define ri register
#define inf 0x7fffffff
#define E (1)
#define mk make_pair
#define int long long
using namespace std; const int N=400010;
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') w=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w;
}
void print(int x) {if(x<0) x=-x, putchar('-'); if(x>9) print(x/10); putchar(x%10+'0'); }
signed main()
{
	int mx=0; int n=read();
	for(ri int i=1;i<=n;i++)
	{
		int k,a;
		k=read(), a=read();
		int mr=ceil(log2(a)/2);
		mx=max(mx,k+max(mr,1ll));
	}
	printf("%lld\n",mx);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> mp;
map<int,int> has;
int res;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , cnt = in();
        has[k] |= bool(cnt);
        while(cnt)
        {
            mp[k] += cnt%4;
            cnt /= 4;
            k ++;
        }
    }
    int tot = 0;
    for (auto it : mp){
        res = max(res,it.first) ;
        tot +=  (1LL << it.first)*(1LL << it.first)*it.second;
    }
    if (has[res] || tot > (1LL << res)*(1L<<res)) res ++;
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> cnt;
map<int,int> has;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , a = in();
        cnt[k] += a;
        has[k] = true;
    }
    int res = 0;
    bool pre = 0;
    for (auto x : cnt)
    {
        if (x.second >= 4)
            cnt[x.first + 1] = max(cnt[x.first + 1] ,x.second/4);
        x.second %= 4;
        if (x.second){
            if (x.second > 1 || pre)
                res = max(res,x.first + 1);
            else res = max(res,x.first );
        }
        pre |= x.second;
    }
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> mp;
map<int,int> has;
int res;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , cnt = in();
        has[k] |= bool(cnt);
        while(cnt)
        {
            mp[k] += cnt%4;
            cnt /= 4;
            k ++;
        }
    }
    int tot = 0;
    for (auto it : mp){
        res = max(res,it.first) ;
        tot +=  (1LL << (it.first*2))*it.second;
    }
    if (has[res] || tot > (1LL << (res*2)) * 2) res ++;
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> cnt;
map<int,int> has;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , a = in();
        cnt[k] += a;
        has[k] = true;
    }
    int res = 0;
    int tot = 0 ;
    for (auto x : cnt)
    {
        if (x.second >= 4)
            cnt[x.first + 1] = max(cnt[x.first + 1] ,x.second/4);
        x.second %= 4;
        if (x.second)
            res = max(res,x.first);
        tot += (1LL << (2 * x.first))*x.second;
    }
    if (has[res] || tot > (1LL << (2 * res))) res ++;
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
int32_t main()
{
    int n = in();
    int res = 0;
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , a = in();
        int x = 1 , cc = 0;
        while(x < a)
            x *= 4 , cc ++;
        res = max(res,max(cc+k,k+1));

    }
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> cnt;
map<int,int> has;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , a = in();
        cnt[k] += a;
        has[k] = true;
    }
    int res = 0;
    bool pre = 0;
    for (auto x : cnt)
    {
        if (x.second >= 4)
            cnt[x.first + 1] = max(cnt[x.first + 1] ,x.second/4);
        x.second %= 4;
        if (x.second){
            if (x.second > 1 || pre || has[x.first])
                res = max(res,x.first + 1);
            else res = max(res,x.first );
        }
        pre |= x.second;
    }
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> mp;
map<int,int> has;
int res;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , cnt = in();
        has[k] |= bool(cnt);
        while(cnt)
        {
            mp[k] = max(mp[k],cnt%4);
            cnt -= mp[k];
            cnt = max(cnt,0ll);
            cnt/=4;
            k ++;
        }
    }
    int tot = 0;
    for (auto it : mp){
        res = max(res,it.first);
        tot +=  (1LL << (it.first*2))*it.second;
    }

    if (has[res] || tot > (1LL << (res*2))) res ++;
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> mp;
int res;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , cnt = in();
        while(cnt)
        {
            mp[k] += cnt%4;
            cnt /= 4;
            k ++;
        }
    }
    int tot = 0;
    for (auto it : mp)  res = max(res,it.first) , tot += it.second;
    if (tot > 1) res ++;
    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10;
map<int,int> mp;
map<int,int> has;
int res;
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i ++)
    {
        int k = in() , cnt = in();
        has[k] |= bool(cnt);
        while(cnt)
        {
            mp[k] = max(mp[k],cnt%4);
            cnt/=4;
            k ++;
        }
    }
    int tot = 0;
    for (auto it : mp){
        res = max(res,it.first);
        tot +=  (1LL << (it.first*2))*it.second;
    }

    if (has[res] || tot > (1LL << (res*2))) res ++;
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int val[1000001],base[100001],num[100001],maxx,n,b,a;
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>base[i]>>num[i];
	for(int i=1;i<=n;i++)
	num[i+1]=max((double)num[i+1],ceil((double)num[i]/pow(4,base[i+1]-base[i])));
	long double ans=ceil(log2(num[n])/2 );
	if(ans==0)ans=1;
	ans+=base[n];
	printf("%I64d",(int)ans);
}
/*
����Ӳװ����С����װ��
ֻ�浱ǰ��С��Ҫ��������֮���׽�ȥ������ν��һ·����Ҫ����
�㵽��������Ҫ����֮��
*/


#include<bits/stdc++.h>
using namespace std;
int val[1000001],base[100001],num[100001],maxx,n,b,a;
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>base[i]>>num[i];
	for(int i=1;i<=n;i++)
		num[i+1]=max((double)num[i+1],ceil((double)num[i]/pow(4,base[i+1]-base[i])));
	double ans=ceil(log2(num[n])/2 );
	cout<<base[n]+ans;
}


#include<bits/stdc++.h>
//    I'm Klaus, Nicklaus
using namespace std;
#define ios ios_base::sync_with_stdio(0),cin.tie(0);
#define ll long long
#define ld long double
#define ms(ar,val) memset(ar,val,sizeof ar)
const int nax = 5001;
int oo = 1e8;
string s, t;
int n, m;
int dp[nax][nax][2][2];
int go(int i, int j, int f1, int f2){
	if( i == n || j == m ) return 0;
	int& ans = dp[i][j][f1][f2];
	if( ans != -1 ) return ans;
	ans = -oo;
	if(f1==2 && f2==2) return 0;
	if(f1 == 0 || f2 == 0){
		if(f1 == 0 && i+1<=n)ans = max(ans, go(i+1,j,1,f2));
		if(f2 == 0 && j+1<=m)ans = max(ans, go(i,j+1,f1,1));
	}
	if(f1 == 1 && f2 == 1){
		if(s[i] == t[j] ){
			ans = max(ans,go(i+1,j+1,1,1)+2);
			ans = max(ans,go(i+1,j+1,2,2)+2);
		}
		if(i+1<=n)ans = max(ans,go(i+1,j,1,1)-1);
		if(j+1<=m)ans = max(ans,go(i,j+1,1,1)-1);
	}
	return ans;
}
int main(){
	ios;
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	ms(dp,-1);
	cin>>n>>m;
	cin>>s>>t;
	int ans = go(0,0,0,0);
	ans = max(ans,go(0,0,0,1));
	ans = max(ans, go(0,0,1,0));
	cout<<max(ans,0)<<endl;
	return 0;
}


#include<bits/stdc++.h>
//    I'm Klaus, Nicklaus
using namespace std;
#define ios ios_base::sync_with_stdio(0),cin.tie(0);
#define ll long long
#define ld long double
#define ms(ar,val) memset(ar,val,sizeof ar)
const int nax = 5003;
int oo = 1e9;
string s, t;
int n, m;
int dp[nax][nax][2][2];
int go(int i, int j, int f1, int f2){
	if( i == n || j == m ) return 0;
	int& ans = dp[i][j][f1][f2];
	if( ans != -1 ) return ans;
	ans = -oo;
	if(f1==2 && f2==2) return 0;
	if(f1 == 0 || f2 == 0){
		if(f1 == 0 && i+1<=n)ans = max(ans, go(i+1,j,1,f2));
		if(f2 == 0 && j+1<=m)ans = max(ans, go(i,j+1,f1,1));
	}
	if(f1 == 1 && f2 == 1){
		if(s[i] == t[j] ){
			ans = max(ans,go(i+1,j+1,1,1)+2);
			ans = max(ans,go(i+1,j+1,2,2)+2);
		}
		if(i+1<=n)ans = max(ans,go(i+1,j,1,1)-1);
		if(j+1<=m)ans = max(ans,go(i,j+1,1,1)-1);
	}
	return ans;
}
int main(){
	ios;
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	ms(dp,-1);
	cin>>n>>m;
	cin>>s>>t;
	int ans = go(0,0,0,0);
	ans = max(ans,go(0,0,0,1));
	ans = max(ans, go(0,0,1,0));
	cout<<max(ans,0)<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define vec vector
#define psb push_back
#define ppb pop_back
#define psf push_front
#define ppf pop_front
#define ins insert
#define fst first
#define scd second
using namespace std;
int main() {
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll n, m;
	cin >> n >> m;
	string a, b;
	cin >> a >> b;
	ll i,j,dp[n+1][m+1], ans = 0;
	for(i=0;i<=n;i++){
		for(j=0;j<=m;j++){
			if(i==0 || j==0) {
				dp[i][j] = 0;
				continue;
			}
			if(a[i-1]==b[j-1]){
				dp[i][j] = dp[i-1][j-1] + 2;
			}
			else{
				dp[i][j] = max(max(dp[i][j-1], dp[i-1][j]) - 1, 0ll);
			}
			ans = max(ans, dp[i][j]);
		}
	}
	cout << ans;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define vec vector
#define psb push_back
#define ppb pop_back
#define psf push_front
#define ppf pop_front
#define ins insert
#define fst first
#define scd second
using namespace std;
int main() {
	ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll n, m;
	cin >> n >> m;
	string a, b;
	cin >> a >> b;
	ll i,j,dp[n+1][m+1], ans = 0;
	for(i=0;i<=n;i++){
		for(j=0;j<=m;j++){
			if(i==0 || j==0) {
				dp[i][j] = 0;
				continue;
			}
			if(a[i-1]==b[j-1]){
				dp[i][j] = dp[i-1][j-1] + 2;
			}
			else{
				dp[i][j] = max(dp[i][j-1], dp[i-1][j]) - 1;
			}
			ans = max(ans, dp[i][j]);
		}
	}
	cout << ans;
	return 0;
}


/*input
8 10
bbbbabab
bbbabaaaaa

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

#define int long long
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)

#define all(v) v.begin(),v.end()
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

const int N = 5001;
int preva[N][26];
int prevb[N][26];
int dp[N][N];
string a,b;
int n,m;

void solve(){
  	cin >> n >> m;
  	cin >> a >> b;
  	a = "$"+a;
  	b = "$"+b;
  	RE(i,n){
  		REP(j,26)preva[i][j] = preva[i-1][j];
  		preva[i][a[i]-'a'] = i;
  	}
  	RE(i,m){
  		REP(j,26)prevb[i][j] = prevb[i-1][j];
  		prevb[i][b[i]-'a'] = i;
  	}
  	int ans = 0;
  	RE(i,n){
  		RE(j,m){
  			dp[i][j] = dp[i-1][j-1]-2;
  			// take i wala
  			int posforb = prevb[j][a[i]-'a'];
  			if(posforb > 0)dp[i][j] = max(dp[i][j],dp[i-1][posforb-1]+3-(j-posforb+1));
  			int posfora = preva[i][b[j]-'a'];
  			if(posfora > 0)dp[i][j] = max(dp[i][j],dp[posfora-1][j-1]+3-(i-posfora+1));
  			ans = max(ans,dp[i][j]);
  		}
  	}
  	cout << ans << endl;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}


/*input
7 7
uiibwws
qhtkxcn

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

#define int long long
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)

#define all(v) v.begin(),v.end()
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

const int N = 5001;
int preva[N][26];
int prevb[N][26];
int dp[N][N];
string a,b;
int n,m;

void solve(){
  	cin >> n >> m;
  	cin >> a >> b;
  	a = "$"+a;
  	b = "$"+b;
  	RE(i,n){
  		REP(j,26)preva[i][j] = preva[i-1][j];
  		preva[i][a[i]-'a'] = i;
  	}
  	RE(i,m){
  		REP(j,26)prevb[i][j] = prevb[i-1][j];
  		prevb[i][b[i]-'a'] = i;
  	}
  	REP(i,N){
  		REP(j,N){
  			dp[i][j] = -INF;
  		}
  	}
  	dp[0][0] = 0;
  	int ans = 0;
  	RE(i,n){
  		RE(j,m){
  			dp[i][j] = -2+4*(a[i] == b[j]);
  			dp[i][j] = max(dp[i][j],dp[i-1][j-1]-2);
  			dp[i][j] = max(dp[i][j-1]-1,dp[i][j]);
  			dp[i][j] = max(dp[i-1][j]-1,dp[i][j]);
  			// take i wala
  			int posforb = prevb[j][a[i]-'a'];
  			if(posforb > 0)dp[i][j] = max(dp[i][j],dp[i-1][posforb-1]+3-(j-posforb+1));
  			int posfora = preva[i][b[j]-'a'];
  			if(posfora > 0)dp[i][j] = max(dp[i][j],dp[posfora-1][j-1]+3-(i-posfora+1));
  			ans = max(ans,dp[i][j]);
  			//cout << i << " " << j << " " << dp[i][j] << endl;
  		}
  	}
  	cout << ans << endl;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}


/*input
4 5
abba
babab

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

#define int long long
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)

#define all(v) v.begin(),v.end()
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

const int N = 5001;
int preva[N][26];
int prevb[N][26];
int dp[N][N];
string a,b;
int n,m;

void solve(){
  	cin >> n >> m;
  	cin >> a >> b;
  	a = "$"+a;
  	b = "$"+b;
  	RE(i,n){
  		REP(j,26)preva[i][j] = preva[i-1][j];
  		preva[i][a[i]-'a'] = i;
  	}
  	RE(i,m){
  		REP(j,26)prevb[i][j] = prevb[i-1][j];
  		prevb[i][b[i]-'a'] = i;
  	}
  	int ans = 0;
  	RE(i,n){
  		RE(j,m){
  			dp[i][j] = dp[i-1][j-1];
  			// take i wala
  			int posforb = prevb[j][a[i]-'a'];
  			if(posforb > 0)dp[i][j] = max(dp[i][j],dp[i-1][posforb-1]+3-(j-posforb+1));
  			int posfora = preva[i][b[j]-'a'];
  			if(posfora > 0)dp[i][j] = max(dp[i][j],dp[posfora-1][j-1]+3-(i-posfora+1));
  			ans = max(ans,dp[i][j]);
  		}
  	}
  	cout << ans << endl;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}


/*input
8 10
bbbbabab
bbbabaaaaa

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

#define int long long
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)

#define all(v) v.begin(),v.end()
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

const int N = 5001;
int preva[N][26];
int prevb[N][26];
int dp[N][N];
string a,b;
int n,m;

void solve(){
  	cin >> n >> m;
  	cin >> a >> b;
  	a = "$"+a;
  	b = "$"+b;
  	RE(i,n){
  		REP(j,26)preva[i][j] = preva[i-1][j];
  		preva[i][a[i]-'a'] = i;
  	}
  	RE(i,m){
  		REP(j,26)prevb[i][j] = prevb[i-1][j];
  		prevb[i][b[i]-'a'] = i;
  	}
  	int ans = 0;
  	RE(i,n){
  		RE(j,m){
  			dp[i][j] = dp[i-1][j-1]-2;
  			dp[i][j] = max(dp[i][j-1]-1,dp[i][j]);
  			dp[i][j] = max(dp[i-1][j]-1,dp[i][j]);
  			// take i wala
  			int posforb = prevb[j][a[i]-'a'];
  			if(posforb > 0)dp[i][j] = max(dp[i][j],dp[i-1][posforb-1]+3-(j-posforb+1));
  			int posfora = preva[i][b[j]-'a'];
  			if(posfora > 0)dp[i][j] = max(dp[i][j],dp[posfora-1][j-1]+3-(i-posfora+1));
  			ans = max(ans,dp[i][j]);
  		}
  	}
  	cout << ans << endl;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}


/*input
4 4
aaaa
aabb
*/
#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;

#define int long long
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i,n) for (int i = 1; i <= n; i++)
#define REP(i,n) for (int i = 0; i < (int)n; i++)

#define all(v) v.begin(),v.end()
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int raise(int a,int n,int m = MOD){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= raise(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

const int N = 5001;
int preva[N][26];
int prevb[N][26];
int dp[N][N];
string a,b;
int n,m;

void solve(){
  	cin >> n >> m;
  	cin >> a >> b;
  	a = "$"+a;
  	b = "$"+b;
  	RE(i,n){
  		REP(j,26)preva[i][j] = preva[i-1][j];
  		preva[i][a[i]-'a'] = i;
  	}
  	RE(i,m){
  		REP(j,26)prevb[i][j] = prevb[i-1][j];
  		prevb[i][b[i]-'a'] = i;
  	}
  	REP(i,N){
  		REP(j,N){
  			dp[i][j] = -INF;
  		}
  	}
  	dp[0][0] = 0;
  	int ans = 0;
  	RE(i,n){
  		RE(j,m){
  			dp[i][j] = dp[i-1][j-1]-2;
  			dp[i][j] = max(dp[i][j-1]-1,dp[i][j]);
  			dp[i][j] = max(dp[i-1][j]-1,dp[i][j]);
  			// take i wala
  			int posforb = prevb[j][a[i]-'a'];
  			if(posforb > 0)dp[i][j] = max(dp[i][j],dp[i-1][posforb-1]+3-(j-posforb+1));
  			int posfora = preva[i][b[j]-'a'];
  			if(posfora > 0)dp[i][j] = max(dp[i][j],dp[posfora-1][j-1]+3-(i-posfora+1));
  			ans = max(ans,dp[i][j]);
  		}
  	}
  	cout << ans << endl;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;
  //cin >> t;
  while(t--){
    solve();
  }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
// #define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=5005;

int n,m;
string s,t;
int dp[N][N][2];

int calc(int x,int y,int start){
	if(x == n || y == m){
		return 0;
	}
	int &ans = dp[x][y][start];
	if(ans != -1) return ans;
	ans = 0;
	if(s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
	amax(ans,calc(x,y+1,start)-start);
	amax(ans,calc(x+1,y,start)-start);
	return ans;
}

void solve(){
	mem1(dp);
	cin >> n >> m >> s >> t;
	assert(n < N && m < N);
	cout << calc(0,0,0);
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
// #define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=5005;

int n,m;
string s,t;
int dp[N][N][2];

int calc(int x,int y,int start){
	if(x == n || y == m){
		return 0;
	}
	int &ans = dp[x][y][start];
	if(ans != -1) return ans;
	ans = 0;
	if(!start && s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
	if(!start) amax(ans,calc(x,y+1,0)),amax(ans,calc(x+1,y,0));
	if(start){
		if(s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
		amax(ans,calc(x,y+1,0)-1);
		amax(ans,calc(x+1,y,0)-1);
	}
	return ans;
}

void solve(){
	mem1(dp);
	cin >> n >> m >> s >> t;
	cout << calc(0,0,0);
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
// #define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=5005;

int n,m;
string s,t;
int dp[N][N][2];

int calc(int x,int y,int start){
	if(x == n || y == m){
		return 0;
	}
	int &ans = dp[x][y][start];
	if(ans != -1) return ans;
	ans = 0;
	if(s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
	amax(ans,calc(x,y+1,start)-start);
	amax(ans,calc(x+1,y,start)-start);
	return ans;
}

void solve(){
	mem1(dp);
	cin >> n >> m >> s >> t;
	cout << calc(0,0,0);
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
// #define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=5005;

int n,m;
string s,t;
int dp[N][N][2];

int calc(int x,int y,int start){
	if(x == n || y == m){
		return 0;
	}
	int &ans = dp[x][y][start];
	if(ans != -1) return ans;
	ans = 0;
	if(!start && s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
	if(!start) amax(ans,calc(x,y+1,0)),amax(ans,calc(x+1,y,0));
	if(start){
		if(s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
		amax(ans,calc(x,y+1,1)-1);
		amax(ans,calc(x+1,y,1)-1);
	}
	return ans;
}

void solve(){
	mem1(dp);
	cin >> n >> m >> s >> t;
	cout << calc(0,0,0);
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=5005;

int n,m;
string s,t;
int dp[N][N][2];

int calc(int x,int y,int start){
	if(x == n || y == m){
		return 0;
	}
	int &ans = dp[x][y][start];
	if(ans != -1) return ans;
	ans = 0;
	if(!start && s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
	if(!start) amax(ans,calc(x,y+1,0)),amax(ans,calc(x+1,y,0));
	if(start){
		if(s[x] == t[y]) amax(ans,calc(x+1,y+1,1)+2);
		amax(ans,calc(x,y+1,0)-1);
		amax(ans,calc(x+1,y,0)-1);
	}
	return ans;
}

void solve(){
	mem1(dp);
	cin >> n >> m >> s >> t;
	cout << calc(0,0,0);
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
// #define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=5005;

int n,m;
string s,t;
int dp[N][N];
int fans = 0;

int calc(int x,int y){
	if(x == n || y == m){
		return 0;
	}
	int &ans = dp[x][y];
	if(ans != -1) return ans;
	ans = 0;
	if(s[x] == t[y]) amax(ans,calc(x+1,y+1)+2);
	amax(ans,calc(x,y+1)-1);
	amax(ans,calc(x+1,y)-1);
	amax(fans,ans);
	return ans;
}

void solve(){
	mem1(dp);
	cin >> n >> m >> s >> t;
	calc(0,0);
	cout << fans;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
typedef long long ll;
typedef pair<ll,ll> ii;
typedef pair<ll,ii> iii;
int n,m;
char a[5005], b[5005];
int dp[5005][5005];

int main()
{
    cin>>n>>m;
    for(int i = 1; i <= n ;i++) cin>>a[i];
    for(int j = 1; j <= m; j++) cin>>b[j];
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(a[i] == b[j]) dp[i][j] = max(2, dp[i-1][j-1] + 2);
            else {
                dp[i][j] = max(dp[i][j], max(dp[i][j-1], dp[i-1][j]) - 1);
            }
            ans = max(ans, dp[i][j]);
        }

    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mem(a, b) memset(a, (b), sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define mod1 1000000007
#define mod2 998244353
#define ll long long int
#define ld long double
#define inf LLONG_MAX
#define endl "\n"
#define F first
#define S second
#define inp(a) int a;cin>>a;
#define inpa(a,n)vector<int> a(n);for(int i = 0; i<n; i++)cin>>a[i];
const double pi=acos(-1.0);
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; //greater<int> can also be used
#define printclock cerr<<"Running Time is: "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define sz(c) (int)c.size()
#define d1(x)            cout<<#x<<"="<<x<<endl;
#define d2(x,y)          cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define d3(x,y,z)        cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
#define d4(x,y,z,w)      cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#w <<"="<<w<<endl;
#define da(arr,j,n)  for(int i=j;i<n;i++) {cout<<"i="<<i<<" arr[i]="<<arr[i]<<"\n";}
#define debug(x)         cout<<"debug"<<x<<" "<<endl;
const  ll MODA =                         1000000007;
// const  int MODA =                     998244353 ;
vector<ll> primefactors    ;            // used by generatePrimeFactors()                                                                                // ARRAYS/VECTORS
vector<ll> factors         ;            // used by generateFactors()
ll   fact[100005]={}         ;            // used by generateFactorial() & ncr()
bool isPowerTwo(ll x)                   { return (x && !(x & (x - 1))); }                                                                                // FUNCTIONS
ll   modmul(ll a,ll b)                  { return((a%MODA)*(b%MODA))%MODA;     }
ll   modadd(ll a,ll b)                  { return((a%MODA)+(b%MODA)+MODA)%MODA;}
ll   modsub(ll a,ll b)                  { return((a%MODA)-(b%MODA)+MODA)%MODA;}
bool isSubstring(string s1, string s2)  { if (s1.find(s2) != string::npos) return true; else return false; }
void generateFactorial(ll n)            { fact[0] = 1; for(ll i = 1; i <= n; i++)   fact[i] = (i* 1ll * fact[i - 1]) ; }
void generateFactorialMOD(ll n)         { fact[0] = 1; for(ll i = 1; i <= n; i++)   fact[i] = (i* 1ll * fact[i - 1]) % MODA; }
bool isPrime(ll n)                      { if(n<2) return false; for(ll i=2;i*i<=n;i++) if(n%i==0) return false; return true; } //O(sqrt(n))
ll   power(ll a, ll b)                  {   ll res=1; while(b) {    if(b&1) res=(res*a); a=(a*a);   b>>=1;  }   return res; }
ll   powermod(ll a, ll b)               {   ll res=1; while(b) {    if(b&1) res=(res*a)%MODA; a=(a*a)%MODA;     b>>=1;  }   return res; }
ll   modi (ll a)                        { ll m=MODA,s=1,p=0;  while(a>1)  {  ll q=a/m,t=m;  m=a%m; a=t; t=p; p=s-q*p; s=t;  } return s>=0 ? s :s+MODA; }
void generatePrimeFactors(ll n)         { primefactors.clear(); for(ll i=2;i*i<=n;i++) { if(n%i==0) { primefactors.pb(i); while(n%i==0)  n=n/i; } } if(n!=1)   primefactors.pb(n); }
ll   ncr(ll n, ll r)                    { if(r==0)return 1;  fact[0]=1; for(int i=1 ; i<=n; i++) fact[i] = fact[i-1]*i%MODA;   return (fact[n]* modi(fact[r]) % MODA *  modi(fact[n-r]) % MODA) % MODA; }
void generateFactors(ll n)              { factors.clear(); for(ll i=2;i*i<=n;i++){ if(n%i==0) { factors.pb(i); if(n/i!=i)factors.pb(n/i); }} factors.pb(1); factors.pb(n); sort(factors.begin(),factors.end()); }
ld   Logn(ld n, ld r)                   { return log(n) / log(r); }

///////////////////////////////////
const int Nmax = 1000005;

int n,m,f[5005][5005],ans;

////////////////////////////////////

void solve(){

        cin >> n >> m;

        string s1, s2;
        cin >> s1 >> s2;
        s1 = "#" + s1;
        s2 = "#" + s2;
        vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
        for(int  i = 1; i<= n; i++){
            for(int j = 1; j<=m; j++){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) - 1;
                if(s1[i] == s2[j]){
                    dp[i][j] = dp[i-1][j-1]+2;

                }
                ans = max(ans, dp[i][j]);
            }
        }

        cout << ans << endl;
    }

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

      int t= 1;
    //  cin >> t;

        while(t--){
            solve();
        }

        printclock;

  }

/////////////////////////////
//try to bring as much symmetry as possible
//just check for integer overflow
//add MOD value when you substract two numbers
//clear globally declared vectors after each test case
// don't return or break while taking input of particular test case
// using brackets when using (XOR, AND, OR) when calculating boolean value
/////////////////////////////


#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mem(a, b) memset(a, (b), sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define mod1 1000000007
#define mod2 998244353
#define ll long long int
#define ld long double
#define inf LLONG_MAX
#define endl "\n"
#define F first
#define S second
#define inp(a) int a;cin>>a;
#define inpa(a,n)vector<int> a(n);for(int i = 0; i<n; i++)cin>>a[i];
const double pi=acos(-1.0);
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; //greater<int> can also be used
#define printclock cerr<<"Running Time is: "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define sz(c) (int)c.size()
#define d1(x)            cout<<#x<<"="<<x<<endl;
#define d2(x,y)          cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define d3(x,y,z)        cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
#define d4(x,y,z,w)      cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#w <<"="<<w<<endl;
#define da(arr,j,n)  for(int i=j;i<n;i++) {cout<<"i="<<i<<" arr[i]="<<arr[i]<<"\n";}
#define debug(x)         cout<<"debug"<<x<<" "<<endl;
const  ll MODA =                         1000000007;
// const  int MODA =                     998244353 ;
vector<ll> primefactors    ;            // used by generatePrimeFactors()                                                                                // ARRAYS/VECTORS
vector<ll> factors         ;            // used by generateFactors()
ll   fact[100005]={}         ;            // used by generateFactorial() & ncr()
bool isPowerTwo(ll x)                   { return (x && !(x & (x - 1))); }                                                                                // FUNCTIONS
ll   modmul(ll a,ll b)                  { return((a%MODA)*(b%MODA))%MODA;     }
ll   modadd(ll a,ll b)                  { return((a%MODA)+(b%MODA)+MODA)%MODA;}
ll   modsub(ll a,ll b)                  { return((a%MODA)-(b%MODA)+MODA)%MODA;}
bool isSubstring(string s1, string s2)  { if (s1.find(s2) != string::npos) return true; else return false; }
void generateFactorial(ll n)            { fact[0] = 1; for(ll i = 1; i <= n; i++)   fact[i] = (i* 1ll * fact[i - 1]) ; }
void generateFactorialMOD(ll n)         { fact[0] = 1; for(ll i = 1; i <= n; i++)   fact[i] = (i* 1ll * fact[i - 1]) % MODA; }
bool isPrime(ll n)                      { if(n<2) return false; for(ll i=2;i*i<=n;i++) if(n%i==0) return false; return true; } //O(sqrt(n))
ll   power(ll a, ll b)                  {   ll res=1; while(b) {    if(b&1) res=(res*a); a=(a*a);   b>>=1;  }   return res; }
ll   powermod(ll a, ll b)               {   ll res=1; while(b) {    if(b&1) res=(res*a)%MODA; a=(a*a)%MODA;     b>>=1;  }   return res; }
ll   modi (ll a)                        { ll m=MODA,s=1,p=0;  while(a>1)  {  ll q=a/m,t=m;  m=a%m; a=t; t=p; p=s-q*p; s=t;  } return s>=0 ? s :s+MODA; }
void generatePrimeFactors(ll n)         { primefactors.clear(); for(ll i=2;i*i<=n;i++) { if(n%i==0) { primefactors.pb(i); while(n%i==0)  n=n/i; } } if(n!=1)   primefactors.pb(n); }
ll   ncr(ll n, ll r)                    { if(r==0)return 1;  fact[0]=1; for(int i=1 ; i<=n; i++) fact[i] = fact[i-1]*i%MODA;   return (fact[n]* modi(fact[r]) % MODA *  modi(fact[n-r]) % MODA) % MODA; }
void generateFactors(ll n)              { factors.clear(); for(ll i=2;i*i<=n;i++){ if(n%i==0) { factors.pb(i); if(n/i!=i)factors.pb(n/i); }} factors.pb(1); factors.pb(n); sort(factors.begin(),factors.end()); }
ld   Logn(ld n, ld r)                   { return log(n) / log(r); }

///////////////////////////////////
const int Nmax = 1000005;

int n,m,f[5005][5005],ans;

////////////////////////////////////

void solve(){

        cin >> n >> m;

        string s1, s2;
        cin >> s1 >> s2;
        s1 = "#" + s1;
        s2 = "#" + s2;
        vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
        for(int  i = 1; i<= n; i++){
            for(int j = 1; j<=m; j++){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) - 1;
                if(dp[i][j] < 0){
                    dp[i][j] = 0;
                }
                if(s1[i] == s2[j]){
                    dp[i][j] = dp[i-1][j-1]+2;

                }
                ans = max(ans, dp[i][j]);
            }
        }

        cout << ans << endl;
    }

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

      int t= 1;
    //  cin >> t;

        while(t--){
            solve();
        }

        printclock;

  }

/////////////////////////////
//try to bring as much symmetry as possible
//just check for integer overflow
//add MOD value when you substract two numbers
//clear globally declared vectors after each test case
// don't return or break while taking input of particular test case
// using brackets when using (XOR, AND, OR) when calculating boolean value
/////////////////////////////


#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mem(a, b) memset(a, (b), sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define mod1 1000000007
#define mod2 998244353
#define ll long long int
#define ld long double
#define inf LLONG_MAX
#define endl "\n"
#define F first
#define S second
#define inp(a) int a;cin>>a;
#define inpa(a,n)vector<int> a(n);for(int i = 0; i<n; i++)cin>>a[i];
const double pi=acos(-1.0);
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; //greater<int> can also be used
#define printclock cerr<<"Running Time is: "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define sz(c) (int)c.size()
#define d1(x)            cout<<#x<<"="<<x<<endl;
#define d2(x,y)          cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define d3(x,y,z)        cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
#define d4(x,y,z,w)      cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<" "<<#w <<"="<<w<<endl;
#define da(arr,j,n)  for(int i=j;i<n;i++) {cout<<"i="<<i<<" arr[i]="<<arr[i]<<"\n";}
#define debug(x)         cout<<"debug"<<x<<" "<<endl;
const  ll MODA =                         1000000007;
// const  int MODA =                     998244353 ;
vector<ll> primefactors    ;            // used by generatePrimeFactors()                                                                                // ARRAYS/VECTORS
vector<ll> factors         ;            // used by generateFactors()
ll   fact[100005]={}         ;            // used by generateFactorial() & ncr()
bool isPowerTwo(ll x)                   { return (x && !(x & (x - 1))); }                                                                                // FUNCTIONS
ll   modmul(ll a,ll b)                  { return((a%MODA)*(b%MODA))%MODA;     }
ll   modadd(ll a,ll b)                  { return((a%MODA)+(b%MODA)+MODA)%MODA;}
ll   modsub(ll a,ll b)                  { return((a%MODA)-(b%MODA)+MODA)%MODA;}
bool isSubstring(string s1, string s2)  { if (s1.find(s2) != string::npos) return true; else return false; }
void generateFactorial(ll n)            { fact[0] = 1; for(ll i = 1; i <= n; i++)   fact[i] = (i* 1ll * fact[i - 1]) ; }
void generateFactorialMOD(ll n)         { fact[0] = 1; for(ll i = 1; i <= n; i++)   fact[i] = (i* 1ll * fact[i - 1]) % MODA; }
bool isPrime(ll n)                      { if(n<2) return false; for(ll i=2;i*i<=n;i++) if(n%i==0) return false; return true; } //O(sqrt(n))
ll   power(ll a, ll b)                  {   ll res=1; while(b) {    if(b&1) res=(res*a); a=(a*a);   b>>=1;  }   return res; }
ll   powermod(ll a, ll b)               {   ll res=1; while(b) {    if(b&1) res=(res*a)%MODA; a=(a*a)%MODA;     b>>=1;  }   return res; }
ll   modi (ll a)                        { ll m=MODA,s=1,p=0;  while(a>1)  {  ll q=a/m,t=m;  m=a%m; a=t; t=p; p=s-q*p; s=t;  } return s>=0 ? s :s+MODA; }
void generatePrimeFactors(ll n)         { primefactors.clear(); for(ll i=2;i*i<=n;i++) { if(n%i==0) { primefactors.pb(i); while(n%i==0)  n=n/i; } } if(n!=1)   primefactors.pb(n); }
ll   ncr(ll n, ll r)                    { if(r==0)return 1;  fact[0]=1; for(int i=1 ; i<=n; i++) fact[i] = fact[i-1]*i%MODA;   return (fact[n]* modi(fact[r]) % MODA *  modi(fact[n-r]) % MODA) % MODA; }
void generateFactors(ll n)              { factors.clear(); for(ll i=2;i*i<=n;i++){ if(n%i==0) { factors.pb(i); if(n/i!=i)factors.pb(n/i); }} factors.pb(1); factors.pb(n); sort(factors.begin(),factors.end()); }
ld   Logn(ld n, ld r)                   { return log(n) / log(r); }

///////////////////////////////////
const int Nmax = 1000005;

int n,m,f[5005][5005],ans;

////////////////////////////////////

void solve(){

        cin >> n >> m;

        string s1, s2;
        cin >> s1 >> s2;
        s1 = "#" + s1;
        s2 = "#" + s2;
        vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
        for(int  i = 1; i<= n; i++){
            for(int j = 1; j<=m; j++){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) - 1;
                if(s1[i] == s2[j]){
                    dp[i][j] = max(dp[i][j], dp[i-1][j-1]+2);

                }
                ans = max(ans, dp[i][j]);
            }
        }

        cout << ans << endl;
    }

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

      int t= 1;
    //  cin >> t;

        while(t--){
            solve();
        }

        printclock;

  }

/////////////////////////////
//try to bring as much symmetry as possible
//just check for integer overflow
//add MOD value when you substract two numbers
//clear globally declared vectors after each test case
// don't return or break while taking input of particular test case
// using brackets when using (XOR, AND, OR) when calculating boolean value
/////////////////////////////


//agrawal117
//chahatagrawal117
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 5005
int n,m;
int dp[MAX][MAX];
string a,b;
int solve(int i, int j)
{
    if(i==n|| j==m) return 0;
    if(dp[i][j]!=-1) return dp[i][j];

    int x=-1+solve(i+1,j);
    int y=-1+solve(i,j+1);
    int z=0;
    if(a[i]==b[j]) {
        z=2+solve(i+1,j+1);
    }
    return dp[i][j]=max({x,y,z});
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    cin>>a>>b;
    memset(dp,-1,sizeof(dp));
    int ans=0;
    for(int i=n-1;i>=0;i--)
    {
        for(int j=m-1;j>=0;j--)
        {
            ans=max(ans,solve(i,j));
        }
    }
    cout<<ans<<endl;
}


//agrawal117
//chahatagrawal117
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 5005
int n,m;
int dp[MAX][MAX];
string a,b;
int solve(int i, int j)
{
    if(i==n|| j==m) return 0;
    if(dp[i][j]!=-1) return dp[i][j];

    int x=-1+solve(i+1,j);
    int y=-1+solve(i,j+1);
    int z=0;
    if(a[i]==b[j]) {
        z=2+solve(i+1,j+1);
    }
    return dp[i][j]=max({x,y,z});
}


#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int dp[5001][5001];

int main() {
    int n, m;
    cin >> n >> m;
    string a, b;
    cin >> a >> b;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            dp[n][m] = 0;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i] == b[j]) {
                dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 2);
            } else {
                dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]) - 1;
            }
        }
    }
    int res = 0;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            res = max(res, dp[i][j]);
        }
    }
    cout << res << endl;
    return 0;
}


#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int dp[5001][5001];

int main() {
    int n, m;
    cin >> n >> m;
    string a, b;
    cin >> a >> b;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            dp[n][m] = 0;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i] == b[j]) {
                dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 2);
            } else {
                dp[i+1][j+1] = max(0, max(dp[i][j+1], dp[i+1][j]) - 1);
            }
        }
    }
    int res = 0;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            res = max(res, dp[i][j]);
        }
    }
    cout << res << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define fastio {ios_base::sync_with_stdio(false);cin.tie(NULL);}
#define mod 1000000007
#define N 5000
int n,m;
string s,t;
short int dp[N][N][2][2];
int solve(int i,int j,int o1,int o2){
    if(i==n || j==m) return 0;
    if(dp[i][j][o1][o2]!=-1) return dp[i][j][o1][o2];
    if(o1==1 && o2==1){
        int ans1=0,ans2=0,ans3=0;
        if(s[i]==t[j]) ans1=2+solve(i+1,j+1,o1,o2);
        ans2=solve(i+1,j,o1,o2)-1;
        ans3=solve(i,j+1,o1,o2)-1;
        return dp[i][j][o1][o2]=max(ans1,max(ans2,max(ans3,0)));
    }
    if(o1==0 && o2==1){
        int ans1=solve(i,j,1,1);
        int ans2=solve(i+1,j,0,1);
        return dp[i][j][o1][o2]=max(ans1,max(ans2,0));
    }
    if(o1==1 && o2==0){
        int ans1=solve(i,j,1,1);
        int ans2=solve(i,j+1,1,0);
        return dp[i][j][o1][o2]=max(ans1,max(ans2,0));
    }
    else{
        int ans1=solve(i,j,1,0);
        int ans2=solve(i,j,0,1);
        int ans3=solve(i+1,j+1,0,0);
        return dp[i][j][o1][o2]=max(ans1,max(ans2,max(ans3,0)));
    }
}
int main()
{
    fastio;
    cin>>n>>m>>s>>t;
    memset(dp,-1,sizeof dp);
    cout<< solve(0,0,0,0) <<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define fastio {ios_base::sync_with_stdio(false);cin.tie(NULL);}
#define mod 1000000007
#define N 5000
int n,m;
string s,t;
int dp[N][N][2][2];
int solve(int i,int j,int o1,int o2){
    if(i==n || j==m) return 0;
    if(dp[i][j][o1][o2]!=-1) return dp[i][j][o1][o2];
    if(o1==1 && o2==1){
        int ans1=0,ans2=0,ans3=0;
        if(s[i]==t[j]) ans1=2+solve(i+1,j+1,o1,o2);
        ans2=solve(i+1,j,o1,o2)-1;
        ans3=solve(i,j+1,o1,o2)-1;
        return dp[i][j][o1][o2]=max(ans1,max(ans2,max(ans3,0)));
    }
    if(o1==0 && o2==1){
        int ans1=solve(i,j,1,1);
        int ans2=solve(i+1,j,0,1);
        return dp[i][j][o1][o2]=max(ans1,max(ans2,0));
    }
    if(o1==1 && o2==0){
        int ans1=solve(i,j,1,1);
        int ans2=solve(i,j+1,1,0);
        return dp[i][j][o1][o2]=max(ans1,max(ans2,0));
    }
    else{
        int ans1=solve(i,j,1,0);
        int ans2=solve(i,j,0,1);
        int ans3=solve(i+1,j+1,0,0);
        return dp[i][j][o1][o2]=max(ans1,max(ans2,max(ans3,0)));
    }
}
int main()
{
    fastio;
    cin>>n>>m>>s>>t;
    memset(dp,-1,sizeof dp);
    cout<< solve(0,0,0,0) <<endl;

}