#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class InfiniteLab { public:
long long getDistance(vector <string> map, long long r1, int c1, long long r2, int c2)
{
LL H = map.size();
LL W = map[0].size();
if( r1 > r2 ) {
swap(r1,r2);
swap(c1,c2);
}
if( r1 < 0 ) {
// check!!!!
LL Hx = (-r1/H + 1)*H;
r1 += Hx;
r2 += Hx;
}
cerr << r1 << " " << r2 << endl;
vector<string> mmm(map);
mmm.insert(mmm.end(), map.begin(), map.end());
mmm.insert(mmm.end(), map.begin(), map.end());
if( r1/H == r2/H )
return dist(mmm, H+(r1%H), c1, H+(r2%H), c2);
return -1;
// top to top
vector<vector<LL> > tochu(W, vector<LL>(W, -1));
for(int x1=0; x1<W; ++x1) if( map[0][x1] != '#' )
for(int x2=0; x2<W; ++x2) if( map[0][x2] != '#' )
tochu[x1][x2] = dist( mmm, H, x1, H*2, x2 );
// r1c1 to top
vector<LL> from(W, -1);
for(int x1=0; x1<W; ++x1) if( map[0][x1] != '#' )
from[W] = dist( mmm, H+(r1%H), c1, H*2, x1 );
// top to r2c2
vector<LL> to(W, -1);
for(int x1=0; x1<W; ++x1) if( map[0][x1] != '#' )
to[W] = dist( mmm, H, x1, H*2+(r2%H), c2 );
LL best = -1;
for(int i=0; i<from.size(); ++i) if( from[i]>=0 )
for(int j=0; j<to.size(); ++j) if( to[j]>=0 )
{
LL sc = from[i] + to[j];
LL s2 = -1; // TODO!
if( s2 != -1 && (best == -1 || s2+sc<best) )
best = s2+sc;
}
return best;
}
LL dist(vector<string>& mmm, LL y1, LL x1, LL y2, LL x2)
{
vector< pair<LL,LL> > Q;
set< pair<LL,LL> > V;
Q.push_back( make_pair(y1,x1) );
V.insert( make_pair(y1,x1) );
for(int d=0; !Q.empty(); ++d)
{
vector< pair<LL,LL> > Q_next;
for(int i=0; i<Q.size(); ++i)
{
LL y = Q[i].first;
LL x = Q[i].second;
if( y==y2 && x==x2 )
return d;
int dy[] = {-1,+1,0,0};
int dx[] = {0,0,-1,+1};
for(int i=0; i<4; ++i) {
LL yy = y+dy[i];
LL xx = x+dx[i];
if( 0<=yy && yy<mmm.size() && 0<=xx && xx<mmm[0].size() && mmm[yy][xx]!='#' ) {
pair<LL,LL> p(yy,xx);
if( ! V.count(p) ) {
V.insert(p);
Q_next.push_back(p);
}
}
}
if( mmm[y][x] == 'T' ) {
for(int xt=0; xt<mmm[y].size(); ++xt) if(xt!=x && mmm[y][xt]=='T') {
pair<LL,LL> p(y,xt);
if( ! V.count(p) ) {
V.insert(p);
Q_next.push_back(p);
}
break;
}
}
}
Q_next.swap(Q);
}
return -1;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, InfiniteLab().getDistance(map, r1, c1, r2, c2));}
int main(){
CASE(0)
string map_[] = {"#...##",
".##...",
"..#.##",
"#.#.##"};
vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_));
long long r1 = 1LL;
int c1 = 0;
long long r2 = 5LL;
int c2 = 3;
long long _ = 7LL;
END
CASE(1)
string map_[] = {"##.#.",
".#T#T",
"...#.",
"##.#."};
vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_));
long long r1 = 7LL;
int c1 = 4;
long long r2 = 1LL;
int c2 = 0;
long long _ = 9LL;
END
CASE(2)
string map_[] = {"..######.#",
".###T###.T",
"..T#.##T##",
".######..#"};
vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_));
long long r1 = 1LL;
int c1 = 0;
long long r2 = 6LL;
int c2 = 4;
long long _ = 11LL;
END
CASE(3)
string map_[] = {"..#..",
".#.#.",
"....."};
vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_));
long long r1 = -29LL;
int c1 = 2;
long long r2 = 19LL;
int c2 = 2;
long long _ = 54LL;
END
CASE(4)
string map_[] = {".#.#.",
"..#..",
".....",
".....",
"..#.."};
vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_));
long long r1 = -999LL;
int c1 = 3;
long long r2 = 100LL;
int c2 = 2;
long long _ = -1LL;
END
/*
CASE(5)
string map_[] = ;
vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_));
long long r1 = LL;
int c1 = ;
long long r2 = LL;
int c2 = ;
long long _ = LL;
END
CASE(6)
string map_[] = ;
vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_));
long long r1 = LL;
int c1 = ;
long long r2 = LL;
int c2 = ;
long long _ = LL;
END
*/
}
// END CUT HERE