#include <iostream>
#include <cstdio>
#include <cstring>

#include <algorithm>
#include <vector>
using namespace std;

#define MP make_pair
#define LL long long
#define REP(i,a,b) for (int i = a; i <= b; ++i)

struct dice {
    int u, d, l, r, f, b;
    bool operator == (const dice &p) const {
        if (u != p.u) return false;
        if (d != p.d) return false;
        if (l != p.l) return false;
        if (r != p.r) return false;
        if (f != p.f) return false;
        if (b != p.b) return false;
    }
    int input() {
        return scanf( "%d%d%d%d%d%d", &u, &d, &l, &r, &f, &b ) != EOF;
    }
}cur, nxt, S, T;

void L() {
    nxt.f = cur.f, nxt.b = cur.b;
    nxt.l = cur.u, nxt.u = cur.r;
    nxt.r = cur.d, nxt.d = cur.l;
}

void R() {
    nxt.f = cur.f, nxt.b = cur.b;
    nxt.l = cur.d, nxt.d = cur.r;
    nxt.r = cur.u, nxt.u = cur.l;
}

void F() {
    nxt.l = cur.l, nxt.r = cur.r;
    nxt.f = cur.u, nxt.u = cur.b;
    nxt.b = cur.d, nxt.d = cur.f;
}

void B() {
    nxt.l = cur.l, nxt.r = cur.r;
    nxt.f = cur.d, nxt.d = cur.b;
    nxt.b = cur.u, nxt.u = cur.f;
}

int dis[105];
dice Q[105];

int bfs() {
    if (S == T) return 0;
    memset(dis, -1, sizeof(dis));
    int msk = S.u * 10 + S.f, head = 0, tail = 0, goal = T.u * 10 + T.f;
    dis[msk] = 0;
    Q[tail++] = S;
    while (head < tail) {
        cur = Q[head++];
        int now = dis[cur.u * 10 + cur.f];
        L(); msk = nxt.u * 10 + nxt.f;
        if (dis[msk] == -1) {
            dis[msk] = now + 1;
            if (msk == goal && nxt == T) return dis[msk];
            Q[tail++] = nxt;
        }
        R(); msk = nxt.u * 10 + nxt.f;
        if (dis[msk] == -1) {
            dis[msk] = now + 1;
            if (msk == goal && nxt == T) return dis[msk];
            Q[tail++] = nxt;
        }
        F(); msk = nxt.u * 10 + nxt.f;
        if (dis[msk] == -1) {
            dis[msk] = now + 1;
            if (msk == goal && nxt == T) return dis[msk];
            Q[tail++] = nxt;
        }
        B(); msk = nxt.u * 10 + nxt.f;
        if (dis[msk] == -1) {
            dis[msk] = now + 1;
            if (msk == goal && nxt == T) return dis[msk];
            Q[tail++] = nxt;
        }
    }
    return -1;
}

int main()
{
    while (S.input()) {
        T.input();
        printf( "%d\n", bfs() );
    }
    return 0;
}