#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, int> pii;
typedef vector<pii> vii;
const ll INF=1LL<<63-1;
string s[2];
int n;
unordered_map<string,int> c[2],nc[2];
string rev(string ss,int i,int j){
while(i<j) swap(ss[i++],ss[j--]);
return ss;
}
bool insert(int from,const string prev,int pi,int pj){
for(int i=0;i<=pi;i++){
for(int j=pj;j<n;j++){
if(i!=pi || j!=pj){
string k=rev(prev,i,j);
//cout<<prev<<" "<<k<<i<<j<<endl;
if(c[from^1].count(k)){
return true;
}else
nc[from][k]=i*16+j;
}
}
}
for(int i=pj+1;i<n;i++){
for(int j=i+1;j<n;j++){
string k=rev(prev,i,j);
//cout<<prev<<" "<<k<<i<<j<<endl;
if(c[from^1].count(k)){
return true;
}else
nc[from][k]=i*16+j;
}
}
return false;
}
int bfs(){
if(s[0]==s[1]) return 0;
c[0].clear();c[1].clear();
c[0][s[0]]=0;
c[1][s[1]]=0;
for(int d=1;d<n;d++){
nc[d%2].clear();
for(auto st:c[d%2]){
if(insert(d%2,st.first,st.second/16,st.second%16)){
return d;
}
}
c[d%2]=nc[d%2];
}
return n;
}
int main() {
cin>>s[0]>>s[1];n=s[0].size();
int d=bfs();cin>>s[0];
cout<<d+bfs();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHBhaXI8bG9uZyBsb25nLCBpbnQ+IHBpaTsKdHlwZWRlZiB2ZWN0b3I8cGlpPiB2aWk7CmNvbnN0IGxsIElORj0xTEw8PDYzLTE7CgpzdHJpbmcgc1syXTsKaW50IG47CnVub3JkZXJlZF9tYXA8c3RyaW5nLGludD4gY1syXSxuY1syXTsKCnN0cmluZyByZXYoc3RyaW5nIHNzLGludCBpLGludCBqKXsKCXdoaWxlKGk8aikgc3dhcChzc1tpKytdLHNzW2otLV0pOwoJcmV0dXJuIHNzOwp9Cgpib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