#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y,weiax,weiay,weibx,weiby,step;
bool operator<(const node& other)const{
if(x!=other.x)return x<other.x;
if(y!=other.y)return y<other.y;
if(weiax!=other.weiax)return weiax<other.weiax;
if(weiay!=other.weiay)return weiay<other.weiay;
if(weibx!=other.weibx)return weibx<other.weibx;
if(weiby!=other.weiby)return weiby<other.weiby;
}
};
int xa,xb,ya,yb,dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
string mp[10];
queue<node> q;
map<node,bool> mapp;
int main(){
int spacex,spacey;
for(int i=0;i<2;i++){
getline(cin,mp[i]);
for(int j=0;j<3;j++){
if(mp[i][j]'A'){
xa=i;
ya=j;
}else if(mp[i][j]'B'){
xb=i;
yb=j;
}else if(mp[i][j]' '){
spacex=i;
spacey=j;
}
}
}
q.push({spacex,spacey,xa,ya,xb,yb,0});
while(q.size()){
node k=q.front();
q.pop();
if(k.weiaxxb&&k.weiayyb&&k.weibxxa&&k.weibyya){
cout<<k.step;
return 0;
}
for(int i=0;i<4;i++){
node tmp;
tmp.x=k.x+dx[i];
tmp.y=k.y+dy[i];
if(tmp.x<0||tmp.x>1||tmp.y<0||tmp.y>2)continue;
tmp.weiax=k.weiax;
tmp.weibx=k.weibx;
tmp.weiay=k.weiay;
tmp.weiby=k.weiby;
tmp.step=k.step+1;
if(tmp.xk.weiax&&tmp.yk.weiay){
tmp.weiax=k.x;
tmp.weiay=k.y;
}else if(tmp.xk.weibx&&tmp.y==k.weiby){
tmp.weibx=k.x;
tmp.weiby=k.y;
}
if(mapp[tmp])continue;
mapp[tmp]=1;
q.push(tmp);
}