#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

constexpr long double EPS = 1e-15;
const long double PI = acos(-1);
constexpr int inf = 2e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9+7;
constexpr ll MOD1 = 998244353;
typedef pair<ll,ll> P;

//#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
//#define mp make_pair

int w,h;
int gh,gw;
map<string,int> mp;
int dh[4] = {0,0,1,-1};
int dw[4] = {1,-1,0,0};

int id (int hh, int ww) {
    return w * hh + ww;
}

void out(string s) {
    REP(i,h) {
        REP(j,w) {
            cout << s[id(i,j)];
        }
        cout << endl;
    }
    cout << endl;
}

void solve() {
while (cin >> w >> h) {
    if (w == 0 && h == 0) break;
    mp.clear();
    string s;
    REP(hh,h) {
        REP(ww,w) {
            char c;
            cin >> c;
            if (c == '3') {
                gh = hh;
                gw = ww;
            }
            s +=c;
        }
    }
    int ans = inf;
    mp[s] = 0;
    queue<pair<string,int>> que;
    que.push({s,0});
    while (!que.empty()) {
        string maze = que.front().fi;
        int cost = que.front().se;
        que.pop();
        if (cost > mp[maze]) continue;
        
        int hh = -1,ww;
        REP(i,h) {
            REP(j,w) {
                if (maze[id(i,j)] == '4') {
                    hh = i;
                    ww = j;
                    break;
                }
            }
            if (hh != -1) break;
        }
        for (int i = 0; i < 4; i++) {

            int th = hh + dh[i];
            int tw = ww + dw[i];
            if (th < 0 || th >= h || tw < 0 || tw >= w) continue;
            if (maze[id(th,tw)] == '1') continue;

            if (maze[id(th,tw)] == '0' || maze[id(th,tw)] == '3') {
                string tmp = maze;
                swap(tmp[id(th,tw)], tmp[id(hh,ww)]);
                if (mp.count(tmp)) continue;
                mp[tmp] = cost;
                que.push({tmp, cost});
            }

            if (maze[id(th,tw)] == '2') {
                int tth = th + dh[i];
                int ttw = tw + dw[i];
                if (tth < 0 || tth >= h || ttw < 0 || ttw >= w) continue;
                if (maze[id(tth,ttw)] == '1') continue;
                string tmp = maze;
                swap(tmp[id(tth,ttw)], tmp[id(th,tw)]);
                swap(tmp[id(th,tw)], tmp[id(hh,ww)]);
                if (mp.count(tmp)) continue;
                mp[tmp] = cost + 1;
                if (tth == gh && ttw == gw) {
                    ans = min(ans, cost + 1);
                    continue;
                }
                que.push({tmp, cost + 1});
            }

        }
    }
    if (ans == inf) cout << -1 << endl;
    else cout << ans << endl;
}
}


int main(int argc, char *argv[]){

    // /* Regular */
    // int case_num = 1;
    // if (argc > 1 && stoi(argv[1])) cin >> case_num;
    // REP(case_no, case_num) {
    //   cerr << endl << "case " << case_no + 1 << endl;
    //   solve();
    // }

    /* AOJ */
    solve();

    return 0;
}


Measured memory_bytes: 9176