#include <bits/stdc++.h>
#define _nhatminh int main()
#define ll long long
#define str string
#define fir first
#define sec second
#define ld long double
#define pb push_back
#define MOD 100000009
#define freopen(name) if(fopen(name".INP","r")) {freopen (name".INP","r",stdin); freopen (name".OUT","w",stdout);}
#define ALL(x) (x).begin(),(x).end()
#define piint pair < int , int >
#define piL pair < int , ll>
#define pLL pair < ll , ll >
#define TIME (1.0*clock()/CLOCKS_PER_SEC)
using namespace std;
const int Max_n=5*1e3 ;
bool check[Max_n+3][Max_n+3];
int dp[Max_n+3][Max_n+3] , f[Max_n+3];
ll res = 1 ;
void KT ( str a, str b){
a = '*' + a ;
b = '*' + b ;
int n = a.size() -1 , m = b.size() - 1 ;
//fill ( check + 1 , check + n + 1 , 1 );
for (int i = 1 ; i <= n ; i ++ )
check[i][i] = true;
for (int d = 2 ; d <= n ; d ++ ){
for (int i = 1 ; i <= n - d + 1 ; i ++){
int j = i + d - 1 ;
if ( d == 2 && a[i] == a[j] ) { check[i][j]= true; continue ;}
if (a[i] == a[j] && check[i+1][j-1]) check[i][j] = true;
}
}
for (int d = 2 ; d <= n ; d ++ ){
for (int i = 1 ; i <= n - d + 1; i ++ ){
int j = i + d - 1 ;
//ghep so xau con lien tiep cua x de dc xdx
if ( a[i] == b[j] ) dp[i][j] = dp[i+1][j-1] + 2 ;
else dp[i][j] = 0 ;
//cout << dp[i][j] << ' ' << i << ' ' << j<< '\n';
}
}
for (int i = 1 ; i <= n ; i ++ ){
for (int j = m ; j >= i ; j --)
f[i] = max ( f[i] , dp[i][j]) ;
}
for (int i = 1 ; i<= n ; i ++ ){
for (int j = i ; j <= n ; j ++ )
if ( check[i+1][j] ) {
res = max ( res , 1ll*f[i] + j - i );
// cout << i+1 << ' ' << j << ' ' << f[i] << '\n';
}
}
}
void solve(){
str a , b ; cin >> a >> b;
KT ( a , b ) ;
reverse(ALL(b));
reverse(ALL(a));
KT(b,a);
cout << res ;
}
_nhatminh{
freopen("GHEPXAU");
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int q=1;
// cin >> q;
while (q--)
solve();
cerr << '\n' << "Time elapsed " << TIME << "s.\n";
return (0);
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgX25oYXRtaW5oICBpbnQgbWFpbigpCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgc3RyIHN0cmluZwojZGVmaW5lIGZpciBmaXJzdAojZGVmaW5lIHNlYyBzZWNvbmQKI2RlZmluZSBsZCBsb25nIGRvdWJsZQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIE1PRCAxMDAwMDAwMDkKI2RlZmluZSBmcmVvcGVuKG5hbWUpIGlmKGZvcGVuKG5hbWUiLklOUCIsInIiKSkge2ZyZW9wZW4gKG5hbWUiLklOUCIsInIiLHN0ZGluKTsgZnJlb3BlbiAobmFtZSIuT1VUIiwidyIsc3Rkb3V0KTt9CiNkZWZpbmUgQUxMKHgpICh4KS5iZWdpbigpLCh4KS5lbmQoKQojZGVmaW5lIHBpaW50IHBhaXIgPCBpbnQgLCBpbnQgPgojZGVmaW5lIHBpTCBwYWlyIDwgaW50ICwgbGw+CiNkZWZpbmUgcExMIHBhaXIgPCBsbCAsIGxsID4KI2RlZmluZSBUSU1FICgxLjAqY2xvY2soKS9DTE9DS1NfUEVSX1NFQykKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IE1heF9uPTUqMWUzIDsKYm9vbCBjaGVja1tNYXhfbiszXVtNYXhfbiszXTsKaW50IGRwW01heF9uKzNdW01heF9uKzNdICwgZltNYXhfbiszXTsKbGwgcmVzID0gMSA7CnZvaWQgS1QgKCBzdHIgYSwgc3RyIGIpewoJYSA9ICcqJyArIGEgOwoJYiA9ICcqJyArIGIgOwoJaW50IG4gPSBhLnNpemUoKSAtMSAgLCBtID0gYi5zaXplKCkgLSAxIDsKCS8vZmlsbCAoIGNoZWNrICsgMSAsIGNoZWNrICsgbiArIDEgLCAxICk7Cglmb3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