// Arup Guha
// 2/22/2020
// Solution to 2020 NAC Problem H: Letter Wheels
// Written during contest and submitted on Kattis, commented later.

import java.util.*;

public class letterwheels {

	final public static long mod = 100000004987L;

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		
		// Read all 3.
		char[] top = stdin.next().toCharArray();
		char[] mid = stdin.next().toCharArray();
		char[] bot = stdin.next().toCharArray();
		int n = top.length;
		
		HashMap<Long,Integer> map = new HashMap<Long,Integer>();
		
		// this is for hashing.
		long[] pow3 = new long[n];
		pow3[0] = 1;
		for (int i=1; i<n; i++) pow3[i] = (pow3[i-1]*3)%mod;
		
		// Place the hash value of the first mapped to starting at index 0.
		long first = 0;
		for (int i=0; i<n; i++) {
			first = ((3*first) + (bot[i]-'A'))%mod;
		}
		map.put(first, 0);
		
		ArrayList[] indexes =  new ArrayList[n];
		for (int i=0; i<n; i++) indexes[i] = new ArrayList<Integer>();
		indexes[0].add(0);
		long prev = first;
		
		// Loop through each other index.
		for (int i=1; i<n; i++) {
			
			// Update the new hash by sliding over 1 spot.
			int pVal = (int)(bot[i-1]-'A');
			prev = (prev - pow3[n-1]*pVal + 4*mod ) %mod;
			prev = (3*prev+pVal)%mod;
			
			// Haven't seen this string before, add this.
			if (!map.containsKey(prev)) {
				map.put(prev, i);
				indexes[i].add(i);
			}
			
			// Store this index in the appropriate index list.
			else {
				indexes[map.get(prev)].add(i);
			}
		}
		
		int res = -1;
		
		// Now go through the middle index.
		for (int mI=0; mI<n; mI++) {
			
			// cur is storing what the third letter is that we need on the last wheel.
			int[] cur = new int[n];
			boolean ok = true;
			for (int i=0; i<n; i++) {
				if (top[i] == mid[(i+mI)%n]) {ok = false;break;}
				cur[i] = 3 - (top[i]-'A') - (mid[(i+mI)%n]-'A');
			}

			// This means somewhere the first two letters are the same, which isn't allowed.
			if (!ok) continue;
			
			// This is the hash value of the string that we need on the third ring.
			long hash = 0;
			for (int i=0; i<n; i++) {
				hash = (3*hash + cur[i])%mod;
			}
			
			// Oops, it's not there.
			if (!map.containsKey(hash)) continue;
			
			// Here we calculate the smallest number of moves we have to make to get to this orientation.
			int bShift = map.get(hash);
			for (Integer x: (ArrayList<Integer>)indexes[bShift]) {
				int curRes = getBest(mI, x, n);
				if (res == -1) res = curRes;
				else if (curRes < res) res = curRes;
			}
		}
		
		// Ta da!
		System.out.println(res);
	}
	
	// There are three options here, so we just have to try them all.
	public static int getBest(int a, int b, int n) {
		int res = Math.min(a,n-a) + Math.min(b,n-b);
		res = Math.min(res, Math.min(a,n-a)+Math.min(Math.abs(b-a), n-Math.abs(b-a)));
		res = Math.min(res, Math.min(b,n-b)+Math.min(Math.abs(b-a), n-Math.abs(b-a)));
		return res;
	}
}