// Arup Guha
// 11/4/2018
// Solution to 2018 SER D1/D2 Problem: Illiteracy

import java.util.*;

public class illiteracy {
	
	final public static int NUML = 8;
	public static int SIZE;
	public static int[] pow6;
	
	public static void main(String[] args) {
		
		// Helpful lookup table.
		pow6 = new int[NUML];
		pow6[7] = 1;
		for (int i=6; i>=0; i--) pow6[i] = pow6[i+1]*6;
		SIZE = pow6[0]*6;
		
		Scanner stdin = new Scanner(System.in);
		int start = getNum(stdin.next());
		int end = getNum(stdin.next());
		
		// Set up stuff for BFS.
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		int[] dist = new int[SIZE];
		Arrays.fill(dist, -1);
		q.add(start);
		dist[start] = 0;
		
		// Start BFS.
		while (q.size() > 0) {
			
			int cur = (int)q.pollFirst();
			
			// Got it.
			if (cur == end) break;
			
			// Put all possible next strings here.
			ArrayList<Integer> next = new ArrayList<Integer>();
			for (int pos=0; pos<8; pos++) {
				int curD = getVal(cur, pos);
				if (curD == 0) next.add(f_a(cur, pos));
				if (curD == 1) next.add(f_b(cur, pos));
				if (curD == 2) next.add(f_c(cur, pos));
				if (curD == 3) next.add(f_d(cur, pos));
				if (curD == 4) next.add(f_e(cur, pos));
				if (curD == 5) next.add(f_f(cur, pos));
			}
			
			// Enqueue new ones.
			for(Integer s: next) {
				if (dist[s] != -1) continue;
				dist[s] = dist[cur] + 1;
				q.add(s);
			}
		}
		
		// Ta da!
		System.out.println(dist[end]);
	}
	
	// Returns base 6 value of s.
	public static int getNum(String s) {
		int res = 0;
		for (int i=0; i<s.length(); i++)
			res = 6*res + s.charAt(i) - 'A';
		return res;
	}
	
	public static int f_a(int s, int pos) {
		
		int delta = 0;
		
		// Do left.
		if (pos > 0) delta += getRotDelta(s, pos-1);
		
		// Do right.
		if (pos < NUML-1) delta += getRotDelta(s, pos+1);
		
		// New val.
		return s + delta;
	}
	
	public static int f_b(int s, int pos) {
		if (pos == 0 || pos == NUML-1) return s;
		
		int left = getVal(s, pos-1);
		int right = getVal(s, pos+1);
		int delta = -right*pow6[pos+1];
		delta += left*pow6[pos+1];
		return s + delta;
	}

	public static int f_c(int s, int pos) {
		int delta = getRotDelta(s, 7-pos);
		return s + delta;
	}
	
	public static int f_d(int s, int pos) {
		
		int delta = 0;
		
		// Left.
		if (pos < 4) {
			for (int i=0; i<pos; i++)
				delta += getRotDelta(s, i);
		}
		
		// Right.
		else {
			for (int i=pos+1; i<NUML; i++)
				delta += getRotDelta(s, i);
		}
		return s + delta;
	}
	
	public static int f_e(int s, int pos) {
		
		// Get these out of the way.
		if (pos == 0 || pos == 7) return s;
		
		int delta = 0;
		
		// This is what it says...
		if (pos < 4) {
			delta += getRotDelta(s, 0);
			delta += getRotDelta(s, 2*pos);
		}
		
		// Or this...
		else {
			delta += getRotDelta(s, NUML-1);
			delta += getRotDelta(s, 2*pos - NUML + 1);
		}
		
		return s + delta;
	}
	
	public static int f_f(int s, int pos) {
		
		// Weird map - I just hard-coded it.
		int[] map = {4,0,5,1,6,2,7,3};
		return s + getRotDelta(s, map[pos]);
	}
	
	// Returns the delta of rotating value at position pos.
	public static int getRotDelta(int s, int pos) {
		int value = (s/pow6[pos])%6;
		if (value < 5) return pow6[pos];
		return -5*pow6[pos];
	}
	
	public static int getVal(int s, int pos) {
		return (s/pow6[pos])%6;
	}
}
