using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Contest
{
    class Program
    {
        static int Main(string[] args)
        {
            new Program().Process();

            return 0;
        }

        private CommonInput cin;

        private Point start, end;

        private void Process()
        {
            cin = new CommonInput();

            int w, h;

            bool isWall = false;
            int c;

            int[,] map;

            while (true)
            {
                w = cin.GetNextToInt();
                h = cin.GetNextToInt();

                if (w == 0 && h == 0)
                    break;

                start = new Point(0, 0);
                end = new Point(2 * w - 1, 2 * h - 1);

                map = new int[2 * w - 1, 2 * h - 1];

                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < w - 1; j++)
                    {
                        isWall = cin.GetNextToInt() == 1;
                        map[2 * j + 1, 2 * i] = isWall ? -1 : 0;
                    }

                    if (i < h - 1)
                    {
                        for (int j = 0; j < 2 * w - 1; j++)
                        {
                            if (j % 2 == 0)
                            {
                                isWall = cin.GetNextToInt() == 1;
                                map[j, 2 * i + 1] = isWall ? -1 : 0;
                            }
                            else
                            {
                                map[j, 2 * i + 1] = -1;
                            }
                        }
                    }
                }

                c = Search(map, start.X, start.Y);

                Console.WriteLine(c);
            }
        }

        private int Search(int[,] map, int i, int j)
        {
            Queue<Point> q = new Queue<Point>();
            Point tmpP = new Point(0, 0);

            q.Enqueue(new Point(i, j));
            map[i, j] = 1;

            while (q.Count > 0 && map[end.X - 1, end.Y - 1] == 0)
            {
                tmpP = q.Dequeue();

                if (tmpP.X + 1 < end.X && map[tmpP.X + 1, tmpP.Y] == 0 && map[tmpP.X + 2, tmpP.Y] == 0)
                {
                    q.Enqueue(new Point(tmpP.X + 2, tmpP.Y));
                    map[tmpP.X + 2, tmpP.Y] = map[tmpP.X, tmpP.Y] + 1;
                }

                if (tmpP.Y + 1 < end.Y && map[tmpP.X, tmpP.Y + 1] == 0 && map[tmpP.X, tmpP.Y + 2] == 0)
                {
                    q.Enqueue(new Point(tmpP.X, tmpP.Y + 2));
                    map[tmpP.X, tmpP.Y + 2] = map[tmpP.X, tmpP.Y] + 1;
                }

                if (tmpP.X - 1 > start.X && map[tmpP.X - 1, tmpP.Y] == 0 && map[tmpP.X - 2, tmpP.Y] == 0)
                {
                    q.Enqueue(new Point(tmpP.X - 2, tmpP.Y));
                    map[tmpP.X - 2, tmpP.Y] = map[tmpP.X, tmpP.Y] + 1;
                }

                if (tmpP.Y - 1 > start.Y && map[tmpP.X, tmpP.Y - 1] == 0 && map[tmpP.X, tmpP.Y - 2] == 0)
                {
                    q.Enqueue(new Point(tmpP.X, tmpP.Y - 2));
                    map[tmpP.X, tmpP.Y - 2] = map[tmpP.X, tmpP.Y] + 1;
                }
            }

            return map[end.X - 1, end.Y - 1];
        }
    }

    public class Point
    {
        public Point(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }

        public int X { get; set; }
        public int Y { get; set; }
    }

    public class CommonInput
    {
        public CommonInput()
        {
            this._s = new string[0];
            this._i = 0;
        }

        public string Line { get; set; }
        private string[] _s;
        private int _i;
        private char[] _sep = { ' ' };

        public void GetLine()
        {
            this._i = 0;
            do
            {
                this.Line = Console.ReadLine();
            } while (this.Line == "");

            if (this.Line != null)
                this._s = this.Line.Split(this._sep, StringSplitOptions.RemoveEmptyEntries);
        }

        public string GetNext()
        {
            if (this._i < this._s.Length)
                return this._s[this._i++];

            this.GetLine();

            return this._s[this._i++];
        }

        public int GetNextToInt()
        {
            return int.Parse(this.GetNext());
        }

        public long GetNextToLong()
        {
            return long.Parse(this.GetNext());
        }

        public double GetNextToDouble()
        {
            return double.Parse(this.GetNext());
        }
    }
}

Measured memory_bytes: 10456