/// <reference path="node.d.ts" />
/// <reference path="lib.ts" />
var PriorityQueue = (function () {
    function PriorityQueue() {
        this.heap = [];
    }
    PriorityQueue.prototype.push = function (v) {
        var heap = this.heap;
        var i = heap.length;
        while (i > 0) {
            var p = _.floor((i - 1) / 2);
            if (heap[p][1] <= v[1])
                break;
            heap[i] = heap[p];
            i = p;
        }
        heap[i] = v;
    };
    PriorityQueue.prototype.pop = function () {
        var heap = this.heap;
        var ret = heap[0];
        var v = heap.pop();
        if (heap.length === 0)
            return ret;
        var i = 0;
        while (i * 2 + 1 < heap.length) {
            var a = i * 2 + 1, b = i * 2 + 2;
            if (b < heap.length && heap[b][1] < heap[a][1])
                a = b;
            if (heap[a][1] >= v[1])
                break;
            heap[i] = heap[a];
            i = a;
        }
        heap[i] = v;
        return ret;
    };
    PriorityQueue.prototype.empty = function () {
        return this.heap.length === 0;
    };
    return PriorityQueue;
})();
function dim(a, v) {
    if (a.length === 0)
        return v;
    var result = [];
    for (var i = 0, len = a.shift(); i < len; ++i)
        result.push(dim(_.clone(a), v));
    return result;
}
var Dice = (function () {
    function Dice(t, n, w) {
        if (t === void 0) { t = 1; }
        if (n === void 0) { n = 5; }
        if (w === void 0) { w = 3; }
        this.t = t;
        this.n = n;
        this.w = w;
    }
    Dice.prototype.roll = function (x, y) {
        var t = this.t, n = this.n, w = this.w;
        if (x === -1)
            _a = [7 - n, t], this.t = _a[0], this.n = _a[1];
        if (x === 1)
            _b = [n, 7 - t], this.t = _b[0], this.n = _b[1];
        if (y === -1)
            _c = [7 - w, t], this.t = _c[0], this.w = _c[1];
        if (y === 1)
            _d = [w, 7 - t], this.t = _d[0], this.w = _d[1];
        var _a, _b, _c, _d;
    };
    return Dice;
})();
var State = (function () {
    function State(x, y, u) {
        this.x = x;
        this.y = y;
        this.u = u;
    }
    State.prototype.hash = function () {
        return this.x.concat(this.y, this.u).join('');
    };
    return State;
})();
var r, c;
var grid = [];
var s = [];
var t = {};
var h;
var memo = {};
function main(input) {
    r = +input.shift();
    c = +input.shift();
    for (var i = 0; i < r; ++i)
        grid.push(input.shift().split(''));
    var d = new Dice();
    for (var i = 0; i < 2; ++i) {
        for (var j = 0; j < 4; ++j) {
            for (var k = 0; k < 4; ++k) {
                s.push(new Dice(d.t, d.n, d.w));
                d.roll(0, 1);
            }
            d.roll(1, 0);
        }
        d.roll(0, 1);
    }
    s = _.uniq(s, false, function (v) { return '' + v.t + v.n + v.w; });
    for (var i = 0; i < 24; ++i) {
        t['' + s[i].t + s[i].n + s[i].w] = i;
    }
    h = dim([3, r, c, 24], Infinity);
    var k = 0;
    for (var i = 0; i < r; ++i) {
        for (var j = 0; j < c; ++j) {
            if (!isNaN(+grid[i][j])) {
                bfs(i, j, k);
                ++k;
            }
        }
    }
    var x = [];
    var y = [];
    var u = [];
    for (var i = 0; i < r; ++i) {
        for (var j = 0; j < c; ++j) {
            if (grid[i][j] === 'o') {
                x.push(i);
                y.push(j);
                u.push(t['153']);
            }
        }
    }
    var start = new State(x, y, u);
    var queue = new PriorityQueue();
    memo[start.hash()] = 0;
    queue.push([start, approx(start)]);
    skip: while (!queue.empty()) {
        var cur = queue.pop()[0];
        var dx = [-1, 1, 0, 0];
        var dy = [0, 0, -1, 1];
        for (var i = 0; i < 3; ++i) {
            for (var j = 0; j < 4; ++j) {
                var next = new State(_.clone(cur.x), _.clone(cur.y), _.clone(cur.u));
                var d = new Dice(s[cur.u[i]].t, s[cur.u[i]].n, s[cur.u[i]].w);
                d.roll(dx[j], dy[j]);
                next.x[i] += dx[j];
                next.y[i] += dy[j];
                next.u[i] = t['' + d.t + d.n + d.w];
                if (_.inRange(next.x[i], 0, r) &&
                    _.inRange(next.y[i], 0, c) &&
                    grid[next.x[i]][next.y[i]] !== 'x' &&
                    _.every([0, 1, 2], function (v) { return cur.x[v] !== next.x[i] || cur.y[v] !== next.y[i]; }) &&
                    memo[next.hash()] === undefined) {
                    memo[next.hash()] = memo[cur.hash()] + 1;
                    queue.push([next, approx(next)]);
                    if (_.every([0, 1, 2], function (v) { return s[next.u[v]].t === +grid[next.x[v]][next.y[v]]; })) {
                        console.log(memo[next.hash()]);
                        break skip;
                    }
                }
            }
        }
    }
}
function bfs(x, y, k) {
    var queue = [];
    for (var i = 0; i < 24; ++i) {
        if (s[i].t === +grid[x][y]) {
            h[k][x][y][i] = 0;
            queue.push([x, y, i]);
        }
    }
    while (queue.length !== 0) {
        var _a = queue.shift(), x = _a[0], y = _a[1], u = _a[2];
        var dx = [-1, 1, 0, 0];
        var dy = [0, 0, -1, 1];
        for (var i = 0; i < 4; ++i) {
            var a = x + dx[i];
            var b = y + dy[i];
            var d = new Dice(s[u].t, s[u].n, s[u].w);
            d.roll(dx[i], dy[i]);
            var v = t['' + d.t + d.n + d.w];
            if (_.inRange(a, 0, r) &&
                _.inRange(b, 0, c) &&
                grid[a][b] !== 'x' &&
                h[k][a][b][v] === Infinity) {
                h[k][a][b][v] = h[k][x][y][u] + 1;
                queue.push([a, b, v]);
            }
        }
    }
}
function approx(state) {
    var result = Infinity;
    var perm = [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]];
    for (var i = 0; i < 6; ++i) {
        var a = 0;
        for (var j = 0; j < 3; ++j) {
            a += h[perm[i][j]][state.x[j]][state.y[j]][state.u[j]];
        }
        result = Math.min(a, result);
    }
    return result;
}
/// <reference path="node.d.ts" />
/// <reference path="main.ts" />
(function () {
    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    var input = '';
    process.stdin.on('data', function (chunk) {
        input += chunk;
    });
    process.stdin.on('end', function () {
        main(input.split(/\s+/));
    });
})();
// based on lodash
var _;
(function (_) {
    function clone(a) {
        var result = [];
        for (var i = 0; i < a.length; ++i)
            result.push(a[i]);
        return result;
    }
    _.clone = clone;
    function every(a, func) {
        for (var i = 0; i < a.length; ++i) {
            if (!func(a[i], i))
                return false;
        }
        return true;
    }
    _.every = every;
    function fill(a, v) {
        for (var i = 0; i < a.length; ++i)
            a[i] = v;
        return a;
    }
    _.fill = fill;
    function floor(v) {
        return Math.floor(v);
    }
    _.floor = floor;
    function indexOf(a, v, start) {
        if (start === void 0) { start = 0; }
        if (start === true) {
            var result = _.sortedIndex(a, v);
            return a[result] === v ? result : -1;
        }
        for (var i = start; i < a.length; ++i) {
            if (a[i] === v)
                return i;
        }
        return -1;
    }
    _.indexOf = indexOf;
    function inRange(v, start, end) {
        return start <= v && v < end;
    }
    _.inRange = inRange;
    function map(a, func) {
        var result = [];
        for (var i = 0; i < a.length; ++i)
            result.push(func(a[i], i));
        return result;
    }
    _.map = map;
    function max(a) {
        return Math.max.apply(Math, a);
    }
    _.max = max;
    function min(a) {
        return Math.min.apply(Math, a);
    }
    _.min = min;
    function reduce(a, func, init) {
        var result = init;
        for (var i = 0; i < a.length; ++i)
            result = func(result, a[i], i);
        return result;
    }
    _.reduce = reduce;
    function sortedIndex(a, v) {
        var s = 0, t = a.length;
        while (s < t) {
            var m = _.floor((s + t) / 2);
            if (a[m] < v)
                s = m + 1;
            else
                t = m;
        }
        return s;
    }
    _.sortedIndex = sortedIndex;
    function sortBy(a, func) {
        if (func === void 0) { func = function (v) { return v; }; }
        var b = a;
        for (var i = 0; i < a.length; ++i)
            b[i] = [a[i], i];
        b.sort(function (a, b) {
            var c = func(a[0], a[1]), d = func(b[0], b[1]);
            return (c !== d) ? c - d : a[1] - b[1];
        });
        for (var i = 0; i < a.length; ++i)
            a[i] = b[i][0];
        return a;
    }
    _.sortBy = sortBy;
    function sum(a) {
        var result = 0;
        for (var i = 0; i < a.length; ++i)
            result += a[i];
        return result;
    }
    _.sum = sum;
    function uniq(a, sorted, func) {
        if (sorted === void 0) { sorted = false; }
        if (func === void 0) { func = function (v) { return v; }; }
        if (sorted) {
            var result = [];
            for (var i = 0; i < a.length; ++i) {
                if (i === 0 || func(a[i], i) !== func(a[i - 1], i - 1))
                    result.push(a[i]);
            }
            return result;
        }
        else {
            var b = [];
            for (var i = 0; i < a.length; ++i) {
                if (_.every(b, function (v) { return func(a[v], v) !== func(a[i], i); }))
                    b.push(i);
            }
            return _.map(b, function (v) { return a[v]; });
        }
    }
    _.uniq = uniq;
})(_ || (_ = {}));