水题。。。爆搜即可。。。


#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>  
#include <cstdlib>
//#include <cmath>
#include <time.h>
#define maxn 500005
#define maxm 100005
#define eps 1e-10
#define mod 1000000007
#define INF 999999999
#define lowbit(x) (x&(-x))
#define mp mark_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
//typedef long long LL;
typedef int LL;
using namespace std;
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head

struct node
{
	int x[10];
	int step;
}tmp, now;
int vis[8][8][8][8][8][8];
int a[10], b[10];
queue<node> q;

void read(void)
{
	for(int i = 2; i <= 6; i++) scanf("%d", &a[i]);
	for(int i = 1; i <= 6; i++) scanf("%d", &b[i]);
}
void bfs(void)
{
	int ans = INF, ok;
	for(int i = 1; i <= 6; i++) tmp.x[i] = a[i], tmp.step = 0;
	q.push(tmp);
	while(!q.empty()) {
		now = q.front();
		q.pop();
		if(vis[now.x[1]][now.x[2]][now.x[3]][now.x[4]][now.x[5]][now.x[6]]) continue;
		vis[now.x[1]][now.x[2]][now.x[3]][now.x[4]][now.x[5]][now.x[6]] = 1;
		ok = 1;
		for(int i = 1; i <= 6; i++)
			if(now.x[i] != b[i]) {
				ok = 0;
				break;
			}
		if(ok) {
			ans = min(now.step, ans);
			continue;
		}
		for(int i = 0; i < 4; i++) {
			if(i == 0) {
				tmp.x[1] = now.x[6];
				tmp.x[2] = now.x[5];
				tmp.x[3] = now.x[3];
				tmp.x[4] = now.x[4];
				tmp.x[5] = now.x[1];
				tmp.x[6] = now.x[2];
			}
			if(i == 1) {
				tmp.x[1] = now.x[5];
				tmp.x[2] = now.x[6];
				tmp.x[3] = now.x[3];
				tmp.x[4] = now.x[4];
				tmp.x[5] = now.x[2];
				tmp.x[6] = now.x[1];
			}
			if(i == 2) {
				tmp.x[1] = now.x[4];
				tmp.x[2] = now.x[3];
				tmp.x[3] = now.x[1];
				tmp.x[4] = now.x[2];
				tmp.x[5] = now.x[5];
				tmp.x[6] = now.x[6];
			}
			if(i == 3) {
				tmp.x[1] = now.x[3];
				tmp.x[2] = now.x[4];
				tmp.x[3] = now.x[2];
				tmp.x[4] = now.x[1];
				tmp.x[5] = now.x[5];
				tmp.x[6] = now.x[6];
			}
			tmp.step = now.step + 1;
			q.push(tmp);
		}
	}
	if(ans == INF) printf("-1\n");
	else printf("%d\n", ans);
}
void work(void)
{
	memset(vis, 0, sizeof vis);
	bfs();
}
int main(void)
{
	while(scanf("%d", &a[1])!=EOF) {
		read();
		work();
	}
	return 0;
}