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

namespace ConsoleApplication1
{
    struct Puzzle
    {
        internal int[] f;
        internal int space, MD;

        public Puzzle(int n)
        {
            f = new int[n];
            space = MD = default(int);
        }

        public Puzzle(Puzzle source)
        {
            f = (int[])source.f.Clone();

            space = source.space;
            MD = source.MD;
        }
    }

    class NPuzzle
    {
        readonly int Limit = 50;
        readonly int n, h, w;

        readonly int[] moveX = new int[] { 0, -1, 0, +1 };
        readonly int[] moveY = new int[] { -1, 0, +1, 0 };
        readonly char[] dir = new char[] { 'u', 'l', 'd', 'r' };

        int limit;
        int[] path;
        int[,] MDT;

        public NPuzzle(int h, int w)
        {
            this.h = h;
            this.w = w;
            n = h * w;

            MDT = new int[n, n];
            path = new int[Limit];

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    MDT[i, j] = Math.Abs(i / w - j / w) + Math.Abs(i % w - j % w);
                }
            }
        }

        public string IterativeDeeping(Puzzle p)
        {
            StringBuilder ret = new StringBuilder();
            Puzzle state;

            p.MD = GetAllMD(p);

            for (limit = p.MD; limit <= Limit; limit++)
            {
                state = new Puzzle(p);

                if (DFS(state, 0, -50))
                {
                    for (int i = 0; i < limit; i++)
                    {
                        ret.Append(dir[path[i]]);
                    }
                    return ret.ToString();
                }
            }
            return null;
        }

        private bool DFS(Puzzle state, int depth, int prev)
        {
            if (state.MD == 0) return true;

            if (depth + state.MD > limit) return false;

            int sx = state.space % w;
            int sy = state.space / w;

            Puzzle tempState;

            for (int r = 0; r < 4; r++)
            {
                int tx = sx + moveX[r];
                int ty = sy + moveY[r];

                if (!IsValid(tx, ty)) continue;

                if (Math.Abs(prev - r) == 2) continue;

                tempState = new Puzzle(state);

                int indexT = tx + ty * w;
                int indexS = sx + sy * w;

                state.MD -= MDT[indexT, state.f[indexT]];
                state.MD += MDT[indexS, state.f[indexT]];

                int temp = state.f[indexT];
                state.f[indexT] = state.f[indexS];
                state.f[indexS] = temp;

                state.space = indexT;

                if (DFS(state, depth + 1, r))
                {
                    path[depth] = r;
                    return true;
                }

                state = tempState;
            }
            return false;
        }

        private bool IsValid(int x, int y)
        {
            if (x < 0) return false;
            if (y < 0) return false;
            if (x >= w) return false;
            if (y >= h) return false;
            return true;
        }

        private int GetAllMD(Puzzle p)
        {
            int sum = 0;

            for (int i = 0; i < n; i++)
            {
                if (p.f[i] != 0) sum += MDT[i, p.f[i]];
            }
            return sum;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();

            int h = 2, w = 4;
            int n = h * w;

            string input;

            while (!string.IsNullOrEmpty(input = Console.ReadLine()))
            {
                NPuzzle NP = new NPuzzle(h, w);
                Puzzle s = new Puzzle(n);

                int[] temp = Array.ConvertAll(input.Split(' '), int.Parse);

                for (int i = 0; i < n; i++)
                {
                    s.f[i] = temp[i];

                    if (s.f[i] == 0) s.space = i;
                }

                string path = NP.IterativeDeeping(s);
                int ans = path != null ? path.Length : 0;
                sb.AppendLine(ans.ToString());
            }
            Console.Write(sb);
        }
    }
}

Measured memory_bytes: 9320