import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

void main() {
    while (true) {
        int W, H;
        scan(W, H);

        if (W == 0 && H == 0) return;

        int[][] kabe;
        foreach (i ; 0 .. 2*H - 1) {
            kabe ~= readln.split.to!(int[]);
        }

        debug {
            writefln("%(%s\n%)", kabe);
        }

        auto ans = solve(H, W, kabe) + 1;
        writeln(ans);
    }
}

int solve(int H, int W, int[][] kabe) {
    auto dist = new int[][](H, W);
    fillAll(dist, inf);
    dist[0][0] = 0;

    auto q = Queue!int(H * W);
    q.insert(0);

    int[] di = [1, -1, 0, 0];
    int[] dj = [0, 0, 1, -1];

    while (!q.empty) {
        auto pi = q.front / W, pj = q.front % W;
        q.popFront();

        foreach (k ; 0 .. 4) {
            int ni = pi + di[k];
            int nj = pj + dj[k];

            if (ni < 0 || nj < 0 || ni >= H || nj >= W) continue;
            if (k == 0 && kabe[2*pi + 1][nj]) continue;
            if (k == 1 && kabe[2*ni + 1][nj]) continue;
            if (k == 2 && kabe[2*ni][nj - 1]) continue;
            if (k == 3 && kabe[2*ni][nj]) continue;
            if (dist[ni][nj] < inf) continue;
            dist[ni][nj] = dist[pi][pj] + 1;
            q.insert(ni * W + nj);
        }
    }

    return dist[H - 1][W - 1] < inf ? dist[H - 1][W - 1] : -1;
}



struct Queue(T) {
    private {
        size_t cap, head, tail;
        T[] data;
    }

    this (size_t n) in {
        assert(n > 0, "The capacity of a queue must be a positive integer.");
    } body {
        cap = n + 1;
        data = new T[](cap);
    }

    void clear() {
        head = tail = 0;
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return head == (tail + 1) % cap;
    }

    size_t length() {
        return head <= tail ? tail - head : cap - head + tail;
    }

    T front() in {
        assert(!empty, "The queue is empty.");
    } body {
        return data[head];
    }

    void removeFront() in {
        assert(!empty, "The queue is empty.");
    } body {
        (++head) %= cap;
    }

    alias popFront = removeFront;

    void insertBack(T x) in {
        assert(!full, "The queue is full.");
    } body {
        data[tail++] = x;
        tail %= cap;
    }

    alias insert = insertBack;

    T[] array() {
        return head <= tail ? data[head .. tail].dup : data[head .. $] ~ data[0 .. tail];
    }

    string toString() {
        import std.format : format;

        if (head <= tail) {
            return format("[%(%s, %)]", data[head .. tail]);
        }
        else {
            return format("[%(%s, %)", data[head .. $]) ~ format(", %(%s, %)]", data[0 .. tail]);
        }
    }
}



void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x > arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x < arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

