#include <cstdio>
#include <queue>
#include <vector>
#include <string>
using namespace std;


const int inf = (int)1e9;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int sdx1[] = {-1, 0, 2, 1};
const int sdy1[] = {0, 2, 1, -1};
const int sdx2[] = {-1, 1, 2, 0};
const int sdy2[] = {1, 2, 0, -1};

inline int to_state(int x, int y, int dir, int h, int w)
{ return (x * w + y) * 4 + dir; }
inline int to_x(int state, int h, int w)
{ return state / 4 / w; }
inline int to_y(int state, int h, int w)
{ return state / 4 % w; }
inline int to_dir(int state, int h, int w)
{ return state % 4; }
inline int to_pos(int x, int y, int h, int w)
{ return x * w + y; }
inline int pos_to_x(int pos, int h, int w)
{ return pos / w; }
inline int pos_to_y(int pos, int h, int w)
{ return pos % w; }
inline bool is_valid(int x, int y, int h, int w)
{ return x >= 0 && x < h && y >= 0 && y < w; }

vector<int> calc_dist(int start_x, int start_y, int kx, int ky, vector<string> &field)
{
	const int h = field.size(), w = field[0].size();
	vector<int> dist(h * w, inf);
	queue<pair<int, int> > q;

	q.push({0, to_pos(start_x, start_y, h, w)});

	while(!q.empty()) {

		const int d = q.front().first;
		const int pos = q.front().second;
		q.pop();
		if(dist[pos] < inf)
			continue;
		dist[pos] = d;

		const int x = pos_to_x(pos, h, w);
		const int y = pos_to_y(pos, h, w);

		for(int i = 0; i < 4; ++i) {
			const int nx = x + dx[i];
			const int ny = y + dy[i];
			if(!is_valid(nx, ny, h, w))
				continue;
			if(field[nx][ny] == '*')
				continue;
			bool ok = true;
			for(int k = 0; k < 4; ++k) {
				if(nx == kx + k / 2 && ny == ky + k % 2)
					ok = false;
			}
			if(ok)
				q.push({d + 1, to_pos(nx, ny, h, w)});
		}
	}

	return dist;
}

void add_next(int sx1, int sy1, int sx2, int sy2, int x, int y, int dir, int step,
	vector<string> &f, priority_queue<pair<int, int> > &q)
{
	const int h = f.size(), w = f[0].size();
	const vector<int> d1 = calc_dist(sx1, sy1, x, y, f);
	const vector<int> d2 = calc_dist(sx2, sy2, x, y, f);

	for(int i = 0; i < 4; ++i) {
		if(i == dir)
			continue;
		const int nx1 = x + sdx1[i];
		const int ny1 = y + sdy1[i];
		const int nx2 = x + sdx2[i];
		const int ny2 = y + sdy2[i];
		if(!is_valid(nx1, ny1, h, w) || !is_valid(nx2, ny2, h, w))
			continue;
		const int len1 = d1[to_pos(nx1, ny1, h, w)] + d2[to_pos(nx2, ny2, h, w)];
		const int len2 = d1[to_pos(nx2, ny2, h, w)] + d2[to_pos(nx1, ny1, h, w)];
		const int len = min(len1, len2);
		if(len < inf)
			q.push({-(step + len), to_state(x, y, i, h, w)});
	}
}

int main()
{
	while(true) {

		int h, w;
		vector<string> field;
		int sp1x = -1, sp1y = -1, sp2x = -1, sp2y = -1;
		int kx = -1, ky = -1;

		scanf("%d%d", &h, &w);
		if(h == 0)
			break;
		field.resize(h);
		for(int x = 0; x < h; ++x) {
			char buf[64];
			scanf(" %s", buf);
			field[x] = buf;
			for(int y = 0; y < w; ++y) {
				if(buf[y] == 'X' && kx == -1)
					kx = x, ky = y;
				if(buf[y] == '.') {
					if(sp1x == -1)
						sp1x = x, sp1y = y;
					else
						sp2x = x, sp2y = y;
				}
			}
		}

		if(kx == 0 && ky == 0) {
			printf("0\n");
			continue;
		}

		vector<int> cost(h * w * 4, inf);
		priority_queue<pair<int, int> > q;

		add_next(sp1x, sp1y, sp2x, sp2y, kx, ky, -1, 0, field, q);

		while(!q.empty()) {

			const int step = -q.top().first;
			const int state = q.top().second;
			q.pop();
			if(cost[state] <= step)
				continue;
			cost[state] = step;

			const int x = to_x(state, h, w);
			const int y = to_y(state, h, w);
			const int dir = to_dir(state, h, w);
			//printf("%d, %d, %d -> %d\n", x, y, dir, step);

			q.push({-(step + 1), to_state(x + dx[dir], y + dy[dir], dir ^ 2, h, w)});

			const int sx1 = x + sdx1[dir], sy1 = y + sdy1[dir];
			const int sx2 = x + sdx2[dir], sy2 = y + sdy2[dir];

			add_next(sx1, sy1, sx2, sy2, x, y, dir, step, field, q);
		}

		int ans = inf;

		for(int i = 0; i < 4; ++i)
			ans = min(ans, cost[to_state(0, 0, i, h, w)]);

		printf("%d\n", ans < inf ? ans : -1);
	}

	return 0;
}

Measured memory_bytes: 1140